@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 +8 -0
- package/build/src/define_config.d.ts +1 -0
- package/build/src/define_config.js +1 -0
- package/build/src/jwt.js +6 -3
- package/build/src/types.d.ts +1 -0
- package/package.json +1 -1
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>;
|
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(
|
|
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(
|
|
75
|
-
(this.#ctx.request.request.headers.cookie.match(
|
|
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
|
package/build/src/types.d.ts
CHANGED
|
@@ -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>;
|