Completed
Push — master ( 78d2f9...949122 )
by greg
42s
created

src/server/routes/users/post/reset.js   A

Complexity

Total Complexity 20
Complexity/F 10

Size

Lines of Code 111
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 20
nc 1
mnd 4
bc 16
fnc 2
dl 0
loc 111
rs 10
bpm 8
cpm 10
noi 7
c 2
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C reset.js ➔ route 0 99 11
1
import fs from 'fs-extra'
2
import path from 'path'
3
4
import {
5
  coreUtils,
6
  Handlebars,
7
  config,
8
  User
9
} from '../../../../cli'
10
11
var route = function(req, res) {
12
  var resHtml = ''
13
  var page
14
  var template
15
  var tmp
16
  if(typeof req.body.token !== 'undefined' && req.body.token !== null
17
    && typeof req.body.password !== 'undefined' && req.body.password !== null
18
    && typeof req.body['repeat-password'] !== 'undefined' && req.body['repeat-password'] !== null) {
19
    if (req.body.password !== req.body['repeat-password']) {
20
21
      page = path.join(__dirname + '/../../../views/users/reset.html')
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
22
      if (coreUtils.file.exist(page)) {
23
        resHtml = fs.readFileSync(page, 'utf8')
24
      }
25
      
26
      template = Handlebars.compile(resHtml, {noEscape: true})
27
28
      tmp = template({
29
        csrfToken: res.locals.csrfToken,
30
        config: JSON.stringify(config),
31
        express: {
32
          req: req,
33
          res: res
34
        },
35
        token: req.body.token,
36
        info: 'Emails are not the same'
37
      })
38
39
      return res.send(tmp)
40
    }
41
    User.utils.findByResetPasswordToken(req.body.token, function (err, userToReset) {
42
      var msg = ''
43
      if (err) {
44
        msg = 'Error'
45
      }else if (typeof userToReset === 'undefined' || userToReset === null) {
46
        msg = 'Invalid token'
47
      }else {
48
        var d = new Date().getTime()
49
        d = (((d - userToReset.resetPasswordExpires) / 1000) / 60)
50
        if (d > 0) {
51
          msg = 'Token expired'
52
        }
53
      }
54
      if (msg !== '') {
55
56
        page = path.join(__dirname + '/../../../views/users/reset.html')
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
57
        if (coreUtils.file.exist(page)) {
58
          resHtml = fs.readFileSync(page, 'utf8')
59
        }
60
        
61
        template = Handlebars.compile(resHtml, {noEscape: true})
62
63
        tmp = template({
64
          csrfToken: res.locals.csrfToken,
65
          config: JSON.stringify(config),
66
          express: {
67
            req: req,
68
            res: res
69
          },
70
          token: req.body.token,
71
          info: msg
72
        })
73
74
        return res.send(tmp)
75
      }
76
77
      userToReset.password = req.body.password
78
      var resUpdatePassword = User.operations.updatePassword(userToReset, req.body.password)
79
      if (resUpdatePassword.success === 1) {
80
        var login = config.users.login
81
        res.redirect(login)
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
82
      }else {
83
        page = path.join(__dirname + '/../../../views/users/reset.html')
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
84
        if (coreUtils.file.exist(page)) {
85
          resHtml = fs.readFileSync(page, 'utf8')
86
        }
87
        
88
        template = Handlebars.compile(resHtml, {noEscape: true})
89
90
        tmp = template({
91
          csrfToken: res.locals.csrfToken,
92
          config: JSON.stringify(config),
93
          express: {
94
            req: req,
95
            res: res
96
          },
97
          token: req.body.token,
98
          info: resUpdatePassword.message
99
        })
100
101
        return res.send(tmp)
102
      }
103
    })
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
104
  }else if(typeof req.body.token !== 'undefined' && req.body.token !== null) {
105
    res.redirect('/abe/users/reset?token=' + req.body.token)
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
106
  }else {
107
    res.redirect('/abe/users/forgot')
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
108
  }
109
}
110
111
export default route