Add /api/sign_(in|out).json endpoints, uncomment sign in/out logic in navbar and...
[ndcode_site.git] / api / sign_out.json.jst
1 let logjson = (await import('@ndcode/logjson')).default
2
3 return async env => {
4   let globals = await env.site.get_json('/_config/globals.json')
5   let nodemailer_noreply = await env.site.get_nodemailer(
6     '/_config/nodemailer_noreply.json'
7   )
8   let post_request = await _require('/_lib/post_request.jst')
9   let session_cookie = await _require('/_lib/session_cookie.jst')
10   let Problem = await _require('/_lib/Problem.jst')
11
12   post_request(
13     // env
14     env,
15     // endpoint
16     '/api/sign_out.json',
17     // handler
18     async () => {
19       let transaction = await env.site.database.Transaction()
20       try {
21         // initialize env.session_key, set cookie in env.response
22         let session = await session_cookie(env, transaction)
23
24         session.set('signed_in_as', null)
25         await transaction.commit()
26       }
27       catch (error) {
28         transaction.rollback()
29         throw error
30       }
31     }
32   )
33 }