@logto/express 1.0.0-beta.2 → 1.0.0-beta.4
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 +44 -0
- package/lib/index.d.ts +3 -11
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +50 -64
- package/lib/index.js.map +1 -1
- package/lib/module.js +49 -59
- package/lib/module.js.map +1 -1
- package/package.json +19 -13
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Logto Express SDK
|
|
2
|
+
[](https://www.npmjs.com/package/@logto/express)
|
|
3
|
+
[](https://github.com/logto-io/js/actions/workflows/main.yml)
|
|
4
|
+
[](https://app.codecov.io/gh/logto-io/js?branch=master)
|
|
5
|
+
|
|
6
|
+
The Logto Express SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/docs/recipes/integrate-logto/express) or [docs](https://docs.logto.io/sdk/JavaScript/express/) for more information.
|
|
7
|
+
|
|
8
|
+
We also provide [集成指南](https://docs.logto.io/zh-cn/docs/recipes/integrate-logto/express/) and [文档](https://docs.logto.io/zh-cn/sdk/JavaScript/express/) in Simplified Chinese.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
### Using npm
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @logto/express
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Using yarn
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
yarn add @logto/express
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Using pnpm
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm add @logto/express
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Get sample
|
|
31
|
+
|
|
32
|
+
A sample project can be found at [Express Sample](https://github.com/logto-io/js/tree/master/packages/express-sample)
|
|
33
|
+
|
|
34
|
+
Check out the source code and try it with ease.
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
pnpm i && pnpm start
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Resources
|
|
41
|
+
|
|
42
|
+
[](https://logto.io/)
|
|
43
|
+
[](https://docs.logto.io/sdk/JavaScript/express/)
|
|
44
|
+
[](https://discord.gg/UEPaF3j5e6)
|
package/lib/index.d.ts
CHANGED
|
@@ -5,21 +5,13 @@ declare module 'http' {
|
|
|
5
5
|
session: Record<string, string | undefined>;
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
-
type LogtoExpressConfig = LogtoConfig & {
|
|
8
|
+
export type LogtoExpressConfig = LogtoConfig & {
|
|
9
9
|
baseUrl: string;
|
|
10
|
-
};
|
|
11
|
-
type WithLogtoConfig = {
|
|
12
10
|
getAccessToken?: boolean;
|
|
13
11
|
};
|
|
14
12
|
export type { LogtoContext } from '@logto/node';
|
|
15
13
|
export type Middleware = (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
handleSignIn: (redirectUri?: string) => Middleware;
|
|
19
|
-
handleSignInCallback: (redirectTo?: string) => Middleware;
|
|
20
|
-
handleSignOut: (redirectUri?: string) => Middleware;
|
|
21
|
-
handleAuthRoutes: () => Router;
|
|
22
|
-
withLogto: (config?: WithLogtoConfig) => Middleware;
|
|
23
|
-
}
|
|
14
|
+
export const handleAuthRoutes: (config: LogtoExpressConfig) => Router;
|
|
15
|
+
export const withLogto: (config: LogtoExpressConfig) => Middleware;
|
|
24
16
|
|
|
25
17
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AEEA,eAAe,MAAM,CAAC;
|
|
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;IAChB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;ACJF,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,yBAAyB,CACvB,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,YAAY,KACf,OAAO,CAAC,IAAI,CAAC,CAAC;AA6BnB,OAAO,MAAM,2BAA4B,kBAAkB,KAAG,MAqC7D,CAAC;AAEF,OAAO,MAAM,oBACF,kBAAkB,KAAG,UAO7B,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 { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport { Request, Response, NextFunction, Router } from 'express';\n\nimport { LogtoExpressError } from './errors';\nimport ExpressStorage from './storage';\nimport { LogtoExpressConfig } from './types';\n\nexport type { LogtoContext } 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(\n {\n ...config,\n persistAccessToken: config.persistAccessToken ?? true,\n },\n {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\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-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(config.getAccessToken);\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
CHANGED
|
@@ -4,16 +4,12 @@ var $jWmTQ$express = require("express");
|
|
|
4
4
|
function $parcel$interopDefault(a) {
|
|
5
5
|
return a && a.__esModule ? a.default : a;
|
|
6
6
|
}
|
|
7
|
-
function $parcel$defineInteropFlag(a) {
|
|
8
|
-
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
9
|
-
}
|
|
10
7
|
function $parcel$export(e, n, v, s) {
|
|
11
8
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
12
9
|
}
|
|
13
10
|
|
|
14
|
-
$parcel$
|
|
15
|
-
|
|
16
|
-
$parcel$export(module.exports, "default", () => $84b1ad9735b720b5$export$2e2bcd8739ae039);
|
|
11
|
+
$parcel$export(module.exports, "handleAuthRoutes", () => $84b1ad9735b720b5$export$218129a5b761c252);
|
|
12
|
+
$parcel$export(module.exports, "withLogto", () => $84b1ad9735b720b5$export$f9a8e793abe4305b);
|
|
17
13
|
|
|
18
14
|
|
|
19
15
|
const $cf736fc4f7518804$var$logtoExpressErrorCodes = Object.freeze({
|
|
@@ -35,77 +31,67 @@ class $8bf40595bbb9a575$export$2e2bcd8739ae039 {
|
|
|
35
31
|
async setItem(key, value) {
|
|
36
32
|
this.request.session[key] = value;
|
|
37
33
|
}
|
|
38
|
-
getItem(key) {
|
|
34
|
+
async getItem(key) {
|
|
39
35
|
const value = this.request.session[key];
|
|
40
36
|
if (value === undefined) return null;
|
|
41
37
|
return String(value);
|
|
42
38
|
}
|
|
43
|
-
removeItem(key) {
|
|
39
|
+
async removeItem(key) {
|
|
44
40
|
this.request.session[key] = undefined;
|
|
45
41
|
}
|
|
46
42
|
}
|
|
47
43
|
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
45
|
+
const $84b1ad9735b720b5$var$createNodeClient = (request, response, config)=>{
|
|
46
|
+
// We assume that `session` is configured in the express app, but need to check it there.
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
48
|
+
if (!request.session) throw new (0, $cf736fc4f7518804$export$b838f54079ce95de)("session_not_configured");
|
|
49
|
+
const storage = new (0, $8bf40595bbb9a575$export$2e2bcd8739ae039)(request);
|
|
50
|
+
return new (0, ($parcel$interopDefault($jWmTQ$logtonode)))({
|
|
51
|
+
...config,
|
|
52
|
+
persistAccessToken: config.persistAccessToken ?? true
|
|
53
|
+
}, {
|
|
54
|
+
storage: storage,
|
|
55
|
+
navigate: (url)=>{
|
|
56
|
+
response.redirect(url);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
const $84b1ad9735b720b5$export$218129a5b761c252 = (config)=>{
|
|
61
|
+
// eslint-disable-next-line new-cap
|
|
62
|
+
const router = (0, $jWmTQ$express.Router)();
|
|
63
|
+
router.use("/logto/:action", async (request, response)=>{
|
|
64
|
+
const { action: action } = request.params;
|
|
65
|
+
const nodeClient = $84b1ad9735b720b5$var$createNodeClient(request, response, config);
|
|
66
|
+
switch(action){
|
|
67
|
+
case "sign-in":
|
|
68
|
+
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);
|
|
69
|
+
break;
|
|
70
|
+
case "sign-in-callback":
|
|
58
71
|
if (request.url) {
|
|
59
|
-
await nodeClient.handleSignInCallback(`${
|
|
60
|
-
response.redirect(
|
|
72
|
+
await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);
|
|
73
|
+
response.redirect(config.baseUrl);
|
|
61
74
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this.handleAuthRoutes = ()=>{
|
|
68
|
-
// eslint-disable-next-line new-cap
|
|
69
|
-
const router = (0, $jWmTQ$express.Router)();
|
|
70
|
-
router.use("/logto/:action", (request, response, next)=>{
|
|
71
|
-
const { action: action } = request.params;
|
|
72
|
-
if (action === "sign-in") return this.handleSignIn()(request, response, next);
|
|
73
|
-
if (action === "sign-in-callback") return this.handleSignInCallback()(request, response, next);
|
|
74
|
-
if (action === "sign-out") return this.handleSignOut()(request, response, next);
|
|
75
|
+
break;
|
|
76
|
+
case "sign-out":
|
|
77
|
+
await nodeClient.signOut(config.baseUrl);
|
|
78
|
+
break;
|
|
79
|
+
default:
|
|
75
80
|
response.status(404).end();
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
next();
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
createNodeClient(request, response) {
|
|
91
|
-
this.checkSession(request);
|
|
92
|
-
const storage = new (0, $8bf40595bbb9a575$export$2e2bcd8739ae039)(request);
|
|
93
|
-
return new (0, ($parcel$interopDefault($jWmTQ$logtonode)))({
|
|
94
|
-
...this.config,
|
|
95
|
-
persistAccessToken: this.config.persistAccessToken ?? true
|
|
96
|
-
}, {
|
|
97
|
-
storage: storage,
|
|
98
|
-
navigate: (url)=>{
|
|
99
|
-
response.redirect(url);
|
|
100
|
-
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return router;
|
|
84
|
+
};
|
|
85
|
+
const $84b1ad9735b720b5$export$f9a8e793abe4305b = (config)=>async (request, response, next)=>{
|
|
86
|
+
const client = $84b1ad9735b720b5$var$createNodeClient(request, response, config);
|
|
87
|
+
const user = await client.getContext(config.getAccessToken);
|
|
88
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
89
|
+
Object.defineProperty(request, "user", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
get: ()=>user
|
|
101
92
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
// We assume that `session` is configured in the express app, but need to check it there.
|
|
105
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
106
|
-
if (!request.session) throw new (0, $cf736fc4f7518804$export$b838f54079ce95de)("session_not_configured");
|
|
107
|
-
}
|
|
108
|
-
}
|
|
93
|
+
next();
|
|
94
|
+
};
|
|
109
95
|
|
|
110
96
|
|
|
111
97
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;;;;AAAA;;ACAA,MAAM,4CAAsB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC3C,sBAAsB,EAAE,sEAAsE;CAC/F,CAAC,AAAC;AAII,MAAM,yCAAiB,SAAS,KAAK;IAI1C,YAAY,IAA0B,EAAE,IAAc,CAAE;QACtD,KAAK,CAAC,4CAAsB,CAAC,IAAI,CAAC,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB;CACF;;;ACXc;IACb,YAA6B,OAAwB,CAAE;aAA1B,OAAwB,GAAxB,OAAwB;KAAI;IAEzD,MAAM,OAAO,CAAC,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,K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M,CAAC,AAAC;QAE/D,OAAQ,MAAM;YACZ,KAAK,SAAS;gBACZ,MAAM,UAAU,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAC,CAAC;gBAEpE,MAAM;YAGR,KAAK,kBAAkB;gBACrB,IAAI,OAAO,CAAC,GAAG,EAAE;oBACf,MAAM,UAAU,CAAC,oBAAoB,CAAC,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;oBACjF,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;iBACnC;gBAED,MAAM;YAGR,KAAK,UAAU;gBACb,MAAM,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAEzC,MAAM;YAGR;gBACE,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;SAE9B;KACF,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;CACf,AAAC;AAEK,MAAM,yCAAS,GACpB,CAAC,MAA0B,GAC3B,OAAO,OAAwB,EAAE,QAAkB,EAAE,IAAkB,GAAK;QAC1E,MAAM,MAAM,GAAG,sCAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,AAAC;QAC3D,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,cAAc,CAAC,AAAC;QAC5D,8DAA8D;QAC9D,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE;YAAE,UAAU,EAAE,IAAI;YAAE,GAAG,EAAE,IAAM,IAAI;SAAE,CAAC,CAAC;QAC9E,IAAI,EAAE,CAAC;KACR,AAAC","sources":["packages/express/src/index.ts","packages/express/src/errors.ts","packages/express/src/storage.ts"],"sourcesContent":["import { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport { Request, Response, NextFunction, Router } from 'express';\n\nimport { LogtoExpressError } from './errors';\nimport ExpressStorage from './storage';\nimport { LogtoExpressConfig } from './types';\n\nexport type { LogtoContext } 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(\n {\n ...config,\n persistAccessToken: config.persistAccessToken ?? true,\n },\n {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\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-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(config.getAccessToken);\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 { IncomingMessage } from 'http';\n\nimport { 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.js
CHANGED
|
@@ -22,78 +22,68 @@ class $0234f3d8cdf68f71$export$2e2bcd8739ae039 {
|
|
|
22
22
|
async setItem(key, value) {
|
|
23
23
|
this.request.session[key] = value;
|
|
24
24
|
}
|
|
25
|
-
getItem(key) {
|
|
25
|
+
async getItem(key) {
|
|
26
26
|
const value = this.request.session[key];
|
|
27
27
|
if (value === undefined) return null;
|
|
28
28
|
return String(value);
|
|
29
29
|
}
|
|
30
|
-
removeItem(key) {
|
|
30
|
+
async removeItem(key) {
|
|
31
31
|
this.request.session[key] = undefined;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
const $07dcc8bf26a4fe36$var$createNodeClient = (request, response, config)=>{
|
|
37
|
+
// We assume that `session` is configured in the express app, but need to check it there.
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
39
|
+
if (!request.session) throw new (0, $a37013145d4244c8$export$b838f54079ce95de)("session_not_configured");
|
|
40
|
+
const storage = new (0, $0234f3d8cdf68f71$export$2e2bcd8739ae039)(request);
|
|
41
|
+
return new (0, $imQ1Q$logtonode)({
|
|
42
|
+
...config,
|
|
43
|
+
persistAccessToken: config.persistAccessToken ?? true
|
|
44
|
+
}, {
|
|
45
|
+
storage: storage,
|
|
46
|
+
navigate: (url)=>{
|
|
47
|
+
response.redirect(url);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const $07dcc8bf26a4fe36$export$218129a5b761c252 = (config)=>{
|
|
52
|
+
// eslint-disable-next-line new-cap
|
|
53
|
+
const router = (0, $imQ1Q$Router)();
|
|
54
|
+
router.use("/logto/:action", async (request, response)=>{
|
|
55
|
+
const { action: action } = request.params;
|
|
56
|
+
const nodeClient = $07dcc8bf26a4fe36$var$createNodeClient(request, response, config);
|
|
57
|
+
switch(action){
|
|
58
|
+
case "sign-in":
|
|
59
|
+
await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`);
|
|
60
|
+
break;
|
|
61
|
+
case "sign-in-callback":
|
|
45
62
|
if (request.url) {
|
|
46
|
-
await nodeClient.handleSignInCallback(`${
|
|
47
|
-
response.redirect(
|
|
63
|
+
await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);
|
|
64
|
+
response.redirect(config.baseUrl);
|
|
48
65
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.handleAuthRoutes = ()=>{
|
|
55
|
-
// eslint-disable-next-line new-cap
|
|
56
|
-
const router = (0, $imQ1Q$Router)();
|
|
57
|
-
router.use("/logto/:action", (request, response, next)=>{
|
|
58
|
-
const { action: action } = request.params;
|
|
59
|
-
if (action === "sign-in") return this.handleSignIn()(request, response, next);
|
|
60
|
-
if (action === "sign-in-callback") return this.handleSignInCallback()(request, response, next);
|
|
61
|
-
if (action === "sign-out") return this.handleSignOut()(request, response, next);
|
|
66
|
+
break;
|
|
67
|
+
case "sign-out":
|
|
68
|
+
await nodeClient.signOut(config.baseUrl);
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
62
71
|
response.status(404).end();
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
next();
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
createNodeClient(request, response) {
|
|
78
|
-
this.checkSession(request);
|
|
79
|
-
const storage = new (0, $0234f3d8cdf68f71$export$2e2bcd8739ae039)(request);
|
|
80
|
-
return new (0, $imQ1Q$logtonode)({
|
|
81
|
-
...this.config,
|
|
82
|
-
persistAccessToken: this.config.persistAccessToken ?? true
|
|
83
|
-
}, {
|
|
84
|
-
storage: storage,
|
|
85
|
-
navigate: (url)=>{
|
|
86
|
-
response.redirect(url);
|
|
87
|
-
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return router;
|
|
75
|
+
};
|
|
76
|
+
const $07dcc8bf26a4fe36$export$f9a8e793abe4305b = (config)=>async (request, response, next)=>{
|
|
77
|
+
const client = $07dcc8bf26a4fe36$var$createNodeClient(request, response, config);
|
|
78
|
+
const user = await client.getContext(config.getAccessToken);
|
|
79
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
80
|
+
Object.defineProperty(request, "user", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
get: ()=>user
|
|
88
83
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
// We assume that `session` is configured in the express app, but need to check it there.
|
|
92
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
93
|
-
if (!request.session) throw new (0, $a37013145d4244c8$export$b838f54079ce95de)("session_not_configured");
|
|
94
|
-
}
|
|
95
|
-
}
|
|
84
|
+
next();
|
|
85
|
+
};
|
|
96
86
|
|
|
97
87
|
|
|
98
|
-
export {$07dcc8bf26a4fe36$export$
|
|
88
|
+
export {$07dcc8bf26a4fe36$export$218129a5b761c252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AAE,MAAM,CAAC,AAAC;QAE/D,OAAQ,MAAM;YACZ,KAAK,SAAS;gBACZ,MAAM,UAAU,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAC,CAAC;gBAEpE,MAAM;YAGR,KAAK,kBAAkB;gBACrB,IAAI,OAAO,CAAC,GAAG,EAAE;oBACf,MAAM,UAAU,CAAC,oBAAoB,CAAC,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;oBACjF,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;iBACnC;gBAED,MAAM;YAGR,KAAK,UAAU;gBACb,MAAM,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAEzC,MAAM;YAGR;gBACE,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;SAE9B;KACF,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;CACf,AAAC;AAEK,MAAM,yCAAS,GACpB,CAAC,MAA0B,GAC3B,OAAO,OAAwB,EAAE,QAAkB,EAAE,IAAkB,GAAK;QAC1E,MAAM,MAAM,GAAG,sCAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,AAAC;QAC3D,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,cAAc,CAAC,AAAC;QAC5D,8DAA8D;QAC9D,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE;YAAE,UAAU,EAAE,IAAI;YAAE,GAAG,EAAE,IAAM,IAAI;SAAE,CAAC,CAAC;QAC9E,IAAI,EAAE,CAAC;KACR,AAAC","sources":["packages/express/src/index.ts","packages/express/src/errors.ts","packages/express/src/storage.ts"],"sourcesContent":["import { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport { Request, Response, NextFunction, Router } from 'express';\n\nimport { LogtoExpressError } from './errors';\nimport ExpressStorage from './storage';\nimport { LogtoExpressConfig } from './types';\n\nexport type { LogtoContext } 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(\n {\n ...config,\n persistAccessToken: config.persistAccessToken ?? true,\n },\n {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\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-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(config.getAccessToken);\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 { IncomingMessage } from 'http';\n\nimport { 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.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/express",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -29,28 +29,34 @@
|
|
|
29
29
|
"prepack": "pnpm test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@logto/node": "^1.0.0-beta.
|
|
32
|
+
"@logto/node": "^1.0.0-beta.4"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@jest/types": "^27.5.1",
|
|
36
|
-
"@parcel/core": "^2.
|
|
37
|
-
"@parcel/packager-ts": "^2.
|
|
38
|
-
"@parcel/transformer-typescript-types": "^2.
|
|
39
|
-
"@silverhand/eslint-config": "^0.
|
|
40
|
-
"@silverhand/ts-config": "^0.
|
|
41
|
-
"@silverhand/ts-config-react": "^0.
|
|
36
|
+
"@parcel/core": "^2.7.0",
|
|
37
|
+
"@parcel/packager-ts": "^2.7.0",
|
|
38
|
+
"@parcel/transformer-typescript-types": "^2.7.0",
|
|
39
|
+
"@silverhand/eslint-config": "^1.0.0",
|
|
40
|
+
"@silverhand/ts-config": "^1.0.0",
|
|
41
|
+
"@silverhand/ts-config-react": "^1.0.0",
|
|
42
|
+
"@types/cookie-parser": "^1.4.3",
|
|
42
43
|
"@types/express": "^4.17.13",
|
|
44
|
+
"@types/express-session": "^1.17.5",
|
|
43
45
|
"@types/jest": "^27.4.0",
|
|
44
|
-
"
|
|
46
|
+
"@types/supertest": "^2.0.12",
|
|
47
|
+
"cookie-parser": "^1.4.6",
|
|
48
|
+
"eslint": "^8.23.0",
|
|
45
49
|
"express": "^4.18.1",
|
|
50
|
+
"express-session": "^1.17.3",
|
|
46
51
|
"jest": "^27.5.1",
|
|
47
52
|
"jest-location-mock": "^1.0.9",
|
|
48
53
|
"jest-matcher-specific-error": "^1.0.0",
|
|
49
54
|
"lint-staged": "^13.0.0",
|
|
50
|
-
"parcel": "^2.
|
|
51
|
-
"prettier": "^2.
|
|
55
|
+
"parcel": "^2.7.0",
|
|
56
|
+
"prettier": "^2.7.1",
|
|
57
|
+
"supertest": "^6.2.4",
|
|
52
58
|
"ts-jest": "^27.0.4",
|
|
53
|
-
"typescript": "
|
|
59
|
+
"typescript": "4.7.4"
|
|
54
60
|
},
|
|
55
61
|
"peerDependencies": {
|
|
56
62
|
"express": ">=4"
|
|
@@ -70,5 +76,5 @@
|
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
},
|
|
73
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "8b0f8947ac211fd8d4b6e2e00ce6acfc9dd344db"
|
|
74
80
|
}
|