@dwtechs/toker-express 0.5.1 → 0.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  [![License: MIT](https://img.shields.io/npm/l/@dwtechs/toker-express.svg?color=brightgreen)](https://opensource.org/licenses/MIT)
3
3
  [![npm version](https://badge.fury.io/js/%40dwtechs%2Ftoker-express.svg)](https://www.npmjs.com/package/@dwtechs/toker-express)
4
4
  [![last version release date](https://img.shields.io/github/release-date/DWTechs/Toker-express.js)](https://www.npmjs.com/package/@dwtechs/toker-express)
5
- ![Jest:coverage](https://img.shields.io/badge/Jest:coverage-95%25-brightgreen.svg)
5
+ ![Jest:coverage](https://img.shields.io/badge/Jest:coverage-93%25-brightgreen.svg)
6
6
 
7
7
 
8
8
  - [Synopsis](#synopsis)
@@ -47,7 +47,7 @@ $ npm i @dwtechs/toker-express
47
47
  ```javascript
48
48
 
49
49
  // @ts-check
50
- import { refresh as refreshTokens, parseBearerToken, decodeAccess, decodeRefresh } from "@dwtechs/toker-express";
50
+ import * as tk from "@dwtechs/toker-express";
51
51
  import express from "express";
52
52
  const router = express.Router();
53
53
 
@@ -66,18 +66,18 @@ const add = [
66
66
 
67
67
  const refresh = [
68
68
  cEntity.validate,
69
- parseBearerToken,
70
- decodeAccess,
71
- decodeRefresh,
69
+ tk.parseBearerToken,
70
+ tk.decodeAccess,
71
+ tk.decodeRefresh,
72
72
  checkToken,
73
- refresh,
73
+ tk.refresh,
74
74
  cEntity.update,
75
75
  ];
76
76
 
77
77
  const del = [
78
78
  checkToken,
79
- parseBearerToken,
80
- decodeAccess,
79
+ tk.parseBearerToken,
80
+ tk.decodeAccess,
81
81
  cEntity.delete,
82
82
  ];
83
83
 
@@ -244,17 +244,17 @@ if (!iss)
244
244
  iss = res.locals.id ?? null;
245
245
  ```
246
246
 
247
- It will then send both new refresh and access tokens in the res.locals object and optionally in req.body.rows[0] if the rows array exists.
247
+ It will then send both new refresh and access tokens in the res.locals object and in req.body.rows[0].
248
+
249
+ **Note:** If `req.body.rows` doesn't exist or is not an array, it will be automatically created as `[{}]`. If `req.body.rows[0]` is not an object, it will be replaced with `{}`.
248
250
 
249
251
  ```Javascript
250
252
  res.locals.accessToken = accessToken;
251
253
  res.locals.refreshToken = refreshToken;
252
254
 
253
- const rbr = req.body?.rows;
254
- if (isArray(rbr, ">=", 1) && isObject(rbr[0])) {
255
- rbr[0].accessToken = accessToken;
256
- rbr[0].refreshToken = refreshToken;
257
- }
255
+ // req.body.rows is automatically created if needed
256
+ req.body.rows[0].accessToken = accessToken;
257
+ req.body.rows[0].refreshToken = refreshToken;
258
258
  ```
259
259
 
260
260
  ### JWT Decoding
@@ -26,12 +26,6 @@ https://github.com/DWTechs/Toker-express.js
26
26
 
27
27
  import type { Request, Response, NextFunction } from 'express';
28
28
 
29
- export interface RowWithTokens {
30
- accessToken?: string;
31
- refreshToken?: string;
32
- [key: string]: any;
33
- }
34
-
35
29
  declare function refresh(req: Request, res: Response, next: NextFunction): void;
36
30
  declare function parseBearerToken(req: Request, res: Response, next: NextFunction): void;
37
31
  declare function decodeAccess(_req: Request, res: Response, next: NextFunction): void;
@@ -57,11 +57,12 @@ function refresh(req, res, next) {
57
57
  log.debug(`refreshToken='${rt}', accessToken='${at}'`);
58
58
  res.locals.accessToken = at;
59
59
  res.locals.refreshToken = rt;
60
- const rbr = (_d = req.body) === null || _d === void 0 ? void 0 : _d.rows;
61
- if (isArray(rbr, ">=", 1) && isObject(rbr[0])) {
62
- rbr[0].accessToken = at;
63
- rbr[0].refreshToken = rt;
64
- }
60
+ if (!isArray((_d = req.body) === null || _d === void 0 ? void 0 : _d.rows, ">=", 1))
61
+ req.body.rows = [{}];
62
+ else if (!isObject(req.body.rows[0]))
63
+ req.body.rows[0] = {};
64
+ req.body.rows[0].accessToken = at;
65
+ req.body.rows[0].refreshToken = rt;
65
66
  next();
66
67
  }
67
68
  function parseBearerToken(req, res, next) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/toker-express",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Open source JWT management library for Express.js to refresh and decode tokens safely.",
5
5
  "keywords": [
6
6
  "JWT",