1 let crypto = require('crypto')
2 let jst_server = (await import('@ndcode/jst_server')).default
5 let post_request = await _require('/_lib/post_request.jst')
11 async (email, link_code) => {
12 // coerce and/or validate
13 email = email.slice(0, 256).toLowerCase()
14 link_code = link_code.slice(0, 256).toLowerCase()
15 if (email.length === 0 || link_code.length < 32)
16 throw new jst_server.Problem(
18 'Minimum length check failed',
22 let transaction = await env.site.database.Transaction()
24 let root = await transaction.get({})
25 let accounts = await root.get('accounts', {})
26 let account = await accounts.get(email)
27 if (account === undefined)
28 throw new jst_server.Problem(
29 'Account does not exist',
30 `Please create the account for "${email}" before attempting to verify the email verification link.`
34 if (await account.get_json('email_verified'))
35 throw new jst_server.Problem(
36 'Email already verified',
37 `Your email "${email}" is already verified. You can now sign in.`
41 let verify_email = await account.get('verify_email')
43 verify_email === undefined ||
44 env.now >= await verify_email.get_json('expires')
46 throw new jst_server.Problem(
48 `Email verification link code for account "${email}" does not exist or has expired.`,
51 if (link_code !== await verify_email.get_json('link_code'))
52 throw new jst_server.Problem(
54 `Provided email verification link code "${link_code}" does not match expected value.`,
58 await account.delete('verify_email')
59 await account.set('email_verified', true)
60 await transaction.commit()
63 transaction.rollback()