Conditions | 7 |
Paths | 18 |
Total Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 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:
1 | import fs from 'fs-extra' |
||
44 | crypto.randomBytes(20, function(err, buf) { |
||
45 | var resetPasswordToken = buf.toString('hex') |
||
46 | var forgotExpire = config.forgotExpire |
||
47 | |||
48 | User.operations.update({ |
||
49 | id:user.id, |
||
50 | resetPasswordToken: resetPasswordToken, |
||
51 | resetPasswordExpires: Date.now() + (forgotExpire*60*1000) |
||
52 | }) |
||
53 | |||
54 | var requestedUrl = req.protocol + '://' + req.get('Host') + '/abe/users/reset?token=' + resetPasswordToken |
||
55 | |||
56 | var smtp = config.smtp |
||
57 | var emailConf = config.users.email |
||
58 | html = emailConf.html || '' |
||
59 | |||
60 | if(typeof emailConf.templateHtml !== 'undefined' && emailConf.templateHtml !== null) { |
||
61 | var fileHtml = path.join(config.root, emailConf.templateHtml) |
||
62 | if (coreUtils.file.exist(fileHtml)) { |
||
63 | html = fs.readFileSync(fileHtml, 'utf8') |
||
64 | } |
||
65 | } |
||
66 | |||
67 | var template = Handlebars.compile(html, {noEscape: true}) |
||
68 | |||
69 | html = template({ |
||
70 | express: { |
||
71 | req: req, |
||
72 | res: res |
||
73 | }, |
||
74 | forgotUrl: requestedUrl, |
||
75 | siteUrl: req.protocol + '://' + req.get('Host'), |
||
76 | user: user |
||
77 | }) |
||
78 | |||
79 | if(typeof smtp === 'undefined' || smtp === null) { |
||
80 | var transport = nodemailer.createTransport('direct', {}) |
||
81 | transport.sendMail({ |
||
82 | from: emailConf.from, // sender address |
||
83 | to: user.email, // list of receivers |
||
84 | subject: emailConf.subject, // Subject line |
||
85 | text: emailConf.text.replace(/\{\{forgotUrl\}\}/g, requestedUrl), // plaintext body |
||
86 | html: html.replace(/\{\{forgotUrl\}\}/g, requestedUrl) // html body |
||
87 | }, console.error) |
||
88 | |||
89 | showHtml(res, req, 'Check your inbox') |
||
90 | }else if(typeof smtp !== 'string') { |
||
91 | // create reusable transporter object using the default SMTP transport |
||
92 | var transporter = nodemailer.createTransport('SMTP', smtp) |
||
93 | |||
94 | // setup e-mail data with unicode symbols |
||
95 | var mailOptions = { |
||
96 | from: emailConf.from, // sender address |
||
97 | to: user.email, // list of receivers |
||
98 | subject: emailConf.subject, // Subject line |
||
99 | text: emailConf.text.replace(/\{\{forgotUrl\}\}/g, requestedUrl), // plaintext body |
||
100 | html: html.replace(/\{\{forgotUrl\}\}/g, requestedUrl) // html body |
||
101 | } |
||
102 | |||
103 | // send mail with defined transport object |
||
104 | transporter.sendMail(mailOptions, function(error, info){ |
||
105 | if(error){ |
||
106 | return console.log(error) |
||
107 | } |
||
108 | |||
109 | showHtml(res, req, 'Check your inbox') |
||
110 | console.log('Message sent: ' + info.response) |
||
111 | }) |
||
112 | } |
||
113 | }) |
||
114 | }) |
||
120 | export default route |