reset.js ➔ route   F
last analyzed

Complexity

Conditions 11
Paths 6

Size

Total Lines 108

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
c 1
b 0
f 1
nc 6
nop 2
dl 0
loc 108
rs 3.78

1 Function

Rating   Name   Duplication   Size   Complexity  
C reset.js ➔ ... ➔ User.utils.findByResetPasswordToken 0 68 9

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like reset.js ➔ route often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import fs from 'fs-extra'
2
import path from 'path'
3
4
import {coreUtils, Handlebars, config, User} from '../../../../cli'
5
6
var route = function(req, res) {
7
  var resHtml = ''
8
  var page
9
  var template
10
  var tmp
11
  if (
12
    typeof req.body.token !== 'undefined' &&
13
    req.body.token !== null &&
14
    typeof req.body.password !== 'undefined' &&
15
    req.body.password !== null &&
16
    typeof req.body['repeat-password'] !== 'undefined' &&
17
    req.body['repeat-password'] !== null
18
  ) {
19
    if (req.body.password !== req.body['repeat-password']) {
20
      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...
21
      if (coreUtils.file.exist(page)) {
22
        resHtml = fs.readFileSync(page, 'utf8')
23
      }
24
25
      template = Handlebars.compile(resHtml, {noEscape: true})
26
27
      tmp = template({
28
        csrfToken: res.locals.csrfToken,
29
        config: JSON.stringify(config),
30
        express: {
31
          req: req,
32
          res: res
33
        },
34
        token: req.body.token,
35
        info: 'Emails are not the same'
36
      })
37
38
      return res.send(tmp)
39
    }
40
    User.utils.findByResetPasswordToken(req.body.token, function(
41
      err,
42
      userToReset
43
    ) {
44
      var msg = ''
45
      if (err) {
46
        msg = 'Error'
47
      } else if (typeof userToReset === 'undefined' || userToReset === null) {
48
        msg = 'Invalid token'
49
      } else {
50
        var d = new Date().getTime()
51
        d = (d - userToReset.resetPasswordExpires) / 1000 / 60
52
        if (d > 0) {
53
          msg = 'Token expired'
54
        }
55
      }
56
      if (msg !== '') {
57
        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...
58
        if (coreUtils.file.exist(page)) {
59
          resHtml = fs.readFileSync(page, 'utf8')
60
        }
61
62
        template = Handlebars.compile(resHtml, {noEscape: true})
63
64
        tmp = template({
65
          csrfToken: res.locals.csrfToken,
66
          config: JSON.stringify(config),
67
          express: {
68
            req: req,
69
            res: res
70
          },
71
          token: req.body.token,
72
          info: msg
73
        })
74
75
        return res.send(tmp)
76
      }
77
78
      userToReset.password = req.body.password
79
      var resUpdatePassword = User.operations.updatePassword(
80
        userToReset,
81
        req.body.password
82
      )
83
      if (resUpdatePassword.success === 1) {
84
        var login = config.users.login
85
        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...
86
      } else {
87
        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...
88
        if (coreUtils.file.exist(page)) {
89
          resHtml = fs.readFileSync(page, 'utf8')
90
        }
91
92
        template = Handlebars.compile(resHtml, {noEscape: true})
93
94
        tmp = template({
95
          csrfToken: res.locals.csrfToken,
96
          config: JSON.stringify(config),
97
          express: {
98
            req: req,
99
            res: res
100
          },
101
          token: req.body.token,
102
          info: resUpdatePassword.message
103
        })
104
105
        return res.send(tmp)
106
      }
107
    })
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
  } else if (typeof req.body.token !== 'undefined' && req.body.token !== null) {
109
    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...
110
  } else {
111
    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...
112
  }
113
}
114
115
export default route
116