Update /my_account/verify_password/index.html.jst to latest way, no accordion
[ndcode_site.git] / api / account / sign_up / send_email_verification_link.json.jst
1 let crypto = require('crypto')
2 let jst_server = (await import('@ndcode/jst_server')).default
3 let XDate = require('xdate')
4
5 return async env => {
6   let get_nodemailer = await _require('/_lib/get_nodemailer.jst')
7   let post_request = await _require('/_lib/post_request.jst')
8
9   await post_request(
10     // env
11     env,
12     // handler
13     async email => {
14       // coerce and/or validate
15       email = email.slice(0, 256).toLowerCase()
16       if (email.length === 0)
17         throw new jst_server.Problem(
18           'Bad request',
19           'Minimum length check failed',
20           400
21         )
22
23       let transaction = await env.site.database.Transaction()
24       let link_code
25       let nodemailer
26       let site_url, noreply_from, noreply_signature
27       let given_names, family_name
28       try {
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.`
36             421
37           )
38
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.`
43             422
44           )
45
46         link_code = crypto.randomBytes(16).toString('hex')
47         let expires = new XDate()
48         expires.addDays(1)
49         account.set_json(
50           'verify_email',
51           {link_code, expires: expires.getTime()}
52         )
53
54         nodemailer = await get_nodemailer(root, 'noreply')
55
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')
60
61         given_names = await account.get_json('given_names', '')
62         family_name = await account.get_json('family_name', '')
63
64         await transaction.commit()
65       }
66       catch (error) {
67         transaction.rollback()
68         throw error
69       }
70
71       let name =
72         family_name.length ? `${given_names} ${family_name}` : given_names
73       await nodemailer.sendMail(
74         {
75           from: noreply_from,
76           to: `${name} <${email}>`,
77           subject: 'Email address verification',
78           text: `Dear ${given_names},
79
80 We have received a request to sign up using your email address.
81
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)}
84
85 The link is valid for 24 hours.
86
87 Thanks,
88 ${noreply_signature}
89 `
90         }
91       )
92     }
93   )
94 }