@maximemrf/adonisjs-jwt 0.0.18 → 0.0.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.
- package/README.md +11 -1
- package/build/src/jwt.js +4 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,8 @@ const authConfig = defineConfig({
|
|
|
41
41
|
jwt: jwtGuard({
|
|
42
42
|
// tokenExpiresIn can be a string or a number, it can be optional
|
|
43
43
|
tokenExpiresIn: '1h',
|
|
44
|
+
// if you want to use cookies for the authentication instead of the bearer token (optional)
|
|
45
|
+
useCookies: true,
|
|
44
46
|
provider: sessionUserProvider({
|
|
45
47
|
model: () => import('#models/user'),
|
|
46
48
|
}),
|
|
@@ -49,7 +51,7 @@ const authConfig = defineConfig({
|
|
|
49
51
|
})
|
|
50
52
|
```
|
|
51
53
|
|
|
52
|
-
`tokenExpiresIn` is the time before the token expires it can be a string or a number
|
|
54
|
+
`tokenExpiresIn` is the time before the token expires it can be a string or a number and it can be optional.
|
|
53
55
|
|
|
54
56
|
```typescript
|
|
55
57
|
// string
|
|
@@ -58,6 +60,14 @@ tokenExpiresIn: '1h'
|
|
|
58
60
|
tokenExpiresIn: 60 * 60
|
|
59
61
|
```
|
|
60
62
|
|
|
63
|
+
You can also use cookies for the authentication instead of the bearer token by setting `useCookies` to `true`.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
useCookies: true
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
If you just want to use jwt with the bearer token no need to set `useCookies` to `false` you can just remove it.
|
|
70
|
+
|
|
61
71
|
## Usage
|
|
62
72
|
|
|
63
73
|
To make a protected route, you have to use the `auth` middleware with the `jwt` guard.
|
package/build/src/jwt.js
CHANGED
|
@@ -64,14 +64,13 @@ export class JwtGuard {
|
|
|
64
64
|
this.authenticationAttempted = true;
|
|
65
65
|
const cookieHeader = this.#ctx.request.request.headers.cookie;
|
|
66
66
|
let token;
|
|
67
|
-
console.log('cookieHeader', cookieHeader);
|
|
68
67
|
/**
|
|
69
68
|
* If cookies are enabled, then read the token from the cookies
|
|
70
69
|
*/
|
|
71
70
|
if (cookieHeader) {
|
|
72
|
-
token =
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
token = this.#ctx.request.cookie('token')
|
|
72
|
+
? this.#ctx.request.cookie('token')
|
|
73
|
+
: (this.#ctx.request.request.headers.cookie.match(/token=(.*?)(;|$)/) || [])[1];
|
|
75
74
|
}
|
|
76
75
|
/**
|
|
77
76
|
* If token is missing on cookies, then try to read it from the header authorization
|
|
@@ -91,7 +90,6 @@ export class JwtGuard {
|
|
|
91
90
|
*/
|
|
92
91
|
;
|
|
93
92
|
[, token] = authHeader.split('Bearer ');
|
|
94
|
-
console.log('token', token);
|
|
95
93
|
if (!token) {
|
|
96
94
|
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
|
|
97
95
|
guardDriverName: this.driverName,
|
|
@@ -157,18 +155,9 @@ export class JwtGuard {
|
|
|
157
155
|
*/
|
|
158
156
|
async authenticateAsClient(user) {
|
|
159
157
|
const token = await this.generate(user);
|
|
160
|
-
// if (this.#options.useCookies) {
|
|
161
|
-
// console.log('useCookies')
|
|
162
|
-
// console.log(token.ctx.response.cookie())
|
|
163
|
-
// return {
|
|
164
|
-
// headers: {
|
|
165
|
-
// 'set-cookie': `token=${token}`,
|
|
166
|
-
// },
|
|
167
|
-
// }
|
|
168
|
-
// }
|
|
169
158
|
return {
|
|
170
159
|
headers: {
|
|
171
|
-
authorization: `Bearer ${token.token}`,
|
|
160
|
+
authorization: `Bearer ${this.#options.useCookies ? token : token.token}`,
|
|
172
161
|
},
|
|
173
162
|
};
|
|
174
163
|
}
|