@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -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 id = context.params.connectionId
34
- const connection = await context.app.service('Connection')._findUnique({ where: { id }})
35
- const data = JSON.parse(connection.data)
36
- const user = data.user
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
  }
@@ -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,