@jcbuisson/express-x 2.1.18 → 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 +4 -68
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "2.1.18",
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,82 +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) => {
328
+ export const protect = (field) => (context) => {
329
+ if (context.result) {
332
330
  if (Array.isArray(context.result)) {
333
331
  for (const value of context.result) {
334
332
  delete value[field]
335
333
  }
336
- } else {
334
+ } else if (typeof context.result === "object") {
337
335
  delete context.result[field]
338
336
  }
339
- return (context)
340
- }
341
- }
342
-
343
- export const isNotExpired = async (context) => {
344
- // do nothing if it's not a client call from a ws connexion
345
- if (!context.socket) return
346
- const expiresAt = context.socket?.data?.expiresAt
347
- if (expiresAt) {
348
- const expiresAtDate = new Date(expiresAt)
349
- const now = new Date()
350
- if (now > expiresAtDate) {
351
- // expiration date is met
352
- // clear socket.data
353
- context.socket.data = {}
354
- // leave all rooms except socket#id
355
- const rooms = new Set(context.socket.rooms)
356
- for (const room of rooms) {
357
- if (room === context.socket.id) continue
358
- context.socket.leave(room)
359
- }
360
- // send an event to the client (typical client handling: logout)
361
- context.socket.emit('not-authenticated')
362
- // throw exception
363
- throw new EXError('not-authenticated', "Session expired")
364
- }
365
- } else {
366
- // send an event to the client (typical client handling: logout)
367
- context.socket.emit('not-authenticated')
368
- throw new EXError('not-authenticated', "No expiresAt in socket.data")
369
- }
370
- }
371
-
372
- /*
373
- * Throw an error for a client service method call when socket.data does not contain user
374
- */
375
- export const isAuthenticated = async (context) => {
376
- // do nothing if it's not a client call from a ws connexion
377
- if (context.caller !== 'client') return
378
- if (!context.socket?.data) {
379
- // send an event to the client (typical client handling: logout)
380
- context.socket.emit('not-authenticated')
381
- throw new EXError('not-authenticated', 'no data in socket')
382
- }
383
- if (!context.socket.data?.user) {
384
- // send an event to the client (typical client handling: logout)
385
- context.socket.emit('not-authenticated')
386
- throw new EXError('not-authenticated', 'no user in socket.data')
387
- }
388
- }
389
-
390
- /*
391
- * Extend value of socket.data.expiresAt of `duration` milliseconds
392
- */
393
- export const extendExpiration = (duration) => async (context) => {
394
- const now = new Date()
395
- if (context.caller !== 'client') return
396
- if (!context.socket?.data) {
397
- // send an event to the client (typical client handling: logout)
398
- context.socket.emit('not-authenticated')
399
- throw new EXError('not-authenticated', 'no data in socket')
400
337
  }
401
- context.socket.data.expiresAt = new Date(now.getTime() + duration)
402
338
  }