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')
15 // coerce and/or validate
16 email = email.slice(0, 256).toLowerCase()
17 if (email.length === 0)
20 'Minimum length check failed',
24 let transaction = await env.site.database.Transaction()
26 let nodemailer_noreply, site_url, noreply_from, noreply_signature
27 let given_names, family_name
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 link_code = crypto.randomBytes(16).toString('hex')
52 let expires = new XDate()
56 {link_code, expires: expires.getTime()}
59 let root = await transaction.get({})
60 nodemailer_noreply = nodemailer.createTransport(
61 await (await root.get('nodemailer')).get_json('noreply')
63 let globals = await root.get('globals')
64 site_url = await globals.get_json('site_url')
65 noreply_from = await globals.get_json('noreply_from')
66 noreply_signature = await globals.get_json('noreply_signature')
68 given_names = await account.get_json('given_names', '')
69 family_name = await account.get_json('family_name', '')
71 await transaction.commit()
74 transaction.rollback()
79 family_name.length ? `${given_names} ${family_name}` : given_names
80 await nodemailer_noreply.sendMail(
83 to: `${name} <${email}>`,
84 subject: 'Email address verification',
85 text: `Dear ${given_names},
87 We have received a request to sign up using your email address.
89 If this request is valid, please verify your email address by visiting the below link:
90 ${site_url}/my_account/verify_email/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
92 The link is valid for 24 hours.