Conditions | 11 |
Paths | 6 |
Total Lines | 99 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
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' |
||
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') |
||
|
|||
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') |
||
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) |
||
82 | }else { |
||
83 | page = path.join(__dirname + '/../../../views/users/reset.html') |
||
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 | }) |
||
104 | }else if(typeof req.body.token !== 'undefined' && req.body.token !== null) { |
||
105 | res.redirect('/abe/users/reset?token=' + req.body.token) |
||
106 | }else { |
||
107 | res.redirect('/abe/users/forgot') |
||
108 | } |
||
109 | } |
||
110 | |||
111 | export default route |