@maximemrf/adonisjs-jwt 0.0.20 → 0.2.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 CHANGED
@@ -26,6 +26,7 @@ import { defineConfig } from '@adonisjs/auth'
26
26
  import { InferAuthEvents, Authenticators } from '@adonisjs/auth/types'
27
27
  import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'
28
28
  import { jwtGuard } from '@maximemrf/adonisjs-jwt/jwt_config'
29
+ import { JwtGuardUser } from '@maximemrf/adonisjs-jwt/types'
29
30
 
30
31
  const authConfig = defineConfig({
31
32
  // define the default authenticator to jwt
@@ -46,6 +47,11 @@ const authConfig = defineConfig({
46
47
  provider: sessionUserProvider({
47
48
  model: () => import('#models/user'),
48
49
  }),
50
+ // content is a function that takes the user and returns the content of the token, it can be optional, by default it returns only the user id
51
+ content: (user: JwtGuardUser<User>) => ({
52
+ id: user.getId(),
53
+ email: user.getOriginal().email,
54
+ }),
49
55
  }),
50
56
  },
51
57
  })
@@ -93,3 +99,7 @@ router.get('/', async ({ auth }) => {
93
99
  })
94
100
  .use(middleware.auth({ guards: ['jwt'] }))
95
101
  ```
102
+
103
+ ## Security
104
+
105
+ We use natively the AdonisJS application key to sign the token, so you don't have to worry about it and [avoid this](https://trufflesecurity.com/blog/stop-recommending-jwts).
@@ -1,8 +1,9 @@
1
1
  import { GuardConfigProvider } from '@adonisjs/auth/types';
2
2
  import type { HttpContext } from '@adonisjs/core/http';
3
- import { JwtGuard, JwtUserProviderContract } from './jwt.js';
3
+ import { JwtGuard, JwtGuardUser, JwtUserProviderContract } from './jwt.js';
4
4
  export declare function jwtGuard<UserProvider extends JwtUserProviderContract<unknown>>(config: {
5
5
  provider: UserProvider;
6
- tokenExpiresIn: number | string;
6
+ tokenExpiresIn?: number | string;
7
7
  useCookies?: boolean;
8
+ content: <T>(user: JwtGuardUser<T>) => Record<string, any>;
8
9
  }): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider>>;
@@ -7,6 +7,7 @@ export function jwtGuard(config) {
7
7
  secret: appKey,
8
8
  expiresIn: config.tokenExpiresIn,
9
9
  useCookies: config.useCookies,
10
+ content: config.content,
10
11
  };
11
12
  return (ctx) => new JwtGuard(ctx, config.provider, options);
12
13
  },
@@ -35,14 +35,15 @@ export interface JwtUserProviderContract<RealUser> {
35
35
  */
36
36
  findById(identifier: string | number | BigInt): Promise<JwtGuardUser<RealUser> | null>;
37
37
  }
38
- export type JwtGuardOptions = {
38
+ export type JwtGuardOptions<RealUser extends any = unknown> = {
39
39
  secret: string;
40
40
  expiresIn?: number | string;
41
41
  useCookies?: boolean;
42
+ content?: (user: JwtGuardUser<RealUser>) => Record<string, any>;
42
43
  };
43
44
  export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>> implements GuardContract<UserProvider[typeof symbols.PROVIDER_REAL_USER]> {
44
45
  #private;
45
- constructor(ctx: HttpContext, userProvider: UserProvider, option: JwtGuardOptions);
46
+ constructor(ctx: HttpContext, userProvider: UserProvider, option: JwtGuardOptions<UserProvider[typeof symbols.PROVIDER_REAL_USER]>);
46
47
  /**
47
48
  * A list of events and their types emitted by
48
49
  * the guard.
package/build/src/jwt.js CHANGED
@@ -8,6 +8,8 @@ export class JwtGuard {
8
8
  this.#ctx = ctx;
9
9
  this.#userProvider = userProvider;
10
10
  this.#options = option;
11
+ if (!this.#options.content)
12
+ this.#options.content = (user) => ({ userId: user.getId() });
11
13
  }
12
14
  /**
13
15
  * A unique name for the guard driver
@@ -32,7 +34,7 @@ export class JwtGuard {
32
34
  */
33
35
  async generate(user) {
34
36
  const providerUser = await this.#userProvider.createUserForGuard(user);
35
- const token = jwt.sign({ userId: providerUser.getId() }, this.#options.secret, this.#options.expiresIn
37
+ const token = jwt.sign(this.#options.content(providerUser), this.#options.secret, this.#options.expiresIn
36
38
  ? {
37
39
  expiresIn: this.#options.expiresIn,
38
40
  }
@@ -68,9 +70,9 @@ export class JwtGuard {
68
70
  * If cookies are enabled, then read the token from the cookies
69
71
  */
70
72
  if (cookieHeader) {
71
- token = this.#ctx.request.cookie('token')
72
- ? this.#ctx.request.cookie('token')
73
- : (this.#ctx.request.request.headers.cookie.match(/token=(.*?)(;|$)/) || [])[1];
73
+ token =
74
+ this.#ctx.request.cookie('token') ??
75
+ (this.#ctx.request.request.headers.cookie.match(/token=(.*?)(;|$)/) || [])[1];
74
76
  }
75
77
  /**
76
78
  * If token is missing on cookies, then try to read it from the header authorization
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@maximemrf/adonisjs-jwt",
3
- "description": "",
4
- "version": "0.0.20",
3
+ "description": "Adonisjs v6 package for authentication with jwt token",
4
+ "version": "0.2.0",
5
5
  "engines": {
6
6
  "node": ">=20.6.0"
7
7
  },
@@ -33,9 +33,6 @@
33
33
  "version": "npm run build",
34
34
  "prepublishOnly": "npm run build"
35
35
  },
36
- "keywords": [],
37
- "author": "",
38
- "license": "MIT",
39
36
  "devDependencies": {
40
37
  "@adonisjs/assembler": "^7.2.3",
41
38
  "@adonisjs/auth": "^9.1.1",
@@ -62,6 +59,14 @@
62
59
  "peerDependencies": {
63
60
  "@adonisjs/core": "^6.2.0"
64
61
  },
62
+ "author": "MaximeMRF",
63
+ "license": "MIT",
64
+ "keywords": [
65
+ "adonisjs",
66
+ "adonis",
67
+ "jwt",
68
+ "authentication"
69
+ ],
65
70
  "publishConfig": {
66
71
  "access": "public",
67
72
  "tag": "latest"
@@ -1,5 +0,0 @@
1
- /**
2
- * Path to the root directory where the stubs are stored. We use
3
- * this path within commands and the configure hook
4
- */
5
- export declare const stubsRoot: string;
@@ -1,7 +0,0 @@
1
- import { dirname } from 'node:path';
2
- import { fileURLToPath } from 'node:url';
3
- /**
4
- * Path to the root directory where the stubs are stored. We use
5
- * this path within commands and the configure hook
6
- */
7
- export const stubsRoot = dirname(fileURLToPath(import.meta.url));