@maximemrf/adonisjs-jwt 0.0.14 → 0.0.16

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.
@@ -4,4 +4,5 @@ import { JwtGuard, JwtUserProviderContract } from './jwt.js';
4
4
  export declare function jwtGuard<UserProvider extends JwtUserProviderContract<unknown>>(config: {
5
5
  provider: UserProvider;
6
6
  tokenExpiresIn: number | string;
7
+ useCookies?: boolean;
7
8
  }): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider>>;
@@ -3,7 +3,11 @@ export function jwtGuard(config) {
3
3
  return {
4
4
  async resolver(_, app) {
5
5
  const appKey = app.config.get('app.appKey').release();
6
- const options = { secret: appKey, expiresIn: config.tokenExpiresIn };
6
+ const options = {
7
+ secret: appKey,
8
+ expiresIn: config.tokenExpiresIn,
9
+ useCookies: config.useCookies,
10
+ };
7
11
  return (ctx) => new JwtGuard(ctx, config.provider, options);
8
12
  },
9
13
  };
@@ -38,6 +38,7 @@ export interface JwtUserProviderContract<RealUser> {
38
38
  export type JwtGuardOptions = {
39
39
  secret: string;
40
40
  expiresIn?: number | string;
41
+ useCookies?: boolean;
41
42
  };
42
43
  export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>> implements GuardContract<UserProvider[typeof symbols.PROVIDER_REAL_USER]> {
43
44
  #private;
@@ -68,7 +69,7 @@ export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unkno
68
69
  /**
69
70
  * Generate a JWT token for a given user.
70
71
  */
71
- generate(user: UserProvider[typeof symbols.PROVIDER_REAL_USER]): Promise<{
72
+ generate(user: UserProvider[typeof symbols.PROVIDER_REAL_USER]): Promise<import("@adonisjs/core/http").Response | {
72
73
  type: string;
73
74
  token: string;
74
75
  expiresIn: string | number | undefined;
package/build/src/jwt.js CHANGED
@@ -37,6 +37,11 @@ export class JwtGuard {
37
37
  expiresIn: this.#options.expiresIn,
38
38
  }
39
39
  : {});
40
+ if (this.#options.useCookies) {
41
+ return this.#ctx.response.cookie('token', token, {
42
+ httpOnly: true,
43
+ });
44
+ }
40
45
  return {
41
46
  type: 'bearer',
42
47
  token: token,
@@ -57,23 +62,39 @@ export class JwtGuard {
57
62
  return this.getUserOrFail();
58
63
  }
59
64
  this.authenticationAttempted = true;
65
+ const cookieHeader = this.#ctx.request.request.headers.cookie;
66
+ let token;
60
67
  /**
61
- * Ensure the auth header exists
68
+ * If cookies are enabled, then read the token from the cookies
62
69
  */
63
- const authHeader = this.#ctx.request.header('authorization');
64
- if (!authHeader) {
65
- throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
66
- guardDriverName: this.driverName,
67
- });
70
+ if (cookieHeader) {
71
+ ;
72
+ [, token] = cookieHeader.split('=');
68
73
  }
69
74
  /**
70
- * Split the header value and read the token from it
75
+ * If token is missing on cookies, then try to read it from the header authorization
71
76
  */
72
- const [, token] = authHeader.split('Bearer ');
73
77
  if (!token) {
74
- throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
75
- guardDriverName: this.driverName,
76
- });
78
+ /**
79
+ * Ensure the auth header exists
80
+ */
81
+ const authHeader = this.#ctx.request.header('authorization');
82
+ if (!authHeader) {
83
+ throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
84
+ guardDriverName: this.driverName,
85
+ });
86
+ }
87
+ /**
88
+ * Split the header value and read the token from it
89
+ */
90
+ ;
91
+ [, token] = authHeader.split('Bearer ');
92
+ console.log('token', token);
93
+ if (!token) {
94
+ throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
95
+ guardDriverName: this.driverName,
96
+ });
97
+ }
77
98
  }
78
99
  /**
79
100
  * Verify token
@@ -134,6 +155,15 @@ export class JwtGuard {
134
155
  */
135
156
  async authenticateAsClient(user) {
136
157
  const token = await this.generate(user);
158
+ // if (this.#options.useCookies) {
159
+ // console.log('useCookies')
160
+ // console.log(token.ctx.response.cookie())
161
+ // return {
162
+ // headers: {
163
+ // 'set-cookie': `token=${token}`,
164
+ // },
165
+ // }
166
+ // }
137
167
  return {
138
168
  headers: {
139
169
  authorization: `Bearer ${token.token}`,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@maximemrf/adonisjs-jwt",
3
3
  "description": "",
4
- "version": "0.0.14",
4
+ "version": "0.0.16",
5
5
  "engines": {
6
6
  "node": ">=20.6.0"
7
7
  },