Make get_session() readonly and throw an exception if session cannot be found rather...
[ndcode_site.git] / _lib / get_session.jst
index 06fec33..d72d2ab 100644 (file)
@@ -1,15 +1,15 @@
-let XDate = require('xdate')
+let jst_server = (await import('@ndcode/jst_server')).default
 
 return async (env, root) => {
-  let sessions = await root.get('sessions', {})
+  let sessions = await root.get('sessions')
   let session = await sessions.get(env.session_key)
-  if (session === undefined) {
+  if (session === undefined || env.now >= await session.get_json('expires'))
     // this should never happen, but could happen if we take more than a day
     // to process an incoming request, and database is cleaned in the meantime
-    let expires = new XDate(env.now)
-    expires.addDays(1)
-    session = Transaction.json_to_logjson({expires: expires.getTime()})
-    sessions.set(env.session_key, session)
-  }
+    throw new jst_server.Problem(
+      'Session expired',
+      'We took too long to process an incoming request, please try again.'
+      506
+    )
   return session
 }