@maximemrf/adonisjs-jwt 0.2.0 → 0.2.2
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 +8 -3
- package/build/src/define_config.d.ts +3 -2
- package/build/src/jwt.d.ts +1 -40
- package/build/src/types.d.ts +44 -0
- package/build/src/types.js +1 -0
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -26,7 +26,12 @@ 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
|
+
import { JwtGuardUser, BaseJwtContent } from '@maximemrf/adonisjs-jwt/types'
|
|
30
|
+
import User from '#models/user'
|
|
31
|
+
|
|
32
|
+
interface JwtContent extends BaseJwtContent {
|
|
33
|
+
email: string
|
|
34
|
+
}
|
|
30
35
|
|
|
31
36
|
const authConfig = defineConfig({
|
|
32
37
|
// define the default authenticator to jwt
|
|
@@ -48,8 +53,8 @@ const authConfig = defineConfig({
|
|
|
48
53
|
model: () => import('#models/user'),
|
|
49
54
|
}),
|
|
50
55
|
// 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
|
-
|
|
56
|
+
content: (user: JwtGuardUser<User>): JwtContent => ({
|
|
57
|
+
userId: user.getId(),
|
|
53
58
|
email: user.getOriginal().email,
|
|
54
59
|
}),
|
|
55
60
|
}),
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { GuardConfigProvider } from '@adonisjs/auth/types';
|
|
2
2
|
import type { HttpContext } from '@adonisjs/core/http';
|
|
3
|
-
import {
|
|
3
|
+
import { JwtGuardUser, JwtUserProviderContract } from './types.js';
|
|
4
|
+
import { JwtGuard } from './jwt.js';
|
|
4
5
|
export declare function jwtGuard<UserProvider extends JwtUserProviderContract<unknown>>(config: {
|
|
5
6
|
provider: UserProvider;
|
|
6
7
|
tokenExpiresIn?: number | string;
|
|
7
8
|
useCookies?: boolean;
|
|
8
|
-
content: <T>(user: JwtGuardUser<T>) => Record<string, any>;
|
|
9
|
+
content: <T>(user: JwtGuardUser<T>) => Record<string | number, any>;
|
|
9
10
|
}): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider>>;
|
package/build/src/jwt.d.ts
CHANGED
|
@@ -1,46 +1,7 @@
|
|
|
1
1
|
import { symbols } from '@adonisjs/auth';
|
|
2
2
|
import { AuthClientResponse, GuardContract } from '@adonisjs/auth/types';
|
|
3
3
|
import type { HttpContext } from '@adonisjs/core/http';
|
|
4
|
-
|
|
5
|
-
* The bridge between the User provider and the
|
|
6
|
-
* Guard
|
|
7
|
-
*/
|
|
8
|
-
export type JwtGuardUser<RealUser> = {
|
|
9
|
-
/**
|
|
10
|
-
* Returns the unique ID of the user
|
|
11
|
-
*/
|
|
12
|
-
getId(): string | number | BigInt;
|
|
13
|
-
/**
|
|
14
|
-
* Returns the original user object
|
|
15
|
-
*/
|
|
16
|
-
getOriginal(): RealUser;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* The interface for the UserProvider accepted by the
|
|
20
|
-
* JWT guard.
|
|
21
|
-
*/
|
|
22
|
-
export interface JwtUserProviderContract<RealUser> {
|
|
23
|
-
/**
|
|
24
|
-
* A property the guard implementation can use to infer
|
|
25
|
-
* the data type of the actual user (aka RealUser)
|
|
26
|
-
*/
|
|
27
|
-
[symbols.PROVIDER_REAL_USER]: RealUser;
|
|
28
|
-
/**
|
|
29
|
-
* Create a user object that acts as an adapter between
|
|
30
|
-
* the guard and real user value.
|
|
31
|
-
*/
|
|
32
|
-
createUserForGuard(user: RealUser): Promise<JwtGuardUser<RealUser>>;
|
|
33
|
-
/**
|
|
34
|
-
* Find a user by their id.
|
|
35
|
-
*/
|
|
36
|
-
findById(identifier: string | number | BigInt): Promise<JwtGuardUser<RealUser> | null>;
|
|
37
|
-
}
|
|
38
|
-
export type JwtGuardOptions<RealUser extends any = unknown> = {
|
|
39
|
-
secret: string;
|
|
40
|
-
expiresIn?: number | string;
|
|
41
|
-
useCookies?: boolean;
|
|
42
|
-
content?: (user: JwtGuardUser<RealUser>) => Record<string, any>;
|
|
43
|
-
};
|
|
4
|
+
import { JwtUserProviderContract, JwtGuardOptions } from './types.js';
|
|
44
5
|
export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>> implements GuardContract<UserProvider[typeof symbols.PROVIDER_REAL_USER]> {
|
|
45
6
|
#private;
|
|
46
7
|
constructor(ctx: HttpContext, userProvider: UserProvider, option: JwtGuardOptions<UserProvider[typeof symbols.PROVIDER_REAL_USER]>);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { symbols } from '@adonisjs/auth';
|
|
2
|
+
/**
|
|
3
|
+
* The bridge between the User provider and the
|
|
4
|
+
* Guard
|
|
5
|
+
*/
|
|
6
|
+
export type JwtGuardUser<RealUser> = {
|
|
7
|
+
/**
|
|
8
|
+
* Returns the unique ID of the user
|
|
9
|
+
*/
|
|
10
|
+
getId(): string | number | BigInt;
|
|
11
|
+
/**
|
|
12
|
+
* Returns the original user object
|
|
13
|
+
*/
|
|
14
|
+
getOriginal(): RealUser;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* The interface for the UserProvider accepted by the
|
|
18
|
+
* JWT guard.
|
|
19
|
+
*/
|
|
20
|
+
export interface JwtUserProviderContract<RealUser> {
|
|
21
|
+
/**
|
|
22
|
+
* A property the guard implementation can use to infer
|
|
23
|
+
* the data type of the actual user (aka RealUser)
|
|
24
|
+
*/
|
|
25
|
+
[symbols.PROVIDER_REAL_USER]: RealUser;
|
|
26
|
+
/**
|
|
27
|
+
* Create a user object that acts as an adapter between
|
|
28
|
+
* the guard and real user value.
|
|
29
|
+
*/
|
|
30
|
+
createUserForGuard(user: RealUser): Promise<JwtGuardUser<RealUser>>;
|
|
31
|
+
/**
|
|
32
|
+
* Find a user by their id.
|
|
33
|
+
*/
|
|
34
|
+
findById(identifier: string | number | BigInt): Promise<JwtGuardUser<RealUser> | null>;
|
|
35
|
+
}
|
|
36
|
+
export type BaseJwtContent = {
|
|
37
|
+
userId: string | number | BigInt;
|
|
38
|
+
};
|
|
39
|
+
export type JwtGuardOptions<RealUser extends any = unknown> = {
|
|
40
|
+
secret: string;
|
|
41
|
+
expiresIn?: number | string;
|
|
42
|
+
useCookies?: boolean;
|
|
43
|
+
content?: (user: JwtGuardUser<RealUser>) => Record<string, any>;
|
|
44
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { symbols } from '@adonisjs/auth';
|
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.2.
|
|
4
|
+
"version": "0.2.2",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.6.0"
|
|
7
7
|
},
|
|
@@ -56,6 +56,10 @@
|
|
|
56
56
|
"ts-node": "^10.9.2",
|
|
57
57
|
"typescript": "^5.4.2"
|
|
58
58
|
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@types/jsonwebtoken": "^9.0.6",
|
|
61
|
+
"jsonwebtoken": "^9.0.2"
|
|
62
|
+
},
|
|
59
63
|
"peerDependencies": {
|
|
60
64
|
"@adonisjs/core": "^6.2.0"
|
|
61
65
|
},
|
|
@@ -67,6 +71,10 @@
|
|
|
67
71
|
"jwt",
|
|
68
72
|
"authentication"
|
|
69
73
|
],
|
|
74
|
+
"eslintConfig": {
|
|
75
|
+
"extends": "@adonisjs/eslint-config/package"
|
|
76
|
+
},
|
|
77
|
+
"prettier": "@adonisjs/prettier-config",
|
|
70
78
|
"publishConfig": {
|
|
71
79
|
"access": "public",
|
|
72
80
|
"tag": "latest"
|
|
@@ -85,13 +93,5 @@
|
|
|
85
93
|
"exclude": [
|
|
86
94
|
"tests/**"
|
|
87
95
|
]
|
|
88
|
-
},
|
|
89
|
-
"eslintConfig": {
|
|
90
|
-
"extends": "@adonisjs/eslint-config/package"
|
|
91
|
-
},
|
|
92
|
-
"prettier": "@adonisjs/prettier-config",
|
|
93
|
-
"dependencies": {
|
|
94
|
-
"@types/jsonwebtoken": "^9.0.6",
|
|
95
|
-
"jsonwebtoken": "^9.0.2"
|
|
96
96
|
}
|
|
97
97
|
}
|