@logto/express 1.1.1 → 1.1.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/lib/errors.d.ts +10 -0
- package/lib/errors.js +14 -0
- package/lib/errors.mjs +12 -0
- package/lib/index.d.ts +6 -14
- package/lib/index.js +54 -78
- package/lib/index.mjs +66 -0
- package/lib/index.test.d.ts +1 -0
- package/lib/storage.d.ts +9 -0
- package/lib/storage.js +24 -0
- package/lib/storage.mjs +20 -0
- package/lib/storage.test.d.ts +1 -0
- package/lib/test-utils.d.ts +15 -0
- package/lib/types.d.ts +9 -0
- package/package.json +14 -17
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/module.d.mts +0 -17
- package/lib/module.mjs +0 -94
- package/lib/module.mjs.map +0 -1
package/lib/errors.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const logtoExpressErrorCodes: Readonly<{
|
|
2
|
+
session_not_configured: "You should configure express-session before using Logto express SDK.";
|
|
3
|
+
}>;
|
|
4
|
+
export type LogtoClientErrorCode = keyof typeof logtoExpressErrorCodes;
|
|
5
|
+
export declare class LogtoExpressError extends Error {
|
|
6
|
+
code: LogtoClientErrorCode;
|
|
7
|
+
data: unknown;
|
|
8
|
+
constructor(code: LogtoClientErrorCode, data?: unknown);
|
|
9
|
+
}
|
|
10
|
+
export {};
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const logtoExpressErrorCodes = Object.freeze({
|
|
4
|
+
session_not_configured: 'You should configure express-session before using Logto express SDK.',
|
|
5
|
+
});
|
|
6
|
+
class LogtoExpressError extends Error {
|
|
7
|
+
constructor(code, data) {
|
|
8
|
+
super(logtoExpressErrorCodes[code]);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.data = data;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.LogtoExpressError = LogtoExpressError;
|
package/lib/errors.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const logtoExpressErrorCodes = Object.freeze({
|
|
2
|
+
session_not_configured: 'You should configure express-session before using Logto express SDK.',
|
|
3
|
+
});
|
|
4
|
+
class LogtoExpressError extends Error {
|
|
5
|
+
constructor(code, data) {
|
|
6
|
+
super(logtoExpressErrorCodes[code]);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.data = data;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { LogtoExpressError };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
interface IncomingMessage {
|
|
5
|
-
session: Record<string, string | undefined>;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export type LogtoExpressConfig = LogtoConfig & {
|
|
9
|
-
baseUrl: string;
|
|
10
|
-
} & GetContextParameters;
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { Router } from 'express';
|
|
3
|
+
import type { LogtoExpressConfig } from './types';
|
|
11
4
|
export { ReservedScope, UserScope } from '@logto/node';
|
|
12
5
|
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
6
|
+
export type { LogtoExpressConfig } from './types';
|
|
13
7
|
export type Middleware = (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
14
|
-
export const handleAuthRoutes: (config: LogtoExpressConfig) => Router;
|
|
15
|
-
export const withLogto: (config: LogtoExpressConfig) => Middleware;
|
|
16
|
-
|
|
17
|
-
//# sourceMappingURL=index.d.ts.map
|
|
8
|
+
export declare const handleAuthRoutes: (config: LogtoExpressConfig) => Router;
|
|
9
|
+
export declare const withLogto: (config: LogtoExpressConfig) => Middleware;
|
package/lib/index.js
CHANGED
|
@@ -1,104 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
var $7B6LY$express = require("express");
|
|
1
|
+
'use strict';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
9
|
-
}
|
|
3
|
+
var NodeClient = require('@logto/node');
|
|
4
|
+
var express = require('express');
|
|
5
|
+
var errors = require('./errors.js');
|
|
6
|
+
var storage = require('./storage.js');
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
$parcel$export(module.exports, "withLogto", () => $e5532821beefe084$export$f9a8e793abe4305b);
|
|
13
|
-
$parcel$export(module.exports, "ReservedScope", () => $e5532821beefe084$re_export$ReservedScope);
|
|
14
|
-
$parcel$export(module.exports, "UserScope", () => $e5532821beefe084$re_export$UserScope);
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
9
|
|
|
10
|
+
var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
|
|
16
11
|
|
|
17
|
-
const
|
|
18
|
-
session_not_configured: "You should configure express-session before using Logto express SDK."
|
|
19
|
-
});
|
|
20
|
-
class $c0c4c8dcf2e7823b$export$b838f54079ce95de extends Error {
|
|
21
|
-
constructor(code, data){
|
|
22
|
-
super($c0c4c8dcf2e7823b$var$logtoExpressErrorCodes[code]);
|
|
23
|
-
this.code = code;
|
|
24
|
-
this.data = data;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class $27b238e5def1d471$export$2e2bcd8739ae039 {
|
|
30
|
-
constructor(request){
|
|
31
|
-
this.request = request;
|
|
32
|
-
}
|
|
33
|
-
async setItem(key, value) {
|
|
34
|
-
this.request.session[key] = value;
|
|
35
|
-
}
|
|
36
|
-
async getItem(key) {
|
|
37
|
-
const value = this.request.session[key];
|
|
38
|
-
if (value === undefined) return null;
|
|
39
|
-
return String(value);
|
|
40
|
-
}
|
|
41
|
-
async removeItem(key) {
|
|
42
|
-
this.request.session[key] = undefined;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const $e5532821beefe084$var$createNodeClient = (request, response, config)=>{
|
|
12
|
+
const createNodeClient = (request, response, config) => {
|
|
49
13
|
// We assume that `session` is configured in the express app, but need to check it there.
|
|
50
14
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
51
|
-
if (!request.session)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
15
|
+
if (!request.session) {
|
|
16
|
+
throw new errors.LogtoExpressError('session_not_configured');
|
|
17
|
+
}
|
|
18
|
+
const storage$1 = new storage.default(request);
|
|
19
|
+
return new NodeClient__default.default(config, {
|
|
20
|
+
storage: storage$1,
|
|
21
|
+
navigate: (url) => {
|
|
56
22
|
response.redirect(url);
|
|
57
|
-
}
|
|
23
|
+
},
|
|
58
24
|
});
|
|
59
25
|
};
|
|
60
|
-
const
|
|
26
|
+
const handleAuthRoutes = (config) => {
|
|
61
27
|
// eslint-disable-next-line new-cap
|
|
62
|
-
const router =
|
|
63
|
-
router.use(
|
|
64
|
-
const { action
|
|
65
|
-
const nodeClient =
|
|
66
|
-
switch(action){
|
|
67
|
-
case
|
|
28
|
+
const router = express.Router();
|
|
29
|
+
router.use('/logto/:action', async (request, response) => {
|
|
30
|
+
const { action } = request.params;
|
|
31
|
+
const nodeClient = createNodeClient(request, response, config);
|
|
32
|
+
switch (action) {
|
|
33
|
+
case 'sign-in': {
|
|
68
34
|
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);
|
|
69
35
|
break;
|
|
70
|
-
|
|
71
|
-
|
|
36
|
+
}
|
|
37
|
+
case 'sign-up': {
|
|
38
|
+
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`, 'signUp');
|
|
72
39
|
break;
|
|
73
|
-
|
|
40
|
+
}
|
|
41
|
+
case 'sign-in-callback': {
|
|
74
42
|
if (request.url) {
|
|
75
43
|
await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);
|
|
76
44
|
response.redirect(config.baseUrl);
|
|
77
45
|
}
|
|
78
46
|
break;
|
|
79
|
-
|
|
47
|
+
}
|
|
48
|
+
case 'sign-out': {
|
|
80
49
|
await nodeClient.signOut(config.baseUrl);
|
|
81
50
|
break;
|
|
82
|
-
|
|
51
|
+
}
|
|
52
|
+
default: {
|
|
83
53
|
response.status(404).end();
|
|
54
|
+
}
|
|
84
55
|
}
|
|
85
56
|
});
|
|
86
57
|
return router;
|
|
87
58
|
};
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
});
|
|
100
|
-
next();
|
|
101
|
-
};
|
|
102
|
-
|
|
59
|
+
const withLogto = (config) => async (request, response, next) => {
|
|
60
|
+
const client = createNodeClient(request, response, config);
|
|
61
|
+
const user = await client.getContext({
|
|
62
|
+
getAccessToken: config.getAccessToken,
|
|
63
|
+
resource: config.resource,
|
|
64
|
+
fetchUserInfo: config.fetchUserInfo,
|
|
65
|
+
});
|
|
66
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
67
|
+
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
68
|
+
next();
|
|
69
|
+
};
|
|
103
70
|
|
|
104
|
-
|
|
71
|
+
Object.defineProperty(exports, 'ReservedScope', {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
get: function () { return NodeClient.ReservedScope; }
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(exports, 'UserScope', {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function () { return NodeClient.UserScope; }
|
|
78
|
+
});
|
|
79
|
+
exports.handleAuthRoutes = handleAuthRoutes;
|
|
80
|
+
exports.withLogto = withLogto;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import NodeClient from '@logto/node';
|
|
2
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
3
|
+
import { Router } from 'express';
|
|
4
|
+
import { LogtoExpressError } from './errors.mjs';
|
|
5
|
+
import ExpressStorage from './storage.mjs';
|
|
6
|
+
|
|
7
|
+
const createNodeClient = (request, response, config) => {
|
|
8
|
+
// We assume that `session` is configured in the express app, but need to check it there.
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
10
|
+
if (!request.session) {
|
|
11
|
+
throw new LogtoExpressError('session_not_configured');
|
|
12
|
+
}
|
|
13
|
+
const storage = new ExpressStorage(request);
|
|
14
|
+
return new NodeClient(config, {
|
|
15
|
+
storage,
|
|
16
|
+
navigate: (url) => {
|
|
17
|
+
response.redirect(url);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
const handleAuthRoutes = (config) => {
|
|
22
|
+
// eslint-disable-next-line new-cap
|
|
23
|
+
const router = Router();
|
|
24
|
+
router.use('/logto/:action', async (request, response) => {
|
|
25
|
+
const { action } = request.params;
|
|
26
|
+
const nodeClient = createNodeClient(request, response, config);
|
|
27
|
+
switch (action) {
|
|
28
|
+
case 'sign-in': {
|
|
29
|
+
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 'sign-up': {
|
|
33
|
+
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`, 'signUp');
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case 'sign-in-callback': {
|
|
37
|
+
if (request.url) {
|
|
38
|
+
await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);
|
|
39
|
+
response.redirect(config.baseUrl);
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case 'sign-out': {
|
|
44
|
+
await nodeClient.signOut(config.baseUrl);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
default: {
|
|
48
|
+
response.status(404).end();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return router;
|
|
53
|
+
};
|
|
54
|
+
const withLogto = (config) => async (request, response, next) => {
|
|
55
|
+
const client = createNodeClient(request, response, config);
|
|
56
|
+
const user = await client.getContext({
|
|
57
|
+
getAccessToken: config.getAccessToken,
|
|
58
|
+
resource: config.resource,
|
|
59
|
+
fetchUserInfo: config.fetchUserInfo,
|
|
60
|
+
});
|
|
61
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
62
|
+
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
63
|
+
next();
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export { handleAuthRoutes, withLogto };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/storage.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'http';
|
|
2
|
+
import type { Storage, StorageKey } from '@logto/node';
|
|
3
|
+
export default class ExpressStorage implements Storage {
|
|
4
|
+
private readonly request;
|
|
5
|
+
constructor(request: IncomingMessage);
|
|
6
|
+
setItem(key: StorageKey, value: string): Promise<void>;
|
|
7
|
+
getItem(key: StorageKey): Promise<string | null>;
|
|
8
|
+
removeItem(key: StorageKey): Promise<void>;
|
|
9
|
+
}
|
package/lib/storage.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
class ExpressStorage {
|
|
6
|
+
constructor(request) {
|
|
7
|
+
this.request = request;
|
|
8
|
+
}
|
|
9
|
+
async setItem(key, value) {
|
|
10
|
+
this.request.session[key] = value;
|
|
11
|
+
}
|
|
12
|
+
async getItem(key) {
|
|
13
|
+
const value = this.request.session[key];
|
|
14
|
+
if (value === undefined) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return String(value);
|
|
18
|
+
}
|
|
19
|
+
async removeItem(key) {
|
|
20
|
+
this.request.session[key] = undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.default = ExpressStorage;
|
package/lib/storage.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class ExpressStorage {
|
|
2
|
+
constructor(request) {
|
|
3
|
+
this.request = request;
|
|
4
|
+
}
|
|
5
|
+
async setItem(key, value) {
|
|
6
|
+
this.request.session[key] = value;
|
|
7
|
+
}
|
|
8
|
+
async getItem(key) {
|
|
9
|
+
const value = this.request.session[key];
|
|
10
|
+
if (value === undefined) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return String(value);
|
|
14
|
+
}
|
|
15
|
+
async removeItem(key) {
|
|
16
|
+
this.request.session[key] = undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { ExpressStorage as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { NextFunction, Request, Response, Router } from 'express';
|
|
2
|
+
import request from 'supertest';
|
|
3
|
+
import type { Middleware } from '.';
|
|
4
|
+
type TestMiddlewareParameters = {
|
|
5
|
+
middleware: Middleware;
|
|
6
|
+
url?: string;
|
|
7
|
+
test: ({ response, request, next, }: {
|
|
8
|
+
response: Response;
|
|
9
|
+
request: Request;
|
|
10
|
+
next: NextFunction;
|
|
11
|
+
}) => Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
export declare const testMiddleware: ({ middleware, url, test }: TestMiddlewareParameters) => Promise<void>;
|
|
14
|
+
export declare const testRouter: (router: Router) => request.SuperTest<request.Test>;
|
|
15
|
+
export {};
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { GetContextParameters, LogtoConfig } from '@logto/node';
|
|
2
|
+
declare module 'http' {
|
|
3
|
+
interface IncomingMessage {
|
|
4
|
+
session: Record<string, string | undefined>;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export type LogtoExpressConfig = LogtoConfig & {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
} & GetContextParameters;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/express",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
"require": "./lib/index.js",
|
|
8
|
-
"import": "./lib/
|
|
8
|
+
"import": "./lib/index.mjs"
|
|
9
9
|
},
|
|
10
|
-
"module": "./lib/
|
|
10
|
+
"module": "./lib/index.mjs",
|
|
11
11
|
"types": "./lib/index.d.ts",
|
|
12
12
|
"files": [
|
|
13
13
|
"lib"
|
|
@@ -22,41 +22,38 @@
|
|
|
22
22
|
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
23
23
|
"precommit": "lint-staged",
|
|
24
24
|
"check": "tsc --noEmit",
|
|
25
|
-
"build": "rm -rf lib/ &&
|
|
25
|
+
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
26
26
|
"lint": "eslint --ext .ts src",
|
|
27
27
|
"test": "jest",
|
|
28
28
|
"test:coverage": "jest --silent --coverage",
|
|
29
29
|
"prepack": "pnpm test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@logto/node": "^1.1.
|
|
32
|
+
"@logto/node": "^1.1.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@jest/types": "^
|
|
36
|
-
"@parcel/core": "^2.8.3",
|
|
37
|
-
"@parcel/packager-ts": "^2.8.3",
|
|
38
|
-
"@parcel/transformer-typescript-types": "^2.8.3",
|
|
35
|
+
"@jest/types": "^29.5.0",
|
|
39
36
|
"@silverhand/eslint-config": "^2.0.0",
|
|
40
37
|
"@silverhand/ts-config": "^1.0.0",
|
|
41
38
|
"@silverhand/ts-config-react": "^2.0.0",
|
|
39
|
+
"@swc/core": "^1.3.50",
|
|
40
|
+
"@swc/jest": "^0.2.24",
|
|
42
41
|
"@types/cookie-parser": "^1.4.3",
|
|
43
42
|
"@types/express": "^4.17.13",
|
|
44
43
|
"@types/express-session": "^1.17.5",
|
|
45
|
-
"@types/jest": "^
|
|
44
|
+
"@types/jest": "^29.5.0",
|
|
46
45
|
"@types/supertest": "^2.0.12",
|
|
47
46
|
"cookie-parser": "^1.4.6",
|
|
48
|
-
"eslint": "^8.
|
|
47
|
+
"eslint": "^8.38.0",
|
|
49
48
|
"express": "^4.18.1",
|
|
50
49
|
"express-session": "^1.17.3",
|
|
51
|
-
"jest": "^
|
|
50
|
+
"jest": "^29.5.0",
|
|
52
51
|
"jest-location-mock": "^1.0.9",
|
|
53
52
|
"jest-matcher-specific-error": "^1.0.0",
|
|
54
53
|
"lint-staged": "^13.0.0",
|
|
55
|
-
"
|
|
56
|
-
"prettier": "^2.7.1",
|
|
54
|
+
"prettier": "^2.8.7",
|
|
57
55
|
"supertest": "^6.3.3",
|
|
58
|
-
"
|
|
59
|
-
"typescript": "4.9.5"
|
|
56
|
+
"typescript": "^5.0.0"
|
|
60
57
|
},
|
|
61
58
|
"peerDependencies": {
|
|
62
59
|
"express": ">=4"
|
|
@@ -76,5 +73,5 @@
|
|
|
76
73
|
}
|
|
77
74
|
}
|
|
78
75
|
},
|
|
79
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "9e9a8b0887ef67baa7c3c564590bb06e7801d03e"
|
|
80
77
|
}
|
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;AEEA,eAAe,MAAM,CAAC;IAGpB,UAAU,eAAe;QACvB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;KAC7C;CACF;AAED,iCAAiC,WAAW,GAAG;IAC7C,OAAO,EAAE,MAAM,CAAC;CACjB,GAAG,oBAAoB,CAAC;ACFzB,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvD,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGjE,yBAAyB,CACvB,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,YAAY,KACf,OAAO,CAAC,IAAI,CAAC,CAAC;AAuBnB,OAAO,MAAM,2BAA4B,kBAAkB,KAAG,MA2C7D,CAAC;AAEF,OAAO,MAAM,oBACF,kBAAkB,KAAG,UAW7B,CAAC","sources":["packages/express/src/src/errors.ts","packages/express/src/src/storage.ts","packages/express/src/src/types.ts","packages/express/src/src/index.ts","packages/express/src/index.ts"],"sourcesContent":[null,null,null,null,"import type { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport type { Request, Response, NextFunction } from 'express';\nimport { Router } from 'express';\n\nimport { LogtoExpressError } from './errors';\nimport ExpressStorage from './storage';\nimport type { LogtoExpressConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext, InteractionMode } from '@logto/node';\nexport type { LogtoExpressConfig } from './types';\n\nexport type Middleware = (\n request: Request,\n response: Response,\n next: NextFunction\n) => Promise<void>;\n\nconst createNodeClient = (\n request: IncomingMessage,\n response: Response,\n config: LogtoExpressConfig\n) => {\n // We assume that `session` is configured in the express app, but need to check it there.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!request.session) {\n throw new LogtoExpressError('session_not_configured');\n }\n\n const storage = new ExpressStorage(request);\n\n return new NodeClient(config, {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\n });\n};\n\nexport const handleAuthRoutes = (config: LogtoExpressConfig): Router => {\n // eslint-disable-next-line new-cap\n const router = Router();\n\n router.use('/logto/:action', async (request, response) => {\n const { action } = request.params;\n const nodeClient = createNodeClient(request, response, config);\n\n switch (action) {\n case 'sign-in': {\n await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);\n\n break;\n }\n\n case 'sign-up': {\n await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`, 'signUp');\n\n break;\n }\n\n case 'sign-in-callback': {\n if (request.url) {\n await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);\n response.redirect(config.baseUrl);\n }\n\n break;\n }\n\n case 'sign-out': {\n await nodeClient.signOut(config.baseUrl);\n\n break;\n }\n\n default: {\n response.status(404).end();\n }\n }\n });\n\n return router;\n};\n\nexport const withLogto =\n (config: LogtoExpressConfig): Middleware =>\n async (request: IncomingMessage, response: Response, next: NextFunction) => {\n const client = createNodeClient(request, response, config);\n const user = await client.getContext({\n getAccessToken: config.getAccessToken,\n resource: config.resource,\n fetchUserInfo: config.fetchUserInfo,\n });\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n next();\n };\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;AAAA;;ACAA,MAAM,+CAAyB,OAAO,MAAM,CAAC;IAC3C,wBAAwB;AAC1B;AAIO,MAAM,kDAA0B;IAIrC,YAAY,IAA0B,EAAE,IAAc,CAAE;QACtD,KAAK,CAAC,4CAAsB,CAAC,KAAK;QAClC,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,IAAI,GAAG;IACd;AACF;;;ACXe;IACb,YAA6B,QAA0B;uBAA1B;IAA2B;IAExD,MAAM,QAAQ,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;IAC9B;IAEA,MAAM,QAAQ,GAAe,EAAE;QAC7B,MAAM,QAAQ,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI;QAEvC,IAAI,UAAU,WACZ,OAAO,IAAI;QAGb,OAAO,OAAO;IAChB;IAEA,MAAM,WAAW,GAAe,EAAE;QAChC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;IAC9B;AACF;;;;AFHA,MAAM,yCAAmB,CACvB,SACA,UACA,SACG;IACH,yFAAyF;IACzF,uEAAuE;IACvE,IAAI,CAAC,QAAQ,OAAO,EAClB,MAAM,IAAI,CAAA,GAAA,yCAAgB,EAAE,0BAA0B;IAGxD,MAAM,UAAU,IAAI,CAAA,GAAA,wCAAa,EAAE;IAEnC,OAAO,IAAI,CAAA,GAAA,0CAAS,EAAE,QAAQ;iBAC5B;QACA,UAAU,CAAC,MAAQ;YACjB,SAAS,QAAQ,CAAC;QACpB;IACF;AACF;AAEO,MAAM,4CAAmB,CAAC,SAAuC;IACtE,mCAAmC;IACnC,MAAM,SAAS,CAAA,GAAA,qBAAM,AAAD;IAEpB,OAAO,GAAG,CAAC,kBAAkB,OAAO,SAAS,WAAa;QACxD,MAAM,UAAE,OAAM,EAAE,GAAG,QAAQ,MAAM;QACjC,MAAM,aAAa,uCAAiB,SAAS,UAAU;QAEvD,OAAQ;YACN,KAAK;gBACH,MAAM,WAAW,MAAM,CAAC,CAAC,EAAE,OAAO,OAAO,CAAC,uBAAuB,CAAC;gBAElE,KAAM;YAGR,KAAK;gBACH,MAAM,WAAW,MAAM,CAAC,CAAC,EAAE,OAAO,OAAO,CAAC,uBAAuB,CAAC,EAAE;gBAEpE,KAAM;YAGR,KAAK;gBACH,IAAI,QAAQ,GAAG,EAAE;oBACf,MAAM,WAAW,oBAAoB,CAAC,CAAC,EAAE,OAAO,OAAO,CAAC,EAAE,QAAQ,WAAW,CAAC,CAAC;oBAC/E,SAAS,QAAQ,CAAC,OAAO,OAAO;gBAClC,CAAC;gBAED,KAAM;YAGR,KAAK;gBACH,MAAM,WAAW,OAAO,CAAC,OAAO,OAAO;gBAEvC,KAAM;YAGR;gBACE,SAAS,MAAM,CAAC,KAAK,GAAG;QAE5B;IACF;IAEA,OAAO;AACT;AAEO,MAAM,4CACX,CAAC,SACD,OAAO,SAA0B,UAAoB,OAAuB;QAC1E,MAAM,SAAS,uCAAiB,SAAS,UAAU;QACnD,MAAM,OAAO,MAAM,OAAO,UAAU,CAAC;YACnC,gBAAgB,OAAO,cAAc;YACrC,UAAU,OAAO,QAAQ;YACzB,eAAe,OAAO,aAAa;QACrC;QACA,8DAA8D;QAC9D,OAAO,cAAc,CAAC,SAAS,QAAQ;YAAE,YAAY,IAAI;YAAE,KAAK,IAAM;QAAK;QAC3E;IACF","sources":["packages/express/src/index.ts","packages/express/src/errors.ts","packages/express/src/storage.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport type { Request, Response, NextFunction } from 'express';\nimport { Router } from 'express';\n\nimport { LogtoExpressError } from './errors';\nimport ExpressStorage from './storage';\nimport type { LogtoExpressConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext, InteractionMode } from '@logto/node';\nexport type { LogtoExpressConfig } from './types';\n\nexport type Middleware = (\n request: Request,\n response: Response,\n next: NextFunction\n) => Promise<void>;\n\nconst createNodeClient = (\n request: IncomingMessage,\n response: Response,\n config: LogtoExpressConfig\n) => {\n // We assume that `session` is configured in the express app, but need to check it there.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!request.session) {\n throw new LogtoExpressError('session_not_configured');\n }\n\n const storage = new ExpressStorage(request);\n\n return new NodeClient(config, {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\n });\n};\n\nexport const handleAuthRoutes = (config: LogtoExpressConfig): Router => {\n // eslint-disable-next-line new-cap\n const router = Router();\n\n router.use('/logto/:action', async (request, response) => {\n const { action } = request.params;\n const nodeClient = createNodeClient(request, response, config);\n\n switch (action) {\n case 'sign-in': {\n await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);\n\n break;\n }\n\n case 'sign-up': {\n await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`, 'signUp');\n\n break;\n }\n\n case 'sign-in-callback': {\n if (request.url) {\n await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);\n response.redirect(config.baseUrl);\n }\n\n break;\n }\n\n case 'sign-out': {\n await nodeClient.signOut(config.baseUrl);\n\n break;\n }\n\n default: {\n response.status(404).end();\n }\n }\n });\n\n return router;\n};\n\nexport const withLogto =\n (config: LogtoExpressConfig): Middleware =>\n async (request: IncomingMessage, response: Response, next: NextFunction) => {\n const client = createNodeClient(request, response, config);\n const user = await client.getContext({\n getAccessToken: config.getAccessToken,\n resource: config.resource,\n fetchUserInfo: config.fetchUserInfo,\n });\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n next();\n };\n","const logtoExpressErrorCodes = Object.freeze({\n session_not_configured: 'You should configure express-session before using Logto express SDK.',\n});\n\nexport type LogtoClientErrorCode = keyof typeof logtoExpressErrorCodes;\n\nexport class LogtoExpressError extends Error {\n code: LogtoClientErrorCode;\n data: unknown;\n\n constructor(code: LogtoClientErrorCode, data?: unknown) {\n super(logtoExpressErrorCodes[code]);\n this.code = code;\n this.data = data;\n }\n}\n","import type { IncomingMessage } from 'http';\n\nimport type { Storage, StorageKey } from '@logto/node';\n\nexport default class ExpressStorage implements Storage {\n constructor(private readonly request: IncomingMessage) {}\n\n async setItem(key: StorageKey, value: string) {\n this.request.session[key] = value;\n }\n\n async getItem(key: StorageKey) {\n const value = this.request.session[key];\n\n if (value === undefined) {\n return null;\n }\n\n return String(value);\n }\n\n async removeItem(key: StorageKey) {\n this.request.session[key] = undefined;\n }\n}\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../../"}
|
package/lib/module.d.mts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { GetContextParameters, LogtoConfig } from "@logto/node";
|
|
2
|
-
import { Request, Response, NextFunction, Router } from "express";
|
|
3
|
-
declare module 'http' {
|
|
4
|
-
interface IncomingMessage {
|
|
5
|
-
session: Record<string, string | undefined>;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export type LogtoExpressConfig = LogtoConfig & {
|
|
9
|
-
baseUrl: string;
|
|
10
|
-
} & GetContextParameters;
|
|
11
|
-
export { ReservedScope, UserScope } from '@logto/node';
|
|
12
|
-
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
13
|
-
export type Middleware = (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
14
|
-
export const handleAuthRoutes: (config: LogtoExpressConfig) => Router;
|
|
15
|
-
export const withLogto: (config: LogtoExpressConfig) => Middleware;
|
|
16
|
-
|
|
17
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/module.mjs
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import $imQ1Q$logtonode, {ReservedScope as $07dcc8bf26a4fe36$re_export$ReservedScope, UserScope as $07dcc8bf26a4fe36$re_export$UserScope} from "@logto/node";
|
|
2
|
-
import {Router as $imQ1Q$Router} from "express";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const $a37013145d4244c8$var$logtoExpressErrorCodes = Object.freeze({
|
|
7
|
-
session_not_configured: "You should configure express-session before using Logto express SDK."
|
|
8
|
-
});
|
|
9
|
-
class $a37013145d4244c8$export$b838f54079ce95de extends Error {
|
|
10
|
-
constructor(code, data){
|
|
11
|
-
super($a37013145d4244c8$var$logtoExpressErrorCodes[code]);
|
|
12
|
-
this.code = code;
|
|
13
|
-
this.data = data;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class $0234f3d8cdf68f71$export$2e2bcd8739ae039 {
|
|
19
|
-
constructor(request){
|
|
20
|
-
this.request = request;
|
|
21
|
-
}
|
|
22
|
-
async setItem(key, value) {
|
|
23
|
-
this.request.session[key] = value;
|
|
24
|
-
}
|
|
25
|
-
async getItem(key) {
|
|
26
|
-
const value = this.request.session[key];
|
|
27
|
-
if (value === undefined) return null;
|
|
28
|
-
return String(value);
|
|
29
|
-
}
|
|
30
|
-
async removeItem(key) {
|
|
31
|
-
this.request.session[key] = undefined;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const $07dcc8bf26a4fe36$var$createNodeClient = (request, response, config)=>{
|
|
38
|
-
// We assume that `session` is configured in the express app, but need to check it there.
|
|
39
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
40
|
-
if (!request.session) throw new (0, $a37013145d4244c8$export$b838f54079ce95de)("session_not_configured");
|
|
41
|
-
const storage = new (0, $0234f3d8cdf68f71$export$2e2bcd8739ae039)(request);
|
|
42
|
-
return new (0, $imQ1Q$logtonode)(config, {
|
|
43
|
-
storage: storage,
|
|
44
|
-
navigate: (url)=>{
|
|
45
|
-
response.redirect(url);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
const $07dcc8bf26a4fe36$export$218129a5b761c252 = (config)=>{
|
|
50
|
-
// eslint-disable-next-line new-cap
|
|
51
|
-
const router = (0, $imQ1Q$Router)();
|
|
52
|
-
router.use("/logto/:action", async (request, response)=>{
|
|
53
|
-
const { action: action } = request.params;
|
|
54
|
-
const nodeClient = $07dcc8bf26a4fe36$var$createNodeClient(request, response, config);
|
|
55
|
-
switch(action){
|
|
56
|
-
case "sign-in":
|
|
57
|
-
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);
|
|
58
|
-
break;
|
|
59
|
-
case "sign-up":
|
|
60
|
-
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`, "signUp");
|
|
61
|
-
break;
|
|
62
|
-
case "sign-in-callback":
|
|
63
|
-
if (request.url) {
|
|
64
|
-
await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);
|
|
65
|
-
response.redirect(config.baseUrl);
|
|
66
|
-
}
|
|
67
|
-
break;
|
|
68
|
-
case "sign-out":
|
|
69
|
-
await nodeClient.signOut(config.baseUrl);
|
|
70
|
-
break;
|
|
71
|
-
default:
|
|
72
|
-
response.status(404).end();
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
return router;
|
|
76
|
-
};
|
|
77
|
-
const $07dcc8bf26a4fe36$export$f9a8e793abe4305b = (config)=>async (request, response, next)=>{
|
|
78
|
-
const client = $07dcc8bf26a4fe36$var$createNodeClient(request, response, config);
|
|
79
|
-
const user = await client.getContext({
|
|
80
|
-
getAccessToken: config.getAccessToken,
|
|
81
|
-
resource: config.resource,
|
|
82
|
-
fetchUserInfo: config.fetchUserInfo
|
|
83
|
-
});
|
|
84
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
85
|
-
Object.defineProperty(request, "user", {
|
|
86
|
-
enumerable: true,
|
|
87
|
-
get: ()=>user
|
|
88
|
-
});
|
|
89
|
-
next();
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
export {$07dcc8bf26a4fe36$export$218129a5b761c252 as handleAuthRoutes, $07dcc8bf26a4fe36$export$f9a8e793abe4305b as withLogto, $07dcc8bf26a4fe36$re_export$ReservedScope as ReservedScope, $07dcc8bf26a4fe36$re_export$UserScope as UserScope};
|
|
94
|
-
//# sourceMappingURL=module.mjs.map
|
package/lib/module.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;ACAA,MAAM,+CAAyB,OAAO,MAAM,CAAC;IAC3C,wBAAwB;AAC1B;AAIO,MAAM,kDAA0B;IAIrC,YAAY,IAA0B,EAAE,IAAc,CAAE;QACtD,KAAK,CAAC,4CAAsB,CAAC,KAAK;QAClC,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,IAAI,GAAG;IACd;AACF;;;ACXe;IACb,YAA6B,QAA0B;uBAA1B;IAA2B;IAExD,MAAM,QAAQ,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;IAC9B;IAEA,MAAM,QAAQ,GAAe,EAAE;QAC7B,MAAM,QAAQ,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI;QAEvC,IAAI,UAAU,WACZ,OAAO,IAAI;QAGb,OAAO,OAAO;IAChB;IAEA,MAAM,WAAW,GAAe,EAAE;QAChC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;IAC9B;AACF;;;;AFHA,MAAM,yCAAmB,CACvB,SACA,UACA,SACG;IACH,yFAAyF;IACzF,uEAAuE;IACvE,IAAI,CAAC,QAAQ,OAAO,EAClB,MAAM,IAAI,CAAA,GAAA,yCAAgB,EAAE,0BAA0B;IAGxD,MAAM,UAAU,IAAI,CAAA,GAAA,wCAAa,EAAE;IAEnC,OAAO,IAAI,CAAA,GAAA,gBAAS,EAAE,QAAQ;iBAC5B;QACA,UAAU,CAAC,MAAQ;YACjB,SAAS,QAAQ,CAAC;QACpB;IACF;AACF;AAEO,MAAM,4CAAmB,CAAC,SAAuC;IACtE,mCAAmC;IACnC,MAAM,SAAS,CAAA,GAAA,aAAM,AAAD;IAEpB,OAAO,GAAG,CAAC,kBAAkB,OAAO,SAAS,WAAa;QACxD,MAAM,UAAE,OAAM,EAAE,GAAG,QAAQ,MAAM;QACjC,MAAM,aAAa,uCAAiB,SAAS,UAAU;QAEvD,OAAQ;YACN,KAAK;gBACH,MAAM,WAAW,MAAM,CAAC,CAAC,EAAE,OAAO,OAAO,CAAC,uBAAuB,CAAC;gBAElE,KAAM;YAGR,KAAK;gBACH,MAAM,WAAW,MAAM,CAAC,CAAC,EAAE,OAAO,OAAO,CAAC,uBAAuB,CAAC,EAAE;gBAEpE,KAAM;YAGR,KAAK;gBACH,IAAI,QAAQ,GAAG,EAAE;oBACf,MAAM,WAAW,oBAAoB,CAAC,CAAC,EAAE,OAAO,OAAO,CAAC,EAAE,QAAQ,WAAW,CAAC,CAAC;oBAC/E,SAAS,QAAQ,CAAC,OAAO,OAAO;gBAClC,CAAC;gBAED,KAAM;YAGR,KAAK;gBACH,MAAM,WAAW,OAAO,CAAC,OAAO,OAAO;gBAEvC,KAAM;YAGR;gBACE,SAAS,MAAM,CAAC,KAAK,GAAG;QAE5B;IACF;IAEA,OAAO;AACT;AAEO,MAAM,4CACX,CAAC,SACD,OAAO,SAA0B,UAAoB,OAAuB;QAC1E,MAAM,SAAS,uCAAiB,SAAS,UAAU;QACnD,MAAM,OAAO,MAAM,OAAO,UAAU,CAAC;YACnC,gBAAgB,OAAO,cAAc;YACrC,UAAU,OAAO,QAAQ;YACzB,eAAe,OAAO,aAAa;QACrC;QACA,8DAA8D;QAC9D,OAAO,cAAc,CAAC,SAAS,QAAQ;YAAE,YAAY,IAAI;YAAE,KAAK,IAAM;QAAK;QAC3E;IACF","sources":["packages/express/src/index.ts","packages/express/src/errors.ts","packages/express/src/storage.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport type { Request, Response, NextFunction } from 'express';\nimport { Router } from 'express';\n\nimport { LogtoExpressError } from './errors';\nimport ExpressStorage from './storage';\nimport type { LogtoExpressConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext, InteractionMode } from '@logto/node';\nexport type { LogtoExpressConfig } from './types';\n\nexport type Middleware = (\n request: Request,\n response: Response,\n next: NextFunction\n) => Promise<void>;\n\nconst createNodeClient = (\n request: IncomingMessage,\n response: Response,\n config: LogtoExpressConfig\n) => {\n // We assume that `session` is configured in the express app, but need to check it there.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!request.session) {\n throw new LogtoExpressError('session_not_configured');\n }\n\n const storage = new ExpressStorage(request);\n\n return new NodeClient(config, {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\n });\n};\n\nexport const handleAuthRoutes = (config: LogtoExpressConfig): Router => {\n // eslint-disable-next-line new-cap\n const router = Router();\n\n router.use('/logto/:action', async (request, response) => {\n const { action } = request.params;\n const nodeClient = createNodeClient(request, response, config);\n\n switch (action) {\n case 'sign-in': {\n await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);\n\n break;\n }\n\n case 'sign-up': {\n await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`, 'signUp');\n\n break;\n }\n\n case 'sign-in-callback': {\n if (request.url) {\n await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);\n response.redirect(config.baseUrl);\n }\n\n break;\n }\n\n case 'sign-out': {\n await nodeClient.signOut(config.baseUrl);\n\n break;\n }\n\n default: {\n response.status(404).end();\n }\n }\n });\n\n return router;\n};\n\nexport const withLogto =\n (config: LogtoExpressConfig): Middleware =>\n async (request: IncomingMessage, response: Response, next: NextFunction) => {\n const client = createNodeClient(request, response, config);\n const user = await client.getContext({\n getAccessToken: config.getAccessToken,\n resource: config.resource,\n fetchUserInfo: config.fetchUserInfo,\n });\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n next();\n };\n","const logtoExpressErrorCodes = Object.freeze({\n session_not_configured: 'You should configure express-session before using Logto express SDK.',\n});\n\nexport type LogtoClientErrorCode = keyof typeof logtoExpressErrorCodes;\n\nexport class LogtoExpressError extends Error {\n code: LogtoClientErrorCode;\n data: unknown;\n\n constructor(code: LogtoClientErrorCode, data?: unknown) {\n super(logtoExpressErrorCodes[code]);\n this.code = code;\n this.data = data;\n }\n}\n","import type { IncomingMessage } from 'http';\n\nimport type { Storage, StorageKey } from '@logto/node';\n\nexport default class ExpressStorage implements Storage {\n constructor(private readonly request: IncomingMessage) {}\n\n async setItem(key: StorageKey, value: string) {\n this.request.session[key] = value;\n }\n\n async getItem(key: StorageKey) {\n const value = this.request.session[key];\n\n if (value === undefined) {\n return null;\n }\n\n return String(value);\n }\n\n async removeItem(key: StorageKey) {\n this.request.session[key] = undefined;\n }\n}\n"],"names":[],"version":3,"file":"module.mjs.map"}
|