Make get_session() readonly and throw an exception if session cannot be found rather...
[ndcode_site.git] / _lib / get_account.jst
index e80b85e..f5af367 100644 (file)
@@ -1,7 +1,20 @@
+let jst_server = (await import('@ndcode/jst_server')).default
+
 return async (root, session) => {
   let signed_in_as = await session.get_json('signed_in_as')
   if (signed_in_as === undefined)
-    return undefined
-  let accounts = await root.get('accounts', {})
-  return await accounts.get(signed_in_as, {})
+    throw new jst_server.Problem(
+      'Unauthorized',
+      'Please sign in first.',
+      401
+    )
+  let accounts = await root.get('accounts')
+  let account = accounts.get(signed_in_as)
+  if (account === undefined)
+    throw new jst_server.Problem(
+      'Account expired',
+      'Database is inconsistent, please sign out and recreate your account.'
+      507
+    )
+  return account
 }