@jcbuisson/express-x 1.5.2 → 1.5.3
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 +7 -4
- package/src/context.mjs +33 -0
- package/src/index.mjs +5 -0
package/package.json
CHANGED
package/src/common-hooks.mjs
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
import config from 'config'
|
|
3
3
|
import bcrypt from 'bcryptjs'
|
|
4
4
|
|
|
5
|
+
import { getConnectionDataItem } from './context.mjs'
|
|
6
|
+
|
|
5
7
|
|
|
6
8
|
// hash password of user record
|
|
7
9
|
// name of the password field is found in `config.authentication.local.passwordField` (default: 'password')
|
|
@@ -30,9 +32,10 @@ export function protect(field) {
|
|
|
30
32
|
export async function isAuthenticated(context) {
|
|
31
33
|
if (context.transport !== 'ws') return
|
|
32
34
|
// extract user from connection data
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
35
|
+
const user = await getConnectionDataItem(context, 'user')
|
|
36
|
+
// const id = context.params.connectionId
|
|
37
|
+
// const connection = await context.app.service('Connection')._findUnique({ where: { id }})
|
|
38
|
+
// const data = JSON.parse(connection.data)
|
|
39
|
+
// const user = data.user
|
|
37
40
|
if (!user) throw Error(`AuthCode hook: not authenticated ${id}`)
|
|
38
41
|
}
|
package/src/context.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
export async function getConnectionDataItem(context, key) {
|
|
3
|
+
const id = context.params.connectionId
|
|
4
|
+
const connection = await context.app.service('Connection')._findUnique({ where: { id }})
|
|
5
|
+
const data = JSON.parse(connection.data)
|
|
6
|
+
return data[key]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function setConnectionDataItem(context, key, value) {
|
|
10
|
+
const id = context.params.connectionId
|
|
11
|
+
const connection = await context.app.service('Connection')._findUnique({ where: { id }})
|
|
12
|
+
const data = JSON.parse(connection.data)
|
|
13
|
+
data[key] = value
|
|
14
|
+
await context.app.service('Connection').update({
|
|
15
|
+
where: { id },
|
|
16
|
+
data: {
|
|
17
|
+
data: JSON.stringify(data)
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function removeConnectionDataItem(context, key) {
|
|
23
|
+
const id = context.params.connectionId
|
|
24
|
+
const connection = await context.app.service('Connection')._findUnique({ where: { id }})
|
|
25
|
+
const data = JSON.parse(connection.data)
|
|
26
|
+
delete data[key]
|
|
27
|
+
await context.app.service('Connection').update({
|
|
28
|
+
where: { id },
|
|
29
|
+
data: {
|
|
30
|
+
data: JSON.stringify(data)
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
|
|
2
2
|
import { expressX } from './server.mjs'
|
|
3
3
|
import { hashPassword, protect, isAuthenticated } from './common-hooks.mjs'
|
|
4
|
+
import { getConnectionDataItem, setConnectionDataItem, removeConnectionDataItem } from './context.mjs'
|
|
4
5
|
|
|
5
6
|
export {
|
|
6
7
|
expressX,
|
|
7
8
|
|
|
9
|
+
getConnectionDataItem,
|
|
10
|
+
setConnectionDataItem,
|
|
11
|
+
removeConnectionDataItem,
|
|
12
|
+
|
|
8
13
|
hashPassword,
|
|
9
14
|
protect,
|
|
10
15
|
isAuthenticated,
|