@devmoods/express-extras 0.22.2 → 0.22.3
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/dist/auth.d.ts +4 -3
- package/dist/auth.js +13 -4
- package/package.json +13 -14
package/dist/auth.d.ts
CHANGED
|
@@ -10,9 +10,10 @@ export interface AuthOptions<UserModel extends Record<string, any>> {
|
|
|
10
10
|
redis: RedisClient;
|
|
11
11
|
filterUser(user: UserModel): Partial<UserModel>;
|
|
12
12
|
tokenExpiry?: number;
|
|
13
|
-
|
|
13
|
+
onPasswordResetRequested?(user: UserModel, temporaryToken: string): void | Promise<void>;
|
|
14
14
|
onPasswordChanged?(user: UserModel, newPassword: string): Promise<void>;
|
|
15
|
-
|
|
15
|
+
onEmailVerified?(user: UserModel): Promise<void>;
|
|
16
|
+
onEmailVerificationRequest?(email: string): Promise<void>;
|
|
16
17
|
}
|
|
17
18
|
export interface CreateAuth<UserModel extends Record<string, any>> {
|
|
18
19
|
createRouter: () => Router;
|
|
@@ -20,7 +21,7 @@ export interface CreateAuth<UserModel extends Record<string, any>> {
|
|
|
20
21
|
middleware: (authorize?: boolean) => RequestHandler;
|
|
21
22
|
createVerificationToken(email: string): Promise<string>;
|
|
22
23
|
}
|
|
23
|
-
export declare function createAuth<UserModel extends Record<string, any>>({ getUserByToken, methods: { emailAndPassword }, filterUser, redis, tokenExpiry,
|
|
24
|
+
export declare function createAuth<UserModel extends Record<string, any>>({ getUserByToken, methods: { emailAndPassword }, filterUser, redis, tokenExpiry, onPasswordResetRequested, onPasswordChanged, onEmailVerified, onEmailVerificationRequest, }: AuthOptions<UserModel>): CreateAuth<UserModel>;
|
|
24
25
|
/**
|
|
25
26
|
* Extract a Bearer token from the HTTP Authorization header
|
|
26
27
|
* of `req`.
|
package/dist/auth.js
CHANGED
|
@@ -14,7 +14,7 @@ const TEMP_TOKEN_EXIRES_IN = 3600;
|
|
|
14
14
|
export function createAuthToken(payload, secret = process.env.APP_JWT_SECRET) {
|
|
15
15
|
return jwt.sign(payload, secret, { expiresIn: TOKEN_EXPIRES_IN });
|
|
16
16
|
}
|
|
17
|
-
export function createAuth({ getUserByToken, methods: { emailAndPassword }, filterUser = (x) => x, redis, tokenExpiry = TOKEN_EXPIRES_IN,
|
|
17
|
+
export function createAuth({ getUserByToken, methods: { emailAndPassword }, filterUser = (x) => x, redis, tokenExpiry = TOKEN_EXPIRES_IN, onPasswordResetRequested, onPasswordChanged, onEmailVerified, onEmailVerificationRequest, }) {
|
|
18
18
|
const useCurrentUser = () => useContext('currentUser');
|
|
19
19
|
const setCurrentUser = (user) => setContext('currentUser', user);
|
|
20
20
|
const skipPasswordCheck = async () => true;
|
|
@@ -59,7 +59,7 @@ export function createAuth({ getUserByToken, methods: { emailAndPassword }, filt
|
|
|
59
59
|
const tokenSchema = {
|
|
60
60
|
type: 'string',
|
|
61
61
|
minLength: 50,
|
|
62
|
-
maxLength:
|
|
62
|
+
maxLength: 256,
|
|
63
63
|
};
|
|
64
64
|
const passwordSchema = { type: 'string', minLength: 8 };
|
|
65
65
|
const emailSchema = { type: 'string', minLength: 4 };
|
|
@@ -82,6 +82,15 @@ export function createAuth({ getUserByToken, methods: { emailAndPassword }, filt
|
|
|
82
82
|
logger.info('user changed password');
|
|
83
83
|
return 200;
|
|
84
84
|
}));
|
|
85
|
+
router.post('/verify-email-request', validateBody({
|
|
86
|
+
type: 'object',
|
|
87
|
+
properties: { email: emailSchema },
|
|
88
|
+
required: ['email'],
|
|
89
|
+
}), route(async (req) => {
|
|
90
|
+
const { email } = req.body;
|
|
91
|
+
await onEmailVerificationRequest?.(email);
|
|
92
|
+
return 202;
|
|
93
|
+
}));
|
|
85
94
|
router.post('/verify-email', validateBody({
|
|
86
95
|
type: 'object',
|
|
87
96
|
properties: { token: tokenSchema, email: emailSchema },
|
|
@@ -102,7 +111,7 @@ export function createAuth({ getUserByToken, methods: { emailAndPassword }, filt
|
|
|
102
111
|
}
|
|
103
112
|
const user = await emailAndPassword(email, skipPasswordCheck);
|
|
104
113
|
setCurrentUser(user);
|
|
105
|
-
await
|
|
114
|
+
await onEmailVerified?.(user);
|
|
106
115
|
await redis.del(token);
|
|
107
116
|
logger.info('user verified');
|
|
108
117
|
return 200;
|
|
@@ -142,7 +151,7 @@ export function createAuth({ getUserByToken, methods: { emailAndPassword }, filt
|
|
|
142
151
|
await redis.set(temporaryToken, user.email, {
|
|
143
152
|
EX: TEMP_TOKEN_EXIRES_IN,
|
|
144
153
|
});
|
|
145
|
-
await
|
|
154
|
+
await onPasswordResetRequested?.(user, temporaryToken);
|
|
146
155
|
logger.info('password reset request', { email });
|
|
147
156
|
}
|
|
148
157
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devmoods/express-extras",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.3",
|
|
4
4
|
"description": "Kubernetes-ready utilities for rapid Node.js application development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,18 +8,6 @@
|
|
|
8
8
|
"bin": {
|
|
9
9
|
"migrate": "bin/migrate"
|
|
10
10
|
},
|
|
11
|
-
"scripts": {
|
|
12
|
-
"start": "ts-node --esm example/index.ts",
|
|
13
|
-
"start:worker": "ts-node --esm example/worker.ts",
|
|
14
|
-
"clean": "rm -rf dist",
|
|
15
|
-
"lint": "eslint src",
|
|
16
|
-
"test": "NODE_ENV=test vitest",
|
|
17
|
-
"build": "pnpm run clean && tsc --project tsconfig.build.json",
|
|
18
|
-
"prepublishOnly": "pnpm run clean && pnpm run build",
|
|
19
|
-
"prettier": "prettier --write '**/*.{js,css,md,ts,tsx}'",
|
|
20
|
-
"import-sort": "import-sort --write '**/*.{js,jsx,ts,tsx}'",
|
|
21
|
-
"format": "pnpm run import-sort && pnpm run prettier"
|
|
22
|
-
},
|
|
23
11
|
"author": {
|
|
24
12
|
"name": "hanse",
|
|
25
13
|
"email": "hanse@koren.im"
|
|
@@ -106,5 +94,16 @@
|
|
|
106
94
|
},
|
|
107
95
|
"peerDependencies": {
|
|
108
96
|
"faktory-worker": "^4.5.1"
|
|
97
|
+
},
|
|
98
|
+
"scripts": {
|
|
99
|
+
"start": "ts-node --esm example/index.ts",
|
|
100
|
+
"start:worker": "ts-node --esm example/worker.ts",
|
|
101
|
+
"clean": "rm -rf dist",
|
|
102
|
+
"lint": "eslint src",
|
|
103
|
+
"test": "NODE_ENV=test vitest",
|
|
104
|
+
"build": "pnpm run clean && tsc --project tsconfig.build.json",
|
|
105
|
+
"prettier": "prettier --write '**/*.{js,css,md,ts,tsx}'",
|
|
106
|
+
"import-sort": "import-sort --write '**/*.{js,jsx,ts,tsx}'",
|
|
107
|
+
"format": "pnpm run import-sort && pnpm run prettier"
|
|
109
108
|
}
|
|
110
|
-
}
|
|
109
|
+
}
|