@maximemrf/adonisjs-jwt 0.3.0 → 0.4.0

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
@@ -55,6 +55,8 @@ const authConfig = defineConfig({
55
55
  }),
56
56
  // add the jwt guard
57
57
  jwt: jwtGuard({
58
+ // tokenName is the name of the token passed as cookie, it can be optional, by default it is 'token'
59
+ tokenName: 'custom-name',
58
60
  // tokenExpiresIn can be a string or a number, it can be optional
59
61
  tokenExpiresIn: '1h',
60
62
  // if you want to use cookies for the authentication instead of the bearer token (optional)
@@ -74,6 +76,12 @@ const authConfig = defineConfig({
74
76
  })
75
77
  ```
76
78
 
79
+ `tokenName` is the name of the token passed as a cookie, it can be optional, by default it is `token`.
80
+
81
+ ```typescript
82
+ tokenName: 'custom-name'
83
+ ```
84
+
77
85
  `tokenExpiresIn` is the time before the token expires it can be a string or a number and it can be optional.
78
86
 
79
87
  ```typescript
@@ -5,6 +5,7 @@ import { JwtGuard } from './jwt.js';
5
5
  import type { StringValue } from 'ms';
6
6
  export declare function jwtGuard<UserProvider extends JwtUserProviderContract<unknown>>(config: {
7
7
  provider: UserProvider;
8
+ tokenName?: string;
8
9
  tokenExpiresIn?: number | StringValue;
9
10
  useCookies?: boolean;
10
11
  content: <T>(user: JwtGuardUser<T>) => Record<string | number, any>;
@@ -5,6 +5,7 @@ export function jwtGuard(config) {
5
5
  const appKey = app.config.get('app.appKey').release();
6
6
  const options = {
7
7
  secret: appKey,
8
+ tokenName: config.tokenName,
8
9
  expiresIn: config.tokenExpiresIn,
9
10
  useCookies: config.useCookies,
10
11
  content: config.content,
package/build/src/jwt.js CHANGED
@@ -4,12 +4,14 @@ export class JwtGuard {
4
4
  #ctx;
5
5
  #userProvider;
6
6
  #options;
7
+ #tokenName;
7
8
  constructor(ctx, userProvider, option) {
8
9
  this.#ctx = ctx;
9
10
  this.#userProvider = userProvider;
10
11
  this.#options = option;
11
12
  if (!this.#options.content)
12
13
  this.#options.content = (user) => ({ userId: user.getId() });
14
+ this.#tokenName = this.#options.tokenName ?? 'token';
13
15
  }
14
16
  /**
15
17
  * A unique name for the guard driver
@@ -40,7 +42,7 @@ export class JwtGuard {
40
42
  }
41
43
  : {});
42
44
  if (this.#options.useCookies) {
43
- return this.#ctx.response.cookie('token', token, {
45
+ return this.#ctx.response.cookie(`${this.#tokenName}`, token, {
44
46
  httpOnly: true,
45
47
  });
46
48
  }
@@ -70,9 +72,10 @@ export class JwtGuard {
70
72
  * If cookies are enabled, then read the token from the cookies
71
73
  */
72
74
  if (cookieHeader) {
75
+ const regex = new RegExp(`${this.#tokenName}=([^;]*)`);
73
76
  token =
74
- this.#ctx.request.cookie('token') ??
75
- (this.#ctx.request.request.headers.cookie.match(/token=(.*?)(;|$)/) || [])[1];
77
+ this.#ctx.request.cookie(`${this.#tokenName}`) ??
78
+ (this.#ctx.request.request.headers.cookie.match(regex) || [])[1];
76
79
  }
77
80
  /**
78
81
  * If token is missing on cookies, then try to read it from the header authorization
@@ -39,6 +39,7 @@ export type BaseJwtContent = {
39
39
  };
40
40
  export type JwtGuardOptions<RealUser extends any = unknown> = {
41
41
  secret: string;
42
+ tokenName?: string;
42
43
  expiresIn?: number | StringValue;
43
44
  useCookies?: boolean;
44
45
  content?: (user: JwtGuardUser<RealUser>) => Record<string, any>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@maximemrf/adonisjs-jwt",
3
3
  "description": "Adonisjs v6 package for authentication with jwt token",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "engines": {
6
6
  "node": ">=20.6.0"
7
7
  },