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

User.utils.findByResetPasswordToken   C

Complexity

Conditions 9
Paths 20

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 20
dl 0
loc 63
rs 6.6149
c 0
b 0
f 0
nop 2

How to fix   Long Method   

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:

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