1 let crypto = require('crypto')
2 let XDate = require('xdate')
5 let globals = await env.site.get_json('/_config/globals.json')
6 let nodemailer_noreply = await env.site.get_nodemailer(
7 '/_config/nodemailer_noreply.json'
9 let post_request = await _require('/_lib/post_request.jst')
10 let session_cookie = await _require('/_lib/session_cookie.jst')
11 let Problem = await _require('/_lib/Problem.jst')
17 async (email, password) => {
18 // coerce and/or validate
19 email = email.slice(0, 256).toLowerCase()
20 password = password.slice(0, 256)
21 if (email.length === 0 || password.length < 8)
24 'Minimum length check failed',
28 let transaction = await env.site.database.Transaction()
30 // initialize env.session_key, set cookie in env.response
31 await session_cookie(env, transaction)
35 await transaction.get({})
38 if (account === undefined)
40 'Account does not exist',
41 `Please create the account for "${email}" before attempting to reset its password.`
45 let link_code = crypto.randomBytes(16).toString('hex')
46 let expires = new XDate()
50 {password, link_code, expires: expires.getTime()}
53 let given_names = await account.get_json('given_names', '')
54 let family_name = await account.get_json('family_name', '')
56 family_name.length ? `${given_names} ${family_name}` : given_names
58 await nodemailer_noreply.sendMail(
60 from: globals.noreply_from,
61 to: `${name} <${email}>`,
62 subject: 'Password reset',
63 text: `Dear ${given_names},
65 We have received a request to reset the account password for your email address.
67 If this request is valid, please verify the new password by visiting the below link:
68 ${globals.site_url}/my_account/verify_password/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
70 The link is valid for 24 hours.
73 ${globals.noreply_signature}
78 await transaction.commit()
81 transaction.rollback()