@jcbuisson/express-x 1.7.3 → 1.7.5
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 +1 -1
- package/src/common-hooks.mjs +24 -0
- package/src/index.mjs +3 -1
- package/src/server.mjs +4 -3
package/package.json
CHANGED
package/src/common-hooks.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
import bcrypt from 'bcryptjs'
|
|
3
3
|
|
|
4
|
+
import { getConnectionDataItem } from './context.mjs'
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
// hash password of user record
|
|
6
8
|
export const hashPassword = (passwordField) => async (context) => {
|
|
@@ -22,3 +24,25 @@ export function protect(field) {
|
|
|
22
24
|
return (context)
|
|
23
25
|
}
|
|
24
26
|
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
export async function isAuthenticated(context) {
|
|
30
|
+
// extract sessionId from connection data
|
|
31
|
+
const sessionId = await getConnectionDataItem(context, 'sessionId')
|
|
32
|
+
if (!sessionId) throw new Error('not-authenticated')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const isNotExpired = async (context) => {
|
|
36
|
+
const expireAt = await getConnectionDataItem(context, 'expireAt')
|
|
37
|
+
if (expireAt) {
|
|
38
|
+
const expireAtDate = new Date(expireAt)
|
|
39
|
+
const now = new Date()
|
|
40
|
+
if (now > expireAtDate) {
|
|
41
|
+
// expiration date is met: clear connection data & throw exception
|
|
42
|
+
await resetConnection(context)
|
|
43
|
+
throw new Error('session-expired')
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error('session-expired')
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
import { expressX } from './server.mjs'
|
|
3
|
-
import { hashPassword, protect } from './common-hooks.mjs'
|
|
3
|
+
import { hashPassword, protect, isAuthenticated, isNotExpired } from './common-hooks.mjs'
|
|
4
4
|
import { getContextConnection, resetConnection, getConnectionDataItem, setConnectionDataItem, removeConnectionDataItem, sendServiceEventToClient } from './context.mjs'
|
|
5
5
|
|
|
6
6
|
export {
|
|
@@ -17,4 +17,6 @@ export {
|
|
|
17
17
|
|
|
18
18
|
hashPassword,
|
|
19
19
|
protect,
|
|
20
|
+
isAuthenticated,
|
|
21
|
+
isNotExpired,
|
|
20
22
|
}
|
package/src/server.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import http from 'http'
|
|
3
3
|
import { Server } from "socket.io"
|
|
4
4
|
import express from 'express'
|
|
5
|
+
import config from 'config'
|
|
5
6
|
|
|
6
7
|
/*
|
|
7
8
|
* Enhance `app` express application with services and real-time features
|
|
@@ -45,7 +46,7 @@ export function expressX(prisma, options = {}) {
|
|
|
45
46
|
try {
|
|
46
47
|
await prisma.Connection.delete({ where: { id }})
|
|
47
48
|
} catch(err) {
|
|
48
|
-
//
|
|
49
|
+
// in case it would no longer exist
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
|
|
@@ -334,7 +335,7 @@ export function expressX(prisma, options = {}) {
|
|
|
334
335
|
socket.on('disconnect', () => {
|
|
335
336
|
app.log('verbose', `Client disconnected ${connection.id}`)
|
|
336
337
|
|
|
337
|
-
// remove connection record after
|
|
338
|
+
// remove connection record after expiration delay, if it still exists
|
|
338
339
|
setTimeout(async () => {
|
|
339
340
|
const connectionId = connection.id
|
|
340
341
|
// check if connection still exists
|
|
@@ -343,7 +344,7 @@ export function expressX(prisma, options = {}) {
|
|
|
343
344
|
app.log('verbose', `Delete connection ${connectionId}`)
|
|
344
345
|
await deleteConnection(connectionId)
|
|
345
346
|
}
|
|
346
|
-
},
|
|
347
|
+
}, config.SESSION_EXPIRE_DELAY || 24*60*60000)
|
|
347
348
|
})
|
|
348
349
|
|
|
349
350
|
|