1 let crypto = require('crypto')
2 let XDate = require('xdate')
5 let nodemailer_noreply = await env.site.get_nodemailer(
6 '/_config/nodemailer_noreply.json'
8 let post_request = await _require('/_lib/post_request.jst')
9 let session_cookie = await _require('/_lib/session_cookie.jst')
10 let Problem = await _require('/_lib/Problem.jst')
17 // coerce and/or validate
18 email = email.slice(0, 256).toLowerCase()
19 if (email.length === 0)
22 'Minimum length check failed',
26 let transaction = await env.site.database.Transaction()
28 // initialize env.session_key, set cookie in env.response
29 await session_cookie(env, transaction)
33 await transaction.get({})
36 if (account === undefined)
38 'Account does not exist',
39 `Please create the account for "${email}" before attempting to send an email verification link.`
43 if (await account.get_json('email_verified'))
45 'Email already verified',
46 `Your email "${email}" is already verified. You can now sign in.`
50 let link_code = crypto.randomBytes(16).toString('hex')
51 let expires = new XDate()
55 {link_code, expires: expires.getTime()}
58 let globals = await (await transaction.get({})).get('globals')
59 let site_url = await globals.get_json('site_url')
60 let noreply_from = await globals.get_json('noreply_from')
61 let noreply_signature = await globals.get_json('noreply_signature')
63 let given_names = await account.get_json('given_names', '')
64 let family_name = await account.get_json('family_name', '')
66 family_name.length ? `${given_names} ${family_name}` : given_names
68 await nodemailer_noreply.sendMail(
71 to: `${name} <${email}>`,
72 subject: 'Email address verification',
73 text: `Dear ${given_names},
75 We have received a request to sign up using your email address.
77 If this request is valid, please verify your email address by visiting the below link:
78 ${site_url}/my_account/verify_email/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
80 The link is valid for 24 hours.
88 await transaction.commit()
91 transaction.rollback()