Add /_lib/get_account.jst, remove env.signed_in_as
authorNick Downing <nick@ndcode.org>
Sun, 23 Jan 2022 08:23:14 +0000 (19:23 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 23 Jan 2022 08:35:36 +0000 (19:35 +1100)
_lib/get_account.jst [new file with mode: 0644]
_lib/get_session.jst
_lib/navbar.jst
api/account/change_details/get.json.jst
api/account/change_details/set.json.jst
api/account/change_password.json.jst
api/globals/get.json.jst
api/globals/set.json.jst
api/nodemailer/get.json.jst
api/nodemailer/set.json.jst
my_account/index.html.jst

diff --git a/_lib/get_account.jst b/_lib/get_account.jst
new file mode 100644 (file)
index 0000000..6a44637
--- /dev/null
@@ -0,0 +1,12 @@
+return async (env, transaction, session) => {
+  let accounts = await (
+    await transaction.get({})
+  ).get('accounts', {})
+
+  let signed_in_as = await session.get('signed_in_as', null)
+  return (
+    signed_in_as !== null ?
+      await accounts.get(signed_in_as, {}) :
+      undefined
+  )
+}
index 98c8761..c532c8e 100644 (file)
@@ -14,7 +14,5 @@ return async (env, transaction) => {
     session = Transaction.json_to_logjson({expires: expires.getTime()})
     sessions.set(env.session_key, session)
   }
-
-  env.signed_in_as = await session.get_json('signed_in_as', null)
   return session
 }
