@maximemrf/adonisjs-jwt 0.0.19 → 0.1.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 +11 -1
- package/build/src/jwt.js +1 -14
- 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,7 +64,6 @@ 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
|
*/
|
|
@@ -72,8 +71,6 @@ export class JwtGuard {
|
|
|
72
71
|
token = this.#ctx.request.cookie('token')
|
|
73
72
|
? this.#ctx.request.cookie('token')
|
|
74
73
|
: (this.#ctx.request.request.headers.cookie.match(/token=(.*?)(;|$)/) || [])[1];
|
|
75
|
-
console.log('cookietoken|', token);
|
|
76
|
-
//;[, token] = cookieHeader!.split('=')
|
|
77
74
|
}
|
|
78
75
|
/**
|
|
79
76
|
* If token is missing on cookies, then try to read it from the header authorization
|
|
@@ -93,7 +90,6 @@ export class JwtGuard {
|
|
|
93
90
|
*/
|
|
94
91
|
;
|
|
95
92
|
[, token] = authHeader.split('Bearer ');
|
|
96
|
-
console.log('token', token);
|
|
97
93
|
if (!token) {
|
|
98
94
|
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
|
|
99
95
|
guardDriverName: this.driverName,
|
|
@@ -159,18 +155,9 @@ export class JwtGuard {
|
|
|
159
155
|
*/
|
|
160
156
|
async authenticateAsClient(user) {
|
|
161
157
|
const token = await this.generate(user);
|
|
162
|
-
// if (this.#options.useCookies) {
|
|
163
|
-
// console.log('useCookies')
|
|
164
|
-
// console.log(token.ctx.response.cookie())
|
|
165
|
-
// return {
|
|
166
|
-
// headers: {
|
|
167
|
-
// 'set-cookie': `token=${token}`,
|
|
168
|
-
// },
|
|
169
|
-
// }
|
|
170
|
-
// }
|
|
171
158
|
return {
|
|
172
159
|
headers: {
|
|
173
|
-
authorization: `Bearer ${token.token}`,
|
|
160
|
+
authorization: `Bearer ${this.#options.useCookies ? token : token.token}`,
|
|
174
161
|
},
|
|
175
162
|
};
|
|
176
163
|
}
|