@jcbuisson/express-x 1.7.3 → 1.7.4
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/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
|
}
|