@jcbuisson/express-x 2.1.19 → 2.1.20

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +8 -74
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "2.1.19",
3
+ "version": "2.1.20",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -305,14 +305,13 @@ export class EXError extends Error {
305
305
  }
306
306
 
307
307
 
308
-
308
+ ////////////////////////// HOOK METHODS //////////////////////////
309
309
 
310
310
  /*
311
311
  * Add a timestamp property of name `field` with current time as value
312
312
  */
313
313
  export const addTimestamp = (field) => async (context) => {
314
314
  context.result[field] = (new Date()).toISOString()
315
- return context
316
315
  }
317
316
 
318
317
  /*
@@ -321,84 +320,19 @@ export const addTimestamp = (field) => async (context) => {
321
320
  export const hashPassword = (passwordField) => async (context) => {
322
321
  const user = context.result
323
322
  user[passwordField] = await bcrypt.hash(user[passwordField], 5)
324
- return context
325
323
  }
326
324
 
327
325
  /*
328
326
  * Remove `field` from `context.result`
329
327
  */
330
- export function protect(field) {
331
- return async (context) => {
332
- if (context.result) {
333
- if (Array.isArray(context.result)) {
334
- for (const value of context.result) {
335
- delete value[field]
336
- }
337
- } else if (typeof context.result === "object") {
338
- delete context.result[field]
328
+ export const protect = (field) => (context) => {
329
+ if (context.result) {
330
+ if (Array.isArray(context.result)) {
331
+ for (const value of context.result) {
332
+ delete value[field]
339
333
  }
334
+ } else if (typeof context.result === "object") {
335
+ delete context.result[field]
340
336
  }
341
- return context
342
- }
343
- }
344
-
345
- export const isNotExpired = async (context) => {
346
- // do nothing if it's not a client call from a ws connexion
347
- if (!context.socket) return
348
- const expiresAt = context.socket?.data?.expiresAt
349
- if (expiresAt) {
350
- const expiresAtDate = new Date(expiresAt)
351
- const now = new Date()
352
- if (now > expiresAtDate) {
353
- // expiration date is met
354
- // clear socket.data
355
- context.socket.data = {}
356
- // leave all rooms except socket#id
357
- const rooms = new Set(context.socket.rooms)
358
- for (const room of rooms) {
359
- if (room === context.socket.id) continue
360
- context.socket.leave(room)
361
- }
362
- // send an event to the client (typical client handling: logout)
363
- context.socket.emit('not-authenticated')
364
- // throw exception
365
- throw new EXError('not-authenticated', "Session expired")
366
- }
367
- } else {
368
- // send an event to the client (typical client handling: logout)
369
- context.socket.emit('not-authenticated')
370
- throw new EXError('not-authenticated', "No expiresAt in socket.data")
371
- }
372
- }
373
-
374
- /*
375
- * Throw an error for a client service method call when socket.data does not contain user
376
- */
377
- export const isAuthenticated = async (context) => {
378
- // do nothing if it's not a client call from a ws connexion
379
- if (context.caller !== 'client') return
380
- if (!context.socket?.data) {
381
- // send an event to the client (typical client handling: logout)
382
- context.socket.emit('not-authenticated')
383
- throw new EXError('not-authenticated', 'no data in socket')
384
- }
385
- if (!context.socket.data?.user) {
386
- // send an event to the client (typical client handling: logout)
387
- context.socket.emit('not-authenticated')
388
- throw new EXError('not-authenticated', 'no user in socket.data')
389
- }
390
- }
391
-
392
- /*
393
- * Extend value of socket.data.expiresAt of `duration` milliseconds
394
- */
395
- export const extendExpiration = (duration) => async (context) => {
396
- const now = new Date()
397
- if (context.caller !== 'client') return
398
- if (!context.socket?.data) {
399
- // send an event to the client (typical client handling: logout)
400
- context.socket.emit('not-authenticated')
401
- throw new EXError('not-authenticated', 'no data in socket')
402
337
  }
403
- context.socket.data.expiresAt = new Date(now.getTime() + duration)
404
338
  }