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')
18 // coerce and/or validate
19 email = email.slice(0, 256).toLowerCase()
20 if (email.length === 0)
23 'Minimum length check failed',
27 let transaction = await env.site.database.Transaction()
29 // initialize env.session_key, set cookie in env.response
30 await session_cookie(env, transaction)
34 await transaction.get({})
37 if (account === undefined)
39 'Account does not exist',
40 `Please create the account for "${email}" before attempting to send an email verification link.`
44 if (await account.get_json('email_verified'))
46 'Email already verified',
47 `Your email "${email}" is already verified. You can now sign in.`
51 let link_code = crypto.randomBytes(16).toString('hex')
52 let expires = new XDate()
56 {link_code, expires: expires.getTime()}
59 let given_names = await account.get_json('given_names', '')
60 let family_name = await account.get_json('family_name', '')
62 family_name.length ? `${given_names} ${family_name}` : given_names
64 await nodemailer_noreply.sendMail(
66 from: globals.noreply_from,
67 to: `${name} <${email}>`,
68 subject: 'Email address verification',
69 text: `Dear ${given_names},
71 We have received a request to sign up using your email address.
73 If this request is valid, please verify your email address by visiting the below link:
74 ${globals.site_url}/my_account/verify_email/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
76 The link is valid for 24 hours.
79 ${globals.noreply_signature}
84 await transaction.commit()
87 transaction.rollback()