@logto/express 1.0.0-beta.10
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/LICENSE +21 -0
- package/README.md +44 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +97 -0
- package/lib/index.js.map +1 -0
- package/lib/module.js +87 -0
- package/lib/module.js.map +1 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Silverhand
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { 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
|
+
getAccessToken?: boolean;
|
|
11
|
+
fetchUserInfo?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
14
|
+
export type { LogtoContext } from '@logto/node';
|
|
15
|
+
export type Middleware = (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
16
|
+
export const handleAuthRoutes: (config: LogtoExpressConfig) => Router;
|
|
17
|
+
export const withLogto: (config: LogtoExpressConfig) => Middleware;
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;IACzB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;ACLF,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvD,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;AAuBnB,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 { ReservedScope, UserScope } from '@logto/node';\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(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-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, config.fetchUserInfo);\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
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
var $jWmTQ$logtonode = require("@logto/node");
|
|
2
|
+
var $jWmTQ$express = require("express");
|
|
3
|
+
|
|
4
|
+
function $parcel$interopDefault(a) {
|
|
5
|
+
return a && a.__esModule ? a.default : a;
|
|
6
|
+
}
|
|
7
|
+
function $parcel$export(e, n, v, s) {
|
|
8
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
$parcel$export(module.exports, "handleAuthRoutes", () => $84b1ad9735b720b5$export$218129a5b761c252);
|
|
12
|
+
$parcel$export(module.exports, "withLogto", () => $84b1ad9735b720b5$export$f9a8e793abe4305b);
|
|
13
|
+
$parcel$export(module.exports, "ReservedScope", () => $84b1ad9735b720b5$re_export$ReservedScope);
|
|
14
|
+
$parcel$export(module.exports, "UserScope", () => $84b1ad9735b720b5$re_export$UserScope);
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const $cf736fc4f7518804$var$logtoExpressErrorCodes = Object.freeze({
|
|
18
|
+
session_not_configured: "You should configure express-session before using Logto express SDK."
|
|
19
|
+
});
|
|
20
|
+
class $cf736fc4f7518804$export$b838f54079ce95de extends Error {
|
|
21
|
+
constructor(code, data){
|
|
22
|
+
super($cf736fc4f7518804$var$logtoExpressErrorCodes[code]);
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.data = data;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class $8bf40595bbb9a575$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 $84b1ad9735b720b5$var$createNodeClient = (request, response, config)=>{
|
|
49
|
+
// We assume that `session` is configured in the express app, but need to check it there.
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
51
|
+
if (!request.session) throw new (0, $cf736fc4f7518804$export$b838f54079ce95de)("session_not_configured");
|
|
52
|
+
const storage = new (0, $8bf40595bbb9a575$export$2e2bcd8739ae039)(request);
|
|
53
|
+
return new (0, ($parcel$interopDefault($jWmTQ$logtonode)))(config, {
|
|
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":
|
|
71
|
+
if (request.url) {
|
|
72
|
+
await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);
|
|
73
|
+
response.redirect(config.baseUrl);
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
case "sign-out":
|
|
77
|
+
await nodeClient.signOut(config.baseUrl);
|
|
78
|
+
break;
|
|
79
|
+
default:
|
|
80
|
+
response.status(404).end();
|
|
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, config.fetchUserInfo);
|
|
88
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
89
|
+
Object.defineProperty(request, "user", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
get: ()=>user
|
|
92
|
+
});
|
|
93
|
+
next();
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
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,KAAK,CAAC;KACnC;IAED,MAAM,OAAO,CAAC,GAAe,EAAE;QAC7B,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,AAAC;QAExC,IAAI,KAAK,KAAK,SAAS,EACrB,OAAO,IAAI,CAAC;QAGd,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;KACtB;IAED,MAAM,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,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,EAAE,MAAM,CAAC,aAAa,CAAC,AAAC;QAClF,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 { ReservedScope, UserScope } from '@logto/node';\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(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-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, config.fetchUserInfo);\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
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
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-in-callback":
|
|
60
|
+
if (request.url) {
|
|
61
|
+
await nodeClient.handleSignInCallback(`${config.baseUrl}${request.originalUrl}`);
|
|
62
|
+
response.redirect(config.baseUrl);
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
case "sign-out":
|
|
66
|
+
await nodeClient.signOut(config.baseUrl);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
response.status(404).end();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return router;
|
|
73
|
+
};
|
|
74
|
+
const $07dcc8bf26a4fe36$export$f9a8e793abe4305b = (config)=>async (request, response, next)=>{
|
|
75
|
+
const client = $07dcc8bf26a4fe36$var$createNodeClient(request, response, config);
|
|
76
|
+
const user = await client.getContext(config.getAccessToken, config.fetchUserInfo);
|
|
77
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
78
|
+
Object.defineProperty(request, "user", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: ()=>user
|
|
81
|
+
});
|
|
82
|
+
next();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
export {$07dcc8bf26a4fe36$export$218129a5b761c252 as handleAuthRoutes, $07dcc8bf26a4fe36$export$f9a8e793abe4305b as withLogto, $07dcc8bf26a4fe36$re_export$ReservedScope as ReservedScope, $07dcc8bf26a4fe36$re_export$UserScope as UserScope};
|
|
87
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,KAAK,CAAC;KACnC;IAED,MAAM,OAAO,CAAC,GAAe,EAAE;QAC7B,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,AAAC;QAExC,IAAI,KAAK,KAAK,SAAS,EACrB,OAAO,IAAI,CAAC;QAGd,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;KACtB;IAED,MAAM,UAAU,CAAC,GAAe,EAAE;QAChC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS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AAO,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,EAAE,MAAM,CAAC,aAAa,CAAC,AAAC;QAClF,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 { ReservedScope, UserScope } from '@logto/node';\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(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-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, config.fetchUserInfo);\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
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@logto/express",
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
|
+
"source": "./src/index.ts",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"require": "./lib/index.js",
|
|
8
|
+
"import": "./lib/module.js"
|
|
9
|
+
},
|
|
10
|
+
"module": "./lib/module.js",
|
|
11
|
+
"types": "./lib/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"lib"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/logto-io/js.git",
|
|
19
|
+
"directory": "packages/express"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
23
|
+
"precommit": "lint-staged",
|
|
24
|
+
"check": "tsc --noEmit",
|
|
25
|
+
"build": "rm -rf lib/ && pnpm check && parcel build",
|
|
26
|
+
"lint": "eslint --ext .ts src",
|
|
27
|
+
"test": "jest",
|
|
28
|
+
"test:coverage": "jest --silent --coverage",
|
|
29
|
+
"prepack": "pnpm test"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@logto/node": "^1.0.0-beta.10"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@jest/types": "^27.5.1",
|
|
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",
|
|
43
|
+
"@types/express": "^4.17.13",
|
|
44
|
+
"@types/express-session": "^1.17.5",
|
|
45
|
+
"@types/jest": "^27.4.0",
|
|
46
|
+
"@types/supertest": "^2.0.12",
|
|
47
|
+
"cookie-parser": "^1.4.6",
|
|
48
|
+
"eslint": "^8.23.0",
|
|
49
|
+
"express": "^4.18.1",
|
|
50
|
+
"express-session": "^1.17.3",
|
|
51
|
+
"jest": "^27.5.1",
|
|
52
|
+
"jest-location-mock": "^1.0.9",
|
|
53
|
+
"jest-matcher-specific-error": "^1.0.0",
|
|
54
|
+
"lint-staged": "^13.0.0",
|
|
55
|
+
"parcel": "^2.7.0",
|
|
56
|
+
"prettier": "^2.7.1",
|
|
57
|
+
"supertest": "^6.2.4",
|
|
58
|
+
"ts-jest": "^27.0.4",
|
|
59
|
+
"typescript": "4.7.4"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"express": ">=4"
|
|
63
|
+
},
|
|
64
|
+
"eslintConfig": {
|
|
65
|
+
"extends": "@silverhand"
|
|
66
|
+
},
|
|
67
|
+
"prettier": "@silverhand/eslint-config/.prettierrc",
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
},
|
|
71
|
+
"targets": {
|
|
72
|
+
"main": {
|
|
73
|
+
"context": "node",
|
|
74
|
+
"engines": {
|
|
75
|
+
"node": "^16"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"gitHead": "c79c3759b7f15c44bf5b476dcebd82e9c96e9dd8"
|
|
80
|
+
}
|