1 let crypto = require('crypto')
2 let nodemailer = require('nodemailer')
3 let XDate = require('xdate')
6 let post_request = await _require('/_lib/post_request.jst')
7 let session_cookie = await _require('/_lib/session_cookie.jst')
8 let Problem = await _require('/_lib/Problem.jst')
14 async (email, password) => {
15 // coerce and/or validate
16 email = email.slice(0, 256).toLowerCase()
17 password = password.slice(0, 256)
18 if (email.length === 0 || password.length < 8)
21 'Minimum length check failed',
25 let transaction = await env.site.database.Transaction()
27 let nodemailer_noreply, noreply_from, noreply_signature
28 let given_names, family_name
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 link_code = crypto.randomBytes(16).toString('hex')
46 let expires = new XDate()
50 {password, link_code, expires: expires.getTime()}
53 let root = await transaction.get({})
54 nodemailer_noreply = nodemailer.createTransport(
55 await (await root.get('nodemailer')).get_json('noreply')
57 let globals = await root.get('globals')
58 site_url = await globals.get_json('site_url')
59 noreply_from = await globals.get_json('noreply_from')
60 noreply_signature = await globals.get_json('noreply_signature')
62 given_names = await account.get_json('given_names', '')
63 family_name = await account.get_json('family_name', '')
65 await transaction.commit()
68 transaction.rollback()
73 family_name.length ? `${given_names} ${family_name}` : given_names
74 await nodemailer_noreply.sendMail(
77 to: `${name} <${email}>`,
78 subject: 'Password reset',
79 text: `Dear ${given_names},
81 We have received a request to reset the account password for your email address.
83 If this request is valid, please verify the new password by visiting the below link:
84 ${site_url}/my_account/verify_password/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
86 The link is valid for 24 hours.