@devmoods/express-extras 0.51.0 → 0.52.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/dist/auth/passwordless.d.ts +1 -1
- package/dist/auth/passwordless.js +19 -2
- package/dist/cookie-consent.d.ts +3 -0
- package/dist/cookie-consent.js +33 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +9 -9
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AnyUser, type AuthProvider } from './types.js';
|
|
2
2
|
export interface PasswordlessAuthProviderOptions<UserModel = AnyUser> {
|
|
3
|
-
getUserByEmail(email: string): Promise<UserModel>;
|
|
3
|
+
getUserByEmail(email: string): Promise<UserModel | null>;
|
|
4
4
|
onLoginRequest(user: UserModel, temporaryToken: string): Promise<void>;
|
|
5
5
|
}
|
|
6
6
|
export declare function PasswordlessAuthProvider<UserModel extends AnyUser>(options: PasswordlessAuthProviderOptions<UserModel>): AuthProvider<UserModel>;
|
|
@@ -3,9 +3,21 @@ import {} from './types.js';
|
|
|
3
3
|
import { Unauthorized } from '../errors.js';
|
|
4
4
|
import { getLogger } from '../logger.js';
|
|
5
5
|
import { route } from '../middleware.js';
|
|
6
|
+
import { DoesNotExistError } from '../postgres.js';
|
|
6
7
|
import { validateBody } from '../validate.js';
|
|
7
8
|
const logger = getLogger('auth');
|
|
8
9
|
export function PasswordlessAuthProvider(options) {
|
|
10
|
+
const getUserOrNull = async (email) => {
|
|
11
|
+
try {
|
|
12
|
+
return await options.getUserByEmail(email);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error instanceof DoesNotExistError) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
9
21
|
return {
|
|
10
22
|
setupRoutes(router, context) {
|
|
11
23
|
router.post('/passwordless/login/:token', route(async (req, res) => {
|
|
@@ -16,10 +28,11 @@ export function PasswordlessAuthProvider(options) {
|
|
|
16
28
|
await context.verifyTemporaryToken(temporaryToken, user);
|
|
17
29
|
}
|
|
18
30
|
catch {
|
|
31
|
+
logger.warn('passwordless: invalid temp token', { uid: user.id });
|
|
19
32
|
throw Unauthorized('Invalid token');
|
|
20
33
|
}
|
|
21
34
|
const accessToken = await context.login(req, res, user);
|
|
22
|
-
logger.info('passwordless login');
|
|
35
|
+
logger.info('passwordless login', { uid: user.id });
|
|
23
36
|
if (req.accepts('html')) {
|
|
24
37
|
return res.redirect('/');
|
|
25
38
|
}
|
|
@@ -36,7 +49,11 @@ export function PasswordlessAuthProvider(options) {
|
|
|
36
49
|
required: ['email'],
|
|
37
50
|
}), route(async (req) => {
|
|
38
51
|
const { email } = req.body;
|
|
39
|
-
const user = await
|
|
52
|
+
const user = await getUserOrNull(email);
|
|
53
|
+
if (!user) {
|
|
54
|
+
logger.warn('passwordless: user not found', { email });
|
|
55
|
+
return 202;
|
|
56
|
+
}
|
|
40
57
|
const token = await context.createTemporaryToken(user);
|
|
41
58
|
await options.onLoginRequest(user, token);
|
|
42
59
|
logger.info('passwordless login requested', { email });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { getLogger } from './logger.js';
|
|
3
|
+
import { route } from './middleware.js';
|
|
4
|
+
import { validateBody } from './validate.js';
|
|
5
|
+
const logger = getLogger();
|
|
6
|
+
export function cookieConsent({ onConsent, }) {
|
|
7
|
+
const router = Router();
|
|
8
|
+
router.get('/', route(() => {
|
|
9
|
+
return {
|
|
10
|
+
consents: [],
|
|
11
|
+
};
|
|
12
|
+
}));
|
|
13
|
+
router.post('/', validateBody({
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: {
|
|
16
|
+
consents: {
|
|
17
|
+
type: 'array',
|
|
18
|
+
items: { type: 'string' },
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
}), route(async (req, res) => {
|
|
22
|
+
const { consents } = req.body;
|
|
23
|
+
res.cookie('cookie_consent', consents.join(',') || '1', {
|
|
24
|
+
expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000),
|
|
25
|
+
sameSite: 'strict',
|
|
26
|
+
secure: process.env.NODE_ENV === 'production',
|
|
27
|
+
});
|
|
28
|
+
await onConsent?.(consents);
|
|
29
|
+
logger.info('cookie consent');
|
|
30
|
+
return { consents };
|
|
31
|
+
}));
|
|
32
|
+
return router;
|
|
33
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devmoods/express-extras",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.0",
|
|
4
4
|
"description": "Kubernetes-ready utilities for rapid Node.js application development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -44,15 +44,15 @@
|
|
|
44
44
|
],
|
|
45
45
|
"license": "ISC",
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"arktype": "^2.0.2",
|
|
48
47
|
"@devmoods/eslint-config": "^2.6.0",
|
|
49
48
|
"@types/koa-compose": "^3.2.8",
|
|
50
|
-
"@types/node": "22.
|
|
49
|
+
"@types/node": "22.13.0",
|
|
51
50
|
"@types/supertest": "^6.0.2",
|
|
52
51
|
"@vitejs/plugin-react": "^4.3.4",
|
|
53
52
|
"@vitest/coverage-v8": "^3.0.4",
|
|
53
|
+
"arktype": "^2.0.4",
|
|
54
54
|
"cookiejar": "^2.1.4",
|
|
55
|
-
"eslint": "^9.
|
|
55
|
+
"eslint": "^9.19.0",
|
|
56
56
|
"faktory-worker": "^4.6.0",
|
|
57
57
|
"prettier": "3.4.2",
|
|
58
58
|
"supertest": "7.0.0",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
67
|
"@devmoods/fetch": "^4.0.1",
|
|
68
|
-
"@sentry/node": "^8.
|
|
69
|
-
"@sentry/profiling-node": "^8.
|
|
68
|
+
"@sentry/node": "^8.53.0",
|
|
69
|
+
"@sentry/profiling-node": "^8.53.0",
|
|
70
70
|
"@types/bcrypt": "^5.0.2",
|
|
71
71
|
"@types/cookie-parser": "^1.4.8",
|
|
72
72
|
"@types/express": "^5.0.0",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
"pg": "^8.13.1",
|
|
91
91
|
"redis": "^4.7.0",
|
|
92
92
|
"sonda": "^0.7.1",
|
|
93
|
-
"sql-formatter": "^15.4.
|
|
93
|
+
"sql-formatter": "^15.4.10",
|
|
94
94
|
"uuid": "^11.0.5"
|
|
95
95
|
},
|
|
96
96
|
"prettier": {
|
|
@@ -98,9 +98,9 @@
|
|
|
98
98
|
},
|
|
99
99
|
"peerDependencies": {
|
|
100
100
|
"@vitejs/plugin-react": ">=4.3.4",
|
|
101
|
+
"arktype": ">=2.0.0",
|
|
101
102
|
"faktory-worker": ">=4.5.1",
|
|
102
|
-
"vite": ">=6.0.5"
|
|
103
|
-
"arktype": ">=2.0.2"
|
|
103
|
+
"vite": ">=6.0.5"
|
|
104
104
|
},
|
|
105
105
|
"engines": {
|
|
106
106
|
"node": ">=20 <24"
|