index ae7ed4b..9f200e9 100644 (file)
@@ -3,6 +3,7 @@ let XDate = require('xdate')
 
 return async (env, head, body, scripts) => {
   //let cart = await _require('/online_store/cart.jst')
+  let get_session = await _require('/_lib/get_session.jst')
   //let icon_cart_small = await env.site.get_min_svg('/_svg/icon_cart_small.svg')
   let icon_search_mono = await env.site.get_min_svg('/_svg/icon_search_mono.svg')
   let logo_large = await env.site.get_min_svg('/_svg/logo_large.svg')
@@ -12,6 +13,16 @@ return async (env, head, body, scripts) => {
   // initialize env.cart
   //await cart(env)
 
+  let transaction = await env.site.Transaction()
+  let signed_in_as
+  try {
+    let session = await get_session(env, transaction)
+    signed_in_as = session.get_json('signed_in_as', null)
+  }
+  finally {
+    transaction.rollback()
+  }
+
   await page(
     env,
     // head
@@ -54,23 +65,23 @@ return async (env, head, body, scripts) => {
             div.'col-sm-4' {
               div.'mb-1'.text-right {
                 span#signed-in-status {
-                  if (env.signed_in_as !== null)
-                    'Signed in.' //`Signed in as ${env.signed_in_as}.`
+                  if (signed_in_as !== null)
+                    'Signed in.' //`Signed in as ${signed_in_as}.`
                   else
                     'Browsing as guest.'
                 }
                 ' '
-                if (env.signed_in_as !== null)
+                if (signed_in_as !== null)
                   a#sign-in(href="#" style="display: none;") {'Sign in'}
                 else
                   a#sign-in(href="#") {'Sign in'}
                 ' '
-                if (env.signed_in_as !== null)
+                if (signed_in_as !== null)
                   a#sign-up(href="/my_account/sign_up/index.html" style="display: none;") {'Sign up'}
                 else
                   a#sign-up(href="/my_account/sign_up/index.html") {'Sign up'}
                 ' '
-                if (env.signed_in_as !== null)
+                if (signed_in_as !== null)
                   a#sign-out(href="#") {'Sign out'}
                 else
                   a#sign-out(href="#" style="display: none;") {'Sign out'}
index 8a1e74a..d3a78ca 100644 (file)
@@ -1,8 +1,9 @@
 let XDate = require('xdate')
 
 return async env => {
-  let post_request = await _require('/_lib/post_request.jst')
+  let get_account = await _require('/_lib/get_account.jst')
   let get_session = await _require('/_lib/get_session.jst')
+  let post_request = await _require('/_lib/post_request.jst')
   let Problem = await _require('/_lib/Problem.jst')
 
   await post_request(
@@ -12,16 +13,14 @@ return async env => {
     async () => {
       let transaction = await env.site.database.Transaction()
       try {
-        // initialize env.session_key, set cookie in env.response
-        await get_session(env, transaction)
-        if (env.signed_in_as === null)
+        let account = await get_account(
+          env,
+          transaction,
+          await get_session(env, transaction)
+        )
+        if (account === undefined)
           throw new Problem('Unauthorized', 'Please sign in first.', 401)
 
-        let account = await (
-          await (
-            await transaction.get({})
-          ).get('accounts', {})
-        ).get(env.signed_in_as)
         let details = {
           given_names: await account.get_json('given_names'),
           family_name: await account.get_json('family_name'),
index 5a1bd43..0000541 100644 (file)
@@ -1,8 +1,9 @@
 let XDate = require('xdate')
 
 return async env => {
-  let post_request = await _require('/_lib/post_request.jst')
+  let get_account = await _require('/_lib/get_account.jst')
   let get_session = await _require('/_lib/get_session.jst')
+  let post_request = await _require('/_lib/post_request.jst')
   let Problem = await _require('/_lib/Problem.jst')
 
   await post_request(
@@ -24,20 +25,17 @@ return async env => {
 
       let transaction = await env.site.database.Transaction()
       try {
-        // initialize env.session_key, set cookie in env.response
-        await get_session(env, transaction)
-        if (env.signed_in_as === null)
+        let account = await get_account(
+          env,
+          transaction,
+          await get_session(env, transaction)
+        )
+        if (account === undefined)
           throw new Problem('Unauthorized', 'Please sign in first.', 401)
 
-        let account = await (
-          await (
-            await transaction.get({})
-          ).get('accounts', {})
-        ).get(env.signed_in_as)
         account.set_json('given_names', details.given_names)
         account.set_json('family_name', details.family_name)
         account.set_json('contact_me', details.contact_me)
-
         await transaction.commit()
       }
       catch (error) {
index d75e91d..6192e03 100644 (file)
@@ -2,8 +2,9 @@ let crypto = require('crypto')
 let XDate = require('xdate')
 
 return async env => {
-  let post_request = await _require('/_lib/post_request.jst')
+  let get_account = await _require('/_lib/get_account.jst')
   let get_session = await _require('/_lib/get_session.jst')
+  let post_request = await _require('/_lib/post_request.jst')
   let Problem = await _require('/_lib/Problem.jst')
 
   await post_request(
@@ -23,17 +24,14 @@ return async env => {
 
       let transaction = await env.site.database.Transaction()
       try {
-        // initialize env.session_key, set cookie in env.response
-        await get_session(env, transaction)
-        if (env.signed_in_as === null)
+        let account = await get_account(
+          env,
+          transaction,
+          await get_session(env, transaction)
+        )
+        if (account === undefined)
           throw new Problem('Unauthorized', 'Please sign in first.', 401)
 
-        let account = await (
-          await (
-            await transaction.get({})
-          ).get('accounts', {})
-        ).get(env.signed_in_as)
-
         if (old_password !== await account.get_json('password'))
           throw new Problem(
             'Incorrect password',
index 04d64ed..a87a59b 100644 (file)
@@ -1,8 +1,9 @@
 let XDate = require('xdate')
 
 return async env => {
-  let post_request = await _require('/_lib/post_request.jst')
+  let get_account = await _require('/_lib/get_account.jst')
   let get_session = await _require('/_lib/get_session.jst')
+  let post_request = await _require('/_lib/post_request.jst')
   let Problem = await _require('/_lib/Problem.jst')
 
   await post_request(
@@ -12,19 +13,17 @@ return async env => {
     async () => {
       let transaction = await env.site.database.Transaction()
       try {
-        // initialize env.session_key, set cookie in env.response
-        await get_session(env, transaction)
-        if (env.signed_in_as === null)
+        let account = await get_account(
+          env,
+          transaction,
+          await get_session(env, transaction)
+        )
+        if (account === undefined)
           throw new Problem('Unauthorized', 'Please sign in first.', 401)
-
-        let root = await transaction.get({})
-        let account = await (
-          await root.get('accounts', {})
-        ).get(env.signed_in_as)
         if (!await account.get_json('administrator'))
           throw new Problem('Unauthorized', 'Not administrator.', 401)
 
-        globals = await root.get_json('globals', {})
+        globals = await (await transaction.get({})).get_json('globals', {})
 
         await transaction.commit()
         return globals
index fbb0127..9d2e304 100644 (file)
@@ -25,19 +25,17 @@ return async env => {
 
       let transaction = await env.site.database.Transaction()
       try {
-        // initialize env.session_key, set cookie in env.response
-        await get_session(env, transaction)
-        if (env.signed_in_as === null)
+        let account = await get_account(
+          env,
+          transaction,
+          await get_session(env, transaction)
+        )
+        if (account === undefined)
           throw new Problem('Unauthorized', 'Please sign in first.', 401)
-
-        let root = await transaction.get({})
-        let account = await (
-          await root.get('accounts', {})
-        ).get(env.signed_in_as)
         if (!await account.get_json('administrator'))
           throw new Problem('Unauthorized', 'Not administrator.', 401)
 
-        root.set_json('globals', globals)
+        ;(await transaction.get({})).set_json('globals', globals)
         await transaction.commit()
       }
       catch (error) {
index d2c8683..a9e0a8f 100644 (file)
@@ -1,8 +1,9 @@
 let XDate = require('xdate')
 
 return async env => {
-  let post_request = await _require('/_lib/post_request.jst')
+  let get_account = await _require('/_lib/get_account.jst')
   let get_session = await _require('/_lib/get_session.jst')
+  let post_request = await _require('/_lib/post_request.jst')
   let Problem = await _require('/_lib/Problem.jst')
 
   await post_request(
@@ -12,19 +13,20 @@ return async env => {
     async () => {
       let transaction = await env.site.database.Transaction()
       try {
-        // initialize env.session_key, set cookie in env.response
-        await get_session(env, transaction)
-        if (env.signed_in_as === null)
+        let account = await get_account(
+          env,
+          transaction,
+          await get_session(env, transaction)
+        )
+        if (account === undefined)
           throw new Problem('Unauthorized', 'Please sign in first.', 401)
 
-        let root = await transaction.get({})
-        let account = await (
-          await root.get('accounts', {})
-        ).get(env.signed_in_as)
         if (!await account.get_json('administrator'))
           throw new Problem('Unauthorized', 'Not administrator.', 401)
 
-        nodemailer = await root.get_json('nodemailer', {})
+        nodemailer = await (
+          await transaction.get({})
+        ).get_json('nodemailer', {})
 
         await transaction.commit()
         return nodemailer
index 0ca4b66..292529b 100644 (file)
@@ -1,8 +1,9 @@
 let XDate = require('xdate')
 
 return async env => {
-  let post_request = await _require('/_lib/post_request.jst')
+  let get_account = await _require('/_lib/get_account.jst')
   let get_session = await _require('/_lib/get_session.jst')
+  let post_request = await _require('/_lib/post_request.jst')
   let Problem = await _require('/_lib/Problem.jst')
 
   await post_request(
@@ -15,19 +16,18 @@ return async env => {
 
       let transaction = await env.site.database.Transaction()
       try {
-        // initialize env.session_key, set cookie in env.response
-        await get_session(env, transaction)
-        if (env.signed_in_as === null)
+        let account = await get_account(
+          env,
+          transaction,
+          await get_session(env, transaction)
+        )
+        if (account === undefined)
           throw new Problem('Unauthorized', 'Please sign in first.', 401)
 
-        let root = await transaction.get({})
-        let account = await (
-          await root.get('accounts', {})
-        ).get(env.signed_in_as)
         if (!await account.get_json('administrator'))
           throw new Problem('Unauthorized', 'Not administrator.', 401)
 
-        root.set_json('nodemailer', nodemailer)
+        ;(await transaction.get({})).set_json('nodemailer', nodemailer)
         await transaction.commit()
       }
       catch (error) {
index 4e88f56..fc7f0d5 100644 (file)
@@ -2,26 +2,22 @@ let XDate = require('xdate')
 
 return async env => {
   let breadcrumbs = await _require('/_lib/breadcrumbs.jst')
+  let get_account = await _require('/_lib/get_account.jst')
+  let get_session = await _require('/_lib/get_session.jst')
   let icon_cross = await env.site.get_min_svg('/_svg/icon_cross.svg')
   let icon_tick = await env.site.get_min_svg('/_svg/icon_tick.svg')
   let menu = await env.site.get_menu('/my_account/_menu.json')
   let navbar = await _require('/_lib/navbar.jst')
-  let get_session = await _require('/_lib/get_session.jst')
 
   // see whether signed in, if so preload details, and draft details if any
-  let
-    transaction = await env.site.database.Transaction(),
-    details,
-    draft_details
+  let transaction = await env.site.database.Transaction()
+  let signed_in_as, details, draft_details
   try {
-    // initialize env.session_key, set cookie in env.response
     let session = await get_session(env, transaction)
-    if (env.signed_in_as) {
-      account = await (
-        await (
-          await transaction.get({})
-        ).get('accounts')
-      ).get(env.signed_in_as)
+    signed_in_as = await session.get_json('signed_in_as', null)
+
+    let account = await get_account(env, transaction, session)
+    if (account !== undefined) {
       details = {
         given_names: await account.get_json('given_names'),
         family_name: await account.get_json('family_name'),
@@ -60,7 +56,7 @@ return async env => {
     async _out => {
       await breadcrumbs(env, _out)
 
-      if (env.signed_in_as) {
+      if (signed_in_as !== null) {
         // signed in
         p {'Your given names are visible to other users if you comment on our blog. Your email and family name remain private. If your name is one word or does not fit given names/family name pattern, then please enter given names only.'}
 
@@ -209,7 +205,7 @@ return async env => {
         }
       }
 
-      if (env.signed_in_as) {
+      if (signed_in_as !== null) {
         //script(src="/js/api_call.js") {}
 
         script {