@logto/express 1.0.0-beta.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/LICENSE +21 -0
- package/lib/index.d.ts +25 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +111 -0
- package/lib/index.js.map +1 -0
- package/lib/module.js +99 -0
- package/lib/module.js.map +1 -0
- package/package.json +74 -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/lib/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
type LogtoExpressConfig = LogtoConfig & {
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
};
|
|
11
|
+
type WithLogtoConfig = {
|
|
12
|
+
getAccessToken?: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type { LogtoContext } from '@logto/node';
|
|
15
|
+
export type Middleware = (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
16
|
+
export default class LogtoClient {
|
|
17
|
+
constructor(config: LogtoExpressConfig);
|
|
18
|
+
handleSignIn: (redirectUri?: string) => Middleware;
|
|
19
|
+
handleSignInCallback: (redirectTo?: string) => Middleware;
|
|
20
|
+
handleSignOut: (redirectUri?: string) => Middleware;
|
|
21
|
+
handleAuthRoutes: () => Router;
|
|
22
|
+
withLogto: (config?: WithLogtoConfig) => Middleware;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;AEEA,eAAe,MAAM,CAAC;IACpB,UAAU,eAAe;QACvB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;KAC7C;CACF;AAED,0BAAiC,WAAW,GAAG;IAC7C,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,uBAA8B;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;ACLF,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,yBAAyB,CACvB,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,YAAY,KACf,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB;gBAC+B,MAAM,EAAE,kBAAkB;IAEvD,YAAY,4BACuD,UAAU,CAIzE;IAEJ,oBAAoB,2BACkB,UAAU,CAQ5C;IAEJ,aAAa,4BAC0B,UAAU,CAI7C;IAEJ,gBAAgB,QAAO,MAAM,CAuB3B;IAEF,SAAS,YACE,eAAe,KAAQ,UAAU,CAOxC;CA2BL","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, WithLogtoConfig } from './types';\n\nexport type { LogtoContext } from '@logto/node';\n\nexport type Middleware = (\n request: Request,\n response: Response,\n next: NextFunction\n) => Promise<void>;\n\nexport default class LogtoClient {\n constructor(private readonly config: LogtoExpressConfig) {}\n\n handleSignIn =\n (redirectUri = `${this.config.baseUrl}/logto/sign-in-callback`): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n await nodeClient.signIn(redirectUri);\n };\n\n handleSignInCallback =\n (redirectTo = this.config.baseUrl): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.originalUrl}`);\n response.redirect(redirectTo);\n }\n };\n\n handleSignOut =\n (redirectUri = this.config.baseUrl): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n await nodeClient.signOut(redirectUri);\n };\n\n handleAuthRoutes = (): Router => {\n // eslint-disable-next-line new-cap\n const router = Router();\n\n router.use('/logto/:action', (request, response, next) => {\n const { action } = request.params;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response, next);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response, next);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response, next);\n }\n\n response.status(404).end();\n });\n\n return router;\n };\n\n withLogto =\n (config: WithLogtoConfig = {}): Middleware =>\n async (request: IncomingMessage, response: Response, next: NextFunction) => {\n const client = this.createNodeClient(request, response);\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\n private createNodeClient(request: IncomingMessage, response: Response) {\n this.checkSession(request);\n const storage = new ExpressStorage(request);\n\n return new NodeClient(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\n }\n );\n }\n\n private checkSession(request: IncomingMessage) {\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}\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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$defineInteropFlag(a) {
|
|
8
|
+
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
9
|
+
}
|
|
10
|
+
function $parcel$export(e, n, v, s) {
|
|
11
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
$parcel$defineInteropFlag(module.exports);
|
|
15
|
+
|
|
16
|
+
$parcel$export(module.exports, "default", () => $84b1ad9735b720b5$export$2e2bcd8739ae039);
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const $cf736fc4f7518804$var$logtoExpressErrorCodes = Object.freeze({
|
|
20
|
+
session_not_configured: "You should configure express-session before using Logto express SDK."
|
|
21
|
+
});
|
|
22
|
+
class $cf736fc4f7518804$export$b838f54079ce95de extends Error {
|
|
23
|
+
constructor(code, data){
|
|
24
|
+
super($cf736fc4f7518804$var$logtoExpressErrorCodes[code]);
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.data = data;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class $8bf40595bbb9a575$export$2e2bcd8739ae039 {
|
|
32
|
+
constructor(request){
|
|
33
|
+
this.request = request;
|
|
34
|
+
}
|
|
35
|
+
async setItem(key, value) {
|
|
36
|
+
this.request.session[key] = value;
|
|
37
|
+
}
|
|
38
|
+
getItem(key) {
|
|
39
|
+
const value = this.request.session[key];
|
|
40
|
+
if (value === undefined) return null;
|
|
41
|
+
return String(value);
|
|
42
|
+
}
|
|
43
|
+
removeItem(key) {
|
|
44
|
+
this.request.session[key] = undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class $84b1ad9735b720b5$export$2e2bcd8739ae039 {
|
|
50
|
+
constructor(config1){
|
|
51
|
+
this.config = config1;
|
|
52
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/logto/sign-in-callback`)=>async (request, response)=>{
|
|
53
|
+
const nodeClient = this.createNodeClient(request, response);
|
|
54
|
+
await nodeClient.signIn(redirectUri);
|
|
55
|
+
};
|
|
56
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>async (request, response)=>{
|
|
57
|
+
const nodeClient = this.createNodeClient(request, response);
|
|
58
|
+
if (request.url) {
|
|
59
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.originalUrl}`);
|
|
60
|
+
response.redirect(redirectTo);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl)=>async (request, response)=>{
|
|
64
|
+
const nodeClient = this.createNodeClient(request, response);
|
|
65
|
+
await nodeClient.signOut(redirectUri);
|
|
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
|
+
response.status(404).end();
|
|
76
|
+
});
|
|
77
|
+
return router;
|
|
78
|
+
};
|
|
79
|
+
this.withLogto = (config = {})=>async (request, response, next)=>{
|
|
80
|
+
const client = this.createNodeClient(request, response);
|
|
81
|
+
const user = await client.getContext(config.getAccessToken);
|
|
82
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
83
|
+
Object.defineProperty(request, "user", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
get: ()=>user
|
|
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
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
checkSession(request) {
|
|
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
|
+
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AY,CAAC,OAAwB,EAAE;QAC7C,yFAAyF;QACzF,uEAAuE;QACvE,IAAI,CAAC,OAAO,CAAC,OAAO,EAClB,MAAM,IAAI,CAAA,GAAA,yCAAiB,CAAA,CAAC,wBAAwB,CAAC,CAAC;KAEzD;CACF","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, WithLogtoConfig } from './types';\n\nexport type { LogtoContext } from '@logto/node';\n\nexport type Middleware = (\n request: Request,\n response: Response,\n next: NextFunction\n) => Promise<void>;\n\nexport default class LogtoClient {\n constructor(private readonly config: LogtoExpressConfig) {}\n\n handleSignIn =\n (redirectUri = `${this.config.baseUrl}/logto/sign-in-callback`): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n await nodeClient.signIn(redirectUri);\n };\n\n handleSignInCallback =\n (redirectTo = this.config.baseUrl): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.originalUrl}`);\n response.redirect(redirectTo);\n }\n };\n\n handleSignOut =\n (redirectUri = this.config.baseUrl): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n await nodeClient.signOut(redirectUri);\n };\n\n handleAuthRoutes = (): Router => {\n // eslint-disable-next-line new-cap\n const router = Router();\n\n router.use('/logto/:action', (request, response, next) => {\n const { action } = request.params;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response, next);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response, next);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response, next);\n }\n\n response.status(404).end();\n });\n\n return router;\n };\n\n withLogto =\n (config: WithLogtoConfig = {}): Middleware =>\n async (request: IncomingMessage, response: Response, next: NextFunction) => {\n const client = this.createNodeClient(request, response);\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\n private createNodeClient(request: IncomingMessage, response: Response) {\n this.checkSession(request);\n const storage = new ExpressStorage(request);\n\n return new NodeClient(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\n }\n );\n }\n\n private checkSession(request: IncomingMessage) {\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}\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 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 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,99 @@
|
|
|
1
|
+
import $imQ1Q$logtonode 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
|
+
getItem(key) {
|
|
26
|
+
const value = this.request.session[key];
|
|
27
|
+
if (value === undefined) return null;
|
|
28
|
+
return String(value);
|
|
29
|
+
}
|
|
30
|
+
removeItem(key) {
|
|
31
|
+
this.request.session[key] = undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class $07dcc8bf26a4fe36$export$2e2bcd8739ae039 {
|
|
37
|
+
constructor(config1){
|
|
38
|
+
this.config = config1;
|
|
39
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/logto/sign-in-callback`)=>async (request, response)=>{
|
|
40
|
+
const nodeClient = this.createNodeClient(request, response);
|
|
41
|
+
await nodeClient.signIn(redirectUri);
|
|
42
|
+
};
|
|
43
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>async (request, response)=>{
|
|
44
|
+
const nodeClient = this.createNodeClient(request, response);
|
|
45
|
+
if (request.url) {
|
|
46
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.originalUrl}`);
|
|
47
|
+
response.redirect(redirectTo);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl)=>async (request, response)=>{
|
|
51
|
+
const nodeClient = this.createNodeClient(request, response);
|
|
52
|
+
await nodeClient.signOut(redirectUri);
|
|
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);
|
|
62
|
+
response.status(404).end();
|
|
63
|
+
});
|
|
64
|
+
return router;
|
|
65
|
+
};
|
|
66
|
+
this.withLogto = (config = {})=>async (request, response, next)=>{
|
|
67
|
+
const client = this.createNodeClient(request, response);
|
|
68
|
+
const user = await client.getContext(config.getAccessToken);
|
|
69
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
70
|
+
Object.defineProperty(request, "user", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
get: ()=>user
|
|
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
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
checkSession(request) {
|
|
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
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
export {$07dcc8bf26a4fe36$export$2e2bcd8739ae039 as default};
|
|
99
|
+
//# 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,OAAO,CAAC,GAAe,EAAE;QACvB,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,UAAU,CAAC,GAAe,EAAE;QAC1B,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;KACvC;CACF;;;AFPc;IACb,YAA6B,OAA0B,CAAE;aAA5B,MAA0B,GAA1B,OAA0B;aAEvD,YAAY,GACV,CAAC,WAAW,GAAG,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAuB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,QAAQ,CAAC,GAAG,CAAC,CAAC;aACxB;SACF,CACF,CAAC;KACH;IAED,AAAQ,YAAY,CAAC,OAAwB,EAAE;QAC7C,yFAAyF;QACzF,uEAAuE;QACvE,IAAI,CAAC,OAAO,CAAC,OAAO,EAClB,MAAM,IAAI,CAAA,GAAA,yCAAiB,CAAA,CAAC,wBAAwB,CAAC,CAAC;KAEzD;CACF","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, WithLogtoConfig } from './types';\n\nexport type { LogtoContext } from '@logto/node';\n\nexport type Middleware = (\n request: Request,\n response: Response,\n next: NextFunction\n) => Promise<void>;\n\nexport default class LogtoClient {\n constructor(private readonly config: LogtoExpressConfig) {}\n\n handleSignIn =\n (redirectUri = `${this.config.baseUrl}/logto/sign-in-callback`): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n await nodeClient.signIn(redirectUri);\n };\n\n handleSignInCallback =\n (redirectTo = this.config.baseUrl): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.originalUrl}`);\n response.redirect(redirectTo);\n }\n };\n\n handleSignOut =\n (redirectUri = this.config.baseUrl): Middleware =>\n async (request, response) => {\n const nodeClient = this.createNodeClient(request, response);\n await nodeClient.signOut(redirectUri);\n };\n\n handleAuthRoutes = (): Router => {\n // eslint-disable-next-line new-cap\n const router = Router();\n\n router.use('/logto/:action', (request, response, next) => {\n const { action } = request.params;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response, next);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response, next);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response, next);\n }\n\n response.status(404).end();\n });\n\n return router;\n };\n\n withLogto =\n (config: WithLogtoConfig = {}): Middleware =>\n async (request: IncomingMessage, response: Response, next: NextFunction) => {\n const client = this.createNodeClient(request, response);\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\n private createNodeClient(request: IncomingMessage, response: Response) {\n this.checkSession(request);\n const storage = new ExpressStorage(request);\n\n return new NodeClient(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage,\n navigate: (url) => {\n response.redirect(url);\n },\n }\n );\n }\n\n private checkSession(request: IncomingMessage) {\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}\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 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 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,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@logto/express",
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
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.2"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@jest/types": "^27.5.1",
|
|
36
|
+
"@parcel/core": "^2.6.2",
|
|
37
|
+
"@parcel/packager-ts": "^2.6.2",
|
|
38
|
+
"@parcel/transformer-typescript-types": "^2.6.2",
|
|
39
|
+
"@silverhand/eslint-config": "^0.17.0",
|
|
40
|
+
"@silverhand/ts-config": "^0.14.0",
|
|
41
|
+
"@silverhand/ts-config-react": "^0.14.0",
|
|
42
|
+
"@types/express": "^4.17.13",
|
|
43
|
+
"@types/jest": "^27.4.0",
|
|
44
|
+
"eslint": "^8.9.0",
|
|
45
|
+
"express": "^4.18.1",
|
|
46
|
+
"jest": "^27.5.1",
|
|
47
|
+
"jest-location-mock": "^1.0.9",
|
|
48
|
+
"jest-matcher-specific-error": "^1.0.0",
|
|
49
|
+
"lint-staged": "^13.0.0",
|
|
50
|
+
"parcel": "^2.6.2",
|
|
51
|
+
"prettier": "^2.3.2",
|
|
52
|
+
"ts-jest": "^27.0.4",
|
|
53
|
+
"typescript": "^4.5.5"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"express": ">=4"
|
|
57
|
+
},
|
|
58
|
+
"eslintConfig": {
|
|
59
|
+
"extends": "@silverhand"
|
|
60
|
+
},
|
|
61
|
+
"prettier": "@silverhand/eslint-config/.prettierrc",
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public"
|
|
64
|
+
},
|
|
65
|
+
"targets": {
|
|
66
|
+
"main": {
|
|
67
|
+
"context": "node",
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": "^16"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
"gitHead": "212891497b04e3a5fd6b24bbbeec227dfec8ae53"
|
|
74
|
+
}
|