1 let crypto = require('crypto')
2 let logjson = (await import('@ndcode/logjson')).default
3 let XDate = require('xdate')
6 let post_request = await _require('/_lib/post_request.jst')
7 let session_cookie = await _require('/_lib/session_cookie.jst')
8 let Problem = await _require('/_lib/Problem.jst')
14 '/api/sign_up/verify_email.json',
16 async (email, link_code) => {
17 // coerce and/or validate
18 email = email.slice(0, 256).toLowerCase()
19 link_code = link_code.slice(0, 256).toLowerCase()
20 if (email.length === 0 || link_code.length < 32)
23 'Minimum length check failed',
27 let transaction = await env.site.database.Transaction()
29 // initialize env.session_key, set cookie in env.response
30 await session_cookie(env, transaction)
34 await transaction.get({})
37 if (account === undefined)
39 'Account does not exist',
40 `Please create the account for "${email}" before attempting to verify the email verification link.`
45 await logjson.logjson_to_json(
46 await account.get('email_verified')
50 'Email already verified',
51 `Your email "${email}" is already verified. You can now sign in.`
55 let verify_email = await account.get('verify_email')
57 verify_email === undefined ||
58 XDate.now() >= await logjson.logjson_to_json(
59 await verify_email.get('expires')
64 `Email verification link code for account "${email}" does not exist or has expired.`,
68 link_code !== await logjson.logjson_to_json(
69 await verify_email.get('link_code')
74 `Provided email verification link code "${link_code}" does not match expected value.`,
78 await account.delete('verify_email')
79 await account.set('email_verified', true)
81 await transaction.commit()
84 transaction.rollback()