@maximemrf/adonisjs-jwt 0.3.0 → 0.5.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 +14 -1
- package/build/src/define_config.d.ts +2 -0
- package/build/src/define_config.js +2 -1
- package/build/src/jwt.js +6 -3
- package/build/src/types.d.ts +1 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'
|
|
|
38
38
|
import { jwtGuard } from '@maximemrf/adonisjs-jwt/jwt_config'
|
|
39
39
|
import { JwtGuardUser, BaseJwtContent } from '@maximemrf/adonisjs-jwt/types'
|
|
40
40
|
import User from '#models/user'
|
|
41
|
+
import env from '#start/env'
|
|
41
42
|
|
|
42
43
|
interface JwtContent extends BaseJwtContent {
|
|
43
44
|
email: string
|
|
@@ -55,10 +56,16 @@ const authConfig = defineConfig({
|
|
|
55
56
|
}),
|
|
56
57
|
// add the jwt guard
|
|
57
58
|
jwt: jwtGuard({
|
|
59
|
+
// tokenName is the name of the token passed as cookie, it can be optional, by default it is 'token'
|
|
60
|
+
tokenName: 'custom-name',
|
|
58
61
|
// tokenExpiresIn can be a string or a number, it can be optional
|
|
59
62
|
tokenExpiresIn: '1h',
|
|
60
63
|
// if you want to use cookies for the authentication instead of the bearer token (optional)
|
|
61
64
|
useCookies: true,
|
|
65
|
+
// secret is the secret used to sign the token, it can be optional, by default it uses the application key
|
|
66
|
+
// you can use a env variable like JWT_SECRET or set it directly with a string
|
|
67
|
+
// if you don't have specific needs, please discard this option
|
|
68
|
+
secret: env.get('JWT_SECRET'),
|
|
62
69
|
provider: sessionUserProvider({
|
|
63
70
|
model: () => import('#models/user'),
|
|
64
71
|
}),
|
|
@@ -74,6 +81,12 @@ const authConfig = defineConfig({
|
|
|
74
81
|
})
|
|
75
82
|
```
|
|
76
83
|
|
|
84
|
+
`tokenName` is the name of the token passed as a cookie, it can be optional, by default it is `token`.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
tokenName: 'custom-name'
|
|
88
|
+
```
|
|
89
|
+
|
|
77
90
|
`tokenExpiresIn` is the time before the token expires it can be a string or a number and it can be optional.
|
|
78
91
|
|
|
79
92
|
```typescript
|
|
@@ -91,7 +104,7 @@ useCookies: true
|
|
|
91
104
|
|
|
92
105
|
If you just want to use jwt with the bearer token no need to set `useCookies` to `false` you can just remove it.
|
|
93
106
|
|
|
94
|
-
##
|
|
107
|
+
## Authentication
|
|
95
108
|
|
|
96
109
|
To make a protected route, you have to use the `auth` middleware with the `jwt` guard.
|
|
97
110
|
|
|
@@ -5,7 +5,9 @@ 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;
|
|
11
|
+
secret?: string;
|
|
10
12
|
content: <T>(user: JwtGuardUser<T>) => Record<string | number, any>;
|
|
11
13
|
}): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider>>;
|
|
@@ -4,7 +4,8 @@ export function jwtGuard(config) {
|
|
|
4
4
|
async resolver(_, app) {
|
|
5
5
|
const appKey = app.config.get('app.appKey').release();
|
|
6
6
|
const options = {
|
|
7
|
-
secret: appKey,
|
|
7
|
+
secret: config.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(
|
|
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>;
|
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.
|
|
4
|
+
"version": "0.5.0",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.6.0"
|
|
7
7
|
},
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"typecheck": "tsc --noEmit",
|
|
24
24
|
"lint": "eslint .",
|
|
25
25
|
"format": "prettier --write .",
|
|
26
|
-
"quick:test": "node --import
|
|
26
|
+
"quick:test": "node --import=ts-node-maintained/register/esm --enable-source-maps --experimental-import-meta-resolve bin/test.ts --force-exit",
|
|
27
27
|
"pretest": "npm run lint",
|
|
28
28
|
"test": "c8 npm run quick:test",
|
|
29
29
|
"prebuild": "npm run lint && npm run clean",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"np": "^10.0.0",
|
|
54
54
|
"prettier": "^3.5.3",
|
|
55
55
|
"timekeeper": "^2.3.1",
|
|
56
|
-
"ts-node": "^10.9.
|
|
56
|
+
"ts-node-maintained": "^10.9.5",
|
|
57
57
|
"typescript": "~5.8"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|