1 let crypto = require('crypto')
2 let jst_server = (await import('@ndcode/jst_server')).default
3 let XDate = require('xdate')
6 let get_nodemailer = await _require('/_lib/get_nodemailer.jst')
7 let post_request = await _require('/_lib/post_request.jst')
14 // coerce and/or validate
15 email = email.slice(0, 256).toLowerCase()
16 if (email.length === 0)
17 throw new jst_server.Problem(
19 'Minimum length check failed',
23 let transaction = await env.site.database.Transaction()
26 let site_url, noreply_from, noreply_signature
27 let given_names, family_name
29 let root = await transaction.get({})
30 let accounts = await root.get('accounts', {})
31 let account = await accounts.get(email)
32 if (account === undefined)
33 throw new jst_server.Problem(
34 'Account does not exist',
35 `Please create the account for "${email}" before attempting to send an email verification link.`
39 if (await account.get_json('email_verified'))
40 throw new jst_server.Problem(
41 'Email already verified',
42 `Your email "${email}" is already verified. You can now sign in.`
46 link_code = crypto.randomBytes(16).toString('hex')
47 let expires = new XDate(env.now)
51 {link_code, expires: expires.getTime()}
54 nodemailer = await get_nodemailer(root, 'noreply')
56 let globals = await root.get('globals', {})
57 site_url = await globals.get_json('site_url')
58 noreply_from = await globals.get_json('noreply_from')
59 noreply_signature = await globals.get_json('noreply_signature')
61 given_names = await account.get_json('given_names', '')
62 family_name = await account.get_json('family_name', '')
64 await transaction.commit()
67 transaction.rollback()
72 family_name.length ? `${given_names} ${family_name}` : given_names
73 await nodemailer.sendMail(
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 ${site_url}/my_account/verify_email/index.html?email=${encodeURIComponent(email)}&link_code=${encodeURIComponent(link_code)}
85 The link is valid for 24 hours.