@jcbuisson/express-x 1.5.1 → 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.1",
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')
@@ -12,23 +14,6 @@ export async function hashPassword(context) {
12
14
  }
13
15
 
14
16
 
15
- export async function authenticate(context) {
16
- console.log('authenticate hook', context.transport, context.name, context.action, context?.connection?.accessToken)
17
- return context
18
-
19
- if (context.transport === 'ws') {
20
- // for WS transport, just check that connection.accessToken is set and valid
21
- if (!context?.connection?.accessToken) throw new Error("WS connection is not secured by a JWT access token")
22
- // TODO: check token validity
23
-
24
- } else if (context.transport === 'http') {
25
- // for HTTP transport, check that the associated request has a header with a valid JWT
26
- // TODO: check headers for access token
27
- }
28
- return (context)
29
- }
30
-
31
-
32
17
  // remove `field` from `result`
33
18
  export function protect(field) {
34
19
  return async (context) => {
@@ -47,9 +32,10 @@ export function protect(field) {
47
32
  export async function isAuthenticated(context) {
48
33
  if (context.transport !== 'ws') return
49
34
  // extract user from connection data
50
- const id = context.params.connectionId
51
- const connection = await context.app.service('Connection')._findUnique({ where: { id }})
52
- const data = JSON.parse(connection.data)
53
- 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
54
40
  if (!user) throw Error(`AuthCode hook: not authenticated ${id}`)
55
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
- import { hashPassword, protect, } from './common-hooks.mjs'
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,