1 let crypto = require('crypto')
2 let XDate = require('xdate')
5 let post_request = await _require('/_lib/post_request.jst')
6 let Problem = await _require('/_lib/Problem.jst')
12 async (email, link_code) => {
13 // coerce and/or validate
14 email = email.slice(0, 256).toLowerCase()
15 link_code = link_code.slice(0, 256).toLowerCase()
16 if (email.length === 0 || link_code.length < 32)
19 'Minimum length check failed',
23 let transaction = await env.site.database.Transaction()
25 let root = await transaction.get({})
26 let accounts = await root.get('accounts', {})
27 let account = await accounts.get(email)
28 if (account === undefined)
30 'Account does not exist',
31 `Please create the account for "${email}" before attempting to verify the email verification link.`
35 if (await account.get_json('email_verified'))
37 'Email already verified',
38 `Your email "${email}" is already verified. You can now sign in.`
42 let verify_email = await account.get('verify_email')
44 verify_email === undefined ||
45 XDate.now() >= await verify_email.get_json('expires')
49 `Email verification link code for account "${email}" does not exist or has expired.`,
52 if (link_code !== await verify_email.get_json('link_code'))
55 `Provided email verification link code "${link_code}" does not match expected value.`,
59 await account.delete('verify_email')
60 await account.set('email_verified', true)
61 await transaction.commit()
64 transaction.rollback()