1 let crypto = require('crypto')
2 let XDate = require('xdate')
5 let post_request = await _require('/_lib/post_request.jst')
6 let session_cookie = await _require('/_lib/session_cookie.jst')
7 let Problem = await _require('/_lib/Problem.jst')
13 async (email, link_code) => {
14 // coerce and/or validate
15 email = email.slice(0, 256).toLowerCase()
16 link_code = link_code.slice(0, 256).toLowerCase()
17 if (email.length === 0 || link_code.length < 32)
20 'Minimum length check failed',
24 let transaction = await env.site.database.Transaction()
26 // initialize env.session_key, set cookie in env.response
27 await session_cookie(env, transaction)
31 await transaction.get({})
34 if (account === undefined)
36 'Account does not exist',
37 `Please create the account for "${email}" before attempting to verify the email verification link.`
41 if (await account.get_json('email_verified'))
43 'Email already verified',
44 `Your email "${email}" is already verified. You can now sign in.`
48 let verify_email = await account.get('verify_email')
50 verify_email === undefined ||
51 XDate.now() >= await verify_email.get_json('expires')
55 `Email verification link code for account "${email}" does not exist or has expired.`,
58 if (link_code !== await verify_email.get_json('link_code'))
61 `Provided email verification link code "${link_code}" does not match expected value.`,
65 await account.delete('verify_email')
66 await account.set('email_verified', true)
68 await transaction.commit()
71 transaction.rollback()