1 let XDate = require('xdate')
4 let post_request = await _require('/_lib/post_request.jst')
5 let session_cookie = await _require('/_lib/session_cookie.jst')
6 let Problem = await _require('/_lib/Problem.jst')
12 '/api/sign_up/create_account.json',
14 async (verification_code, details) => {
15 // coerce and/or validate
16 verification_code = verification_code.slice(0, 6).toLowerCase()
18 email: details.email.slice(0, 256).toLowerCase(),
19 given_names: details.given_names.slice(0, 256),
20 family_name: details.family_name.slice(0, 256),
21 password: details.password.slice(0, 256),
22 contact_me: details.contact_me ? true : false
25 verification_code.length < 6 ||
26 details.given_names.length === 0 ||
27 details.password.length < 8
31 'Minimum length check failed',
35 let transaction = await env.site.database.Transaction()
37 // initialize env.session_key, set cookie in env.response
38 let session = await session_cookie(env, transaction)
40 let captcha = await session.get('captcha')
41 if (captcha === undefined || XDate.now() >= captcha.get('expires'))
43 'No verification image in session',
44 `Please call the "/api/verification_image.png" endpoint to create a verification image, in same session as the "/api/sign_up/create_account.json" call and less than one hour prior.`,
48 let captcha_text = await captcha.get('text')
49 if (verification_code !== captcha_text) {
50 console.log(`verification code mismatch, \"${verification_code}\" should be \"${captcha_text}\"`)
53 'Verification code mismatch',
54 `The provided verification code "${verification_code}" did not match the verification image.`,
59 let accounts = await (
60 await transaction.get({})
63 if (accounts.has(details.email))
65 'Account already exists',
66 `The email "${details.email}" already has an account registered.`,
69 accounts.set(details.email, transaction.json_to_logjson(details))
71 await transaction.commit()
74 transaction.rollback()