@jcbuisson/express-x 1.5.0 → 1.5.2

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.0",
3
+ "version": "1.5.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -12,23 +12,6 @@ export async function hashPassword(context) {
12
12
  }
13
13
 
14
14
 
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
15
  // remove `field` from `result`
33
16
  export function protect(field) {
34
17
  return async (context) => {
@@ -44,10 +27,12 @@ export function protect(field) {
44
27
  }
45
28
 
46
29
 
47
- export async function setSessionJWT(context) {
48
- if (context.transport === 'ws') {
49
- // associate JWT token to WS connection to mark it as secure
50
- context.connection.accessToken = context.result.accessToken
51
- }
52
- return context
30
+ export async function isAuthenticated(context) {
31
+ if (context.transport !== 'ws') return
32
+ // 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
37
+ if (!user) throw Error(`AuthCode hook: not authenticated ${id}`)
53
38
  }
package/src/index.mjs CHANGED
@@ -1,10 +1,11 @@
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
4
 
5
5
  export {
6
6
  expressX,
7
7
 
8
8
  hashPassword,
9
9
  protect,
10
+ isAuthenticated,
10
11
  }