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')
19 // coerce and/or validate
20 email = email.slice(0, 256).toLowerCase()
21 if (email.length === 0)
24 'Minimum length check failed',
28 let transaction = await env.site.database.Transaction()
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 send an email verification link.`
46 await logjson.logjson_to_json(
47 await account.get('email_verified')
51 'Email already verified',
52 `Your email "${email}" is already verified. You can now sign in.`
56 let link_code = crypto.randomBytes(16).toString('hex')
57 let expires = new XDate()
61 transaction.json_to_logjson({link_code, expires: expires.getTime()})
64 let given_names = await logjson.logjson_to_json(
65 await account.get('given_names', '')
67 let family_name = await logjson.logjson_to_json(
68 await account.get('family_name', '')
71 family_name.length ? `${given_names} ${family_name}` : given_names
73 await nodemailer_noreply.sendMail(
75 from: globals.noreply_from,
76 to: `${name} <${email}>`,
77 subject: 'Email address verification',
78 text: `Dear ${given_names},
80 We have received a request to sign up using your email address.
82 If this request is valid, please verify your email address by visiting the below link:
83 ${globals.site_url}/my_account/verify_email/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
85 The link is valid for 24 hours.
88 ${globals.noreply_signature}
93 await transaction.commit()
96 transaction.rollback()