1 let crypto = require('crypto')
2 let logjson = (await import('@ndcode/logjson')).default
3 let XDate = require('xdate')
6 let globals = await env.site.get_json('/_config/globals.json')
7 let nodemailer_noreply = await env.site.get_nodemailer(
8 '/_config/nodemailer_noreply.json'
10 let post_request = await _require('/_lib/post_request.jst')
11 let session_cookie = await _require('/_lib/session_cookie.jst')
12 let Problem = await _require('/_lib/Problem.jst')
18 async (email, password) => {
19 // coerce and/or validate
20 email = email.slice(0, 256).toLowerCase()
21 password = password.slice(0, 256)
22 if (email.length === 0 || password.length < 8)
25 'Minimum length check failed',
29 let transaction = await env.site.database.Transaction()
31 // initialize env.session_key, set cookie in env.response
32 await session_cookie(env, transaction)
36 await transaction.get({})
39 if (account === undefined)
41 'Account does not exist',
42 `Please create the account for "${email}" before attempting to reset its password.`
46 let link_code = crypto.randomBytes(16).toString('hex')
47 let expires = new XDate()
51 transaction.json_to_logjson(
55 expires: expires.getTime()
60 let given_names = await logjson.logjson_to_json(
61 await account.get('given_names', '')
63 let family_name = await logjson.logjson_to_json(
64 await account.get('family_name', '')
67 family_name.length ? `${given_names} ${family_name}` : given_names
69 await nodemailer_noreply.sendMail(
71 from: globals.noreply_from,
72 to: `${name} <${email}>`,
73 subject: 'Password reset',
74 text: `Dear ${given_names},
76 We have received a request to reset the account password for your email address.
78 If this request is valid, please verify the new password by visiting the below link:
79 ${globals.site_url}/my_account/verify_password/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
81 The link is valid for 24 hours.
84 ${globals.noreply_signature}
89 await transaction.commit()
92 transaction.rollback()