@jcbuisson/express-x 1.6.13 → 1.6.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.6.13",
3
+ "version": "1.6.15",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -2,6 +2,7 @@
2
2
  import bcrypt from 'bcryptjs'
3
3
 
4
4
  import { getConnectionDataItem } from './context.mjs'
5
+ import { MyCustomError } from './server.mjs'
5
6
 
6
7
 
7
8
  // hash password of user record
@@ -25,21 +26,24 @@ export function protect(field) {
25
26
  }
26
27
  }
27
28
 
29
+
28
30
  export async function isAuthenticated(context) {
29
- if (context.transport !== 'ws') return
30
31
  // extract sessionId from connection data
31
32
  const sessionId = await getConnectionDataItem(context, 'sessionId')
32
- if (!sessionId) throw Error(`Not authenticated (no sessionId in connection data)`)
33
+ if (!sessionId) throw new MyCustomError("not authenticated", 'not-authenticated')
33
34
  }
34
35
 
35
36
  export const isNotExpired = async (context) => {
36
- if (context.transport !== 'ws') return
37
- const expireAtISO = await getConnectionDataItem(context, 'expireAt')
38
- const expireAt = new Date(expireAtISO)
39
- const now = new Date()
40
- if (now > expireAt) {
41
- // DON'T CLEAR CONNECTION DATA, LOGOUT WILL NOT BE ABLE TO RETRIEVE SESSIONID
42
- // throw exception
43
- throw new Error('session expired')
37
+ const expireAt = await getConnectionDataItem(context, 'expireAt')
38
+ if (expireAt) {
39
+ const expireAtDate = new Date(expireAt)
40
+ const now = new Date()
41
+ if (now > expireAtDate) {
42
+ // expiration date is met: clear connection data & throw exception
43
+ await resetConnection(context)
44
+ throw new MyCustomError("session expired", 'session-expired')
45
+ }
46
+ } else {
47
+ throw new MyCustomError("session expired", 'session-expired')
44
48
  }
45
49
  }