@logto/next 1.0.0-beta.8 → 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/lib/index.d.ts +5 -13
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +20 -20
- package/lib/index.js.map +1 -1
- package/lib/module.d.mts +30 -0
- package/lib/{module.js → module.mjs} +8 -8
- package/lib/module.mjs.map +1 -0
- package/package.json +11 -11
- package/lib/module.js.map +0 -1
package/README.md
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
[](https://github.com/logto-io/js/actions/workflows/main.yml)
|
|
4
4
|
[](https://app.codecov.io/gh/logto-io/js?branch=master)
|
|
5
5
|
|
|
6
|
-
The Logto Next.js SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/docs/recipes/integrate-logto/next) or [docs](https://docs.logto.io/sdk/JavaScript/next/) for more information.
|
|
6
|
+
The Logto Next.js SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/docs/recipes/integrate-logto/next-js) or [docs](https://docs.logto.io/sdk/JavaScript/next/) for more information.
|
|
7
7
|
|
|
8
|
-
We also provide [集成指南](https://docs.logto.io/zh-cn/docs/recipes/integrate-logto/next/) and [文档](https://docs.logto.io/zh-cn/sdk/JavaScript/next/) in Simplified Chinese.
|
|
8
|
+
We also provide [集成指南](https://docs.logto.io/zh-cn/docs/recipes/integrate-logto/next-js/) and [文档](https://docs.logto.io/zh-cn/sdk/JavaScript/next/) in Simplified Chinese.
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
@@ -33,7 +33,7 @@ A sample project can be found at [Next.js Sample](https://github.com/logto-io/js
|
|
|
33
33
|
|
|
34
34
|
Check out the source code and try it with ease.
|
|
35
35
|
|
|
36
|
-
```
|
|
36
|
+
```bash
|
|
37
37
|
pnpm i && pnpm start
|
|
38
38
|
```
|
|
39
39
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LogtoConfig } from "@logto/node";
|
|
1
|
+
import { LogtoConfig, GetContextParameters } from "@logto/node";
|
|
2
2
|
import { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from "next";
|
|
3
3
|
declare module 'iron-session' {
|
|
4
4
|
interface IronSessionData {
|
|
@@ -13,14 +13,6 @@ type LogtoNextConfig = LogtoConfig & {
|
|
|
13
13
|
cookieSecure: boolean;
|
|
14
14
|
baseUrl: string;
|
|
15
15
|
};
|
|
16
|
-
/**
|
|
17
|
-
* @getAccessToken: if set to true, will try to get an access token and attach to req.user,
|
|
18
|
-
* if unable to grant an access token, will set req.user.isAuthenticated to false,
|
|
19
|
-
* this can make sure the refresh token is not revoked and still valid, so is considered more secure.
|
|
20
|
-
*/
|
|
21
|
-
type WithLogtoConfig = {
|
|
22
|
-
getAccessToken?: boolean;
|
|
23
|
-
};
|
|
24
16
|
export { ReservedScope, UserScope } from '@logto/node';
|
|
25
17
|
export type { LogtoContext } from '@logto/node';
|
|
26
18
|
export default class LogtoClient {
|
|
@@ -28,10 +20,10 @@ export default class LogtoClient {
|
|
|
28
20
|
handleSignIn: (redirectUri?: string) => NextApiHandler;
|
|
29
21
|
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
30
22
|
handleSignOut: (redirectUri?: string) => NextApiHandler;
|
|
31
|
-
handleUser: (
|
|
32
|
-
handleAuthRoutes: (configs?:
|
|
33
|
-
withLogtoApiRoute: (handler: NextApiHandler, config?:
|
|
34
|
-
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,
|
|
23
|
+
handleUser: (configs?: GetContextParameters) => NextApiHandler<any>;
|
|
24
|
+
handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
|
|
25
|
+
withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters) => NextApiHandler;
|
|
26
|
+
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters) => (context: GetServerSidePropsContext<import("querystring").ParsedUrlQuery, import("next").PreviewData>) => Promise<GetServerSidePropsResult<P>>;
|
|
35
27
|
private get ironSessionConfigs();
|
|
36
28
|
}
|
|
37
29
|
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;ACMA,eAAe,cAAc,CAAC;IAG5B,UAAU,eAAe;QACvB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;CACF;AAED,uBAA8B,WAAW,GAAG;IAC1C,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;
|
|
1
|
+
{"mappings":";;ACMA,eAAe,cAAc,CAAC;IAG5B,UAAU,eAAe;QACvB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;CACF;AAED,uBAA8B,WAAW,GAAG;IAC1C,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;ACZF,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvD,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD;gBAG+B,MAAM,EAAE,eAAe;IAEpD,YAAY,4BAET,cAAc,CASa;IAE9B,oBAAoB,2BAAuC,cAAc,CAS3C;IAE9B,aAAa,4BAAwC,cAAc,CAWrC;IAE9B,UAAU,aAAc,oBAAoB,yBAG9B;IAEd,gBAAgB,aACH,oBAAoB,KAAG,cAAc,CAqB9C;IAEJ,iBAAiB,YACN,cAAc,WACf,oBAAoB,KAC3B,cAAc,CAQa;IAE9B,YAAY,mFAEC,yBAAyB,mFAE3B,oBAAoB,oJAQD;IAa9B,OAAO,KAAK,kBAAkB,GAS7B;CAOF","sources":["packages/next/src/src/storage.ts","packages/next/src/src/types.ts","packages/next/src/src/index.ts","packages/next/src/index.ts"],"sourcesContent":[null,null,null,"import { IncomingMessage } from 'http';\n\nimport NodeClient, { GetContextParameters } from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext } from '@logto/node';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (\n redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri);\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleSignInCallback = (redirectTo = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);\n await this.storage?.save();\n response.redirect(redirectTo);\n }\n }, this.ironSessionConfigs);\n\n handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signOut(redirectUri);\n\n request.session.destroy();\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleUser = (configs?: GetContextParameters) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, configs);\n\n handleAuthRoutes =\n (configs?: GetContextParameters): NextApiHandler =>\n (request, response) => {\n const { action } = request.query;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response);\n }\n\n if (action === 'user') {\n return this.handleUser(configs)(request, response);\n }\n\n response.status(404).end();\n };\n\n withLogtoApiRoute = (\n handler: NextApiHandler,\n config: GetContextParameters = {}\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config);\n\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n\n return handler(request, response);\n }, this.ironSessionConfigs);\n\n withLogtoSsr = <P extends Record<string, unknown> = Record<string, unknown>>(\n handler: (\n context: GetServerSidePropsContext\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n configs: GetContextParameters = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, configs);\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });\n\n return handler(context);\n }, this.ironSessionConfigs);\n\n private createNodeClient(request: IncomingMessage) {\n this.storage = new NextStorage(request);\n\n return new NodeClient(this.config, {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\n });\n }\n\n private get ironSessionConfigs() {\n return {\n cookieName: `logto:${this.config.appId}`,\n password: this.config.cookieSecret,\n cookieOptions: {\n secure: this.config.cookieSecure,\n maxAge: 14 * 24 * 60 * 60,\n },\n };\n }\n\n private async getLogtoUserFromRequest(request: IncomingMessage, configs: GetContextParameters) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(configs);\n }\n}\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var $
|
|
2
|
-
var $
|
|
1
|
+
var $gpzKJ$logtonode = require("@logto/node");
|
|
2
|
+
var $gpzKJ$ironsessionnext = require("iron-session/next");
|
|
3
3
|
|
|
4
4
|
function $parcel$interopDefault(a) {
|
|
5
5
|
return a && a.__esModule ? a.default : a;
|
|
@@ -13,12 +13,12 @@ function $parcel$export(e, n, v, s) {
|
|
|
13
13
|
|
|
14
14
|
$parcel$defineInteropFlag(module.exports);
|
|
15
15
|
|
|
16
|
-
$parcel$export(module.exports, "default", () => $
|
|
17
|
-
$parcel$export(module.exports, "ReservedScope", () => $
|
|
18
|
-
$parcel$export(module.exports, "UserScope", () => $
|
|
16
|
+
$parcel$export(module.exports, "default", () => $dee2a34a598960d9$export$2e2bcd8739ae039);
|
|
17
|
+
$parcel$export(module.exports, "ReservedScope", () => $dee2a34a598960d9$re_export$ReservedScope);
|
|
18
|
+
$parcel$export(module.exports, "UserScope", () => $dee2a34a598960d9$re_export$UserScope);
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
class $
|
|
21
|
+
class $f80b46e338398246$export$2e2bcd8739ae039 {
|
|
22
22
|
constructor(request){
|
|
23
23
|
this.request = request;
|
|
24
24
|
this.sessionChanged = false;
|
|
@@ -45,16 +45,16 @@ class $1abf01985e812f74$export$2e2bcd8739ae039 {
|
|
|
45
45
|
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
class $
|
|
48
|
+
class $dee2a34a598960d9$export$2e2bcd8739ae039 {
|
|
49
49
|
constructor(config){
|
|
50
50
|
this.config = config;
|
|
51
|
-
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`)=>(0, $
|
|
51
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`)=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
52
52
|
const nodeClient = this.createNodeClient(request);
|
|
53
53
|
await nodeClient.signIn(redirectUri);
|
|
54
54
|
await this.storage?.save();
|
|
55
55
|
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
56
56
|
}, this.ironSessionConfigs);
|
|
57
|
-
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>(0, $
|
|
57
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
58
58
|
const nodeClient = this.createNodeClient(request);
|
|
59
59
|
if (request.url) {
|
|
60
60
|
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
@@ -62,16 +62,16 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
|
|
|
62
62
|
response.redirect(redirectTo);
|
|
63
63
|
}
|
|
64
64
|
}, this.ironSessionConfigs);
|
|
65
|
-
this.handleSignOut = (redirectUri = this.config.baseUrl)=>(0, $
|
|
65
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl)=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
66
66
|
const nodeClient = this.createNodeClient(request);
|
|
67
67
|
await nodeClient.signOut(redirectUri);
|
|
68
68
|
request.session.destroy();
|
|
69
69
|
await this.storage?.save();
|
|
70
70
|
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
71
71
|
}, this.ironSessionConfigs);
|
|
72
|
-
this.handleUser = (
|
|
72
|
+
this.handleUser = (configs)=>this.withLogtoApiRoute((request, response)=>{
|
|
73
73
|
response.json(request.user);
|
|
74
|
-
},
|
|
74
|
+
}, configs);
|
|
75
75
|
this.handleAuthRoutes = (configs)=>(request, response)=>{
|
|
76
76
|
const { action: action } = request.query;
|
|
77
77
|
if (action === "sign-in") return this.handleSignIn()(request, response);
|
|
@@ -80,8 +80,8 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
|
|
|
80
80
|
if (action === "user") return this.handleUser(configs)(request, response);
|
|
81
81
|
response.status(404).end();
|
|
82
82
|
};
|
|
83
|
-
this.withLogtoApiRoute = (handler, config = {})=>(0, $
|
|
84
|
-
const user = await this.getLogtoUserFromRequest(request, config
|
|
83
|
+
this.withLogtoApiRoute = (handler, config = {})=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
84
|
+
const user = await this.getLogtoUserFromRequest(request, config);
|
|
85
85
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
86
86
|
Object.defineProperty(request, "user", {
|
|
87
87
|
enumerable: true,
|
|
@@ -89,8 +89,8 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
|
|
|
89
89
|
});
|
|
90
90
|
return handler(request, response);
|
|
91
91
|
}, this.ironSessionConfigs);
|
|
92
|
-
this.withLogtoSsr = (handler,
|
|
93
|
-
const user = await this.getLogtoUserFromRequest(context.req,
|
|
92
|
+
this.withLogtoSsr = (handler, configs = {})=>(0, $gpzKJ$ironsessionnext.withIronSessionSsr)(async (context)=>{
|
|
93
|
+
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
94
94
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
95
95
|
Object.defineProperty(context.req, "user", {
|
|
96
96
|
enumerable: true,
|
|
@@ -100,8 +100,8 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
|
|
|
100
100
|
}, this.ironSessionConfigs);
|
|
101
101
|
}
|
|
102
102
|
createNodeClient(request) {
|
|
103
|
-
this.storage = new (0, $
|
|
104
|
-
return new (0, ($parcel$interopDefault($
|
|
103
|
+
this.storage = new (0, $f80b46e338398246$export$2e2bcd8739ae039)(request);
|
|
104
|
+
return new (0, ($parcel$interopDefault($gpzKJ$logtonode)))(this.config, {
|
|
105
105
|
storage: this.storage,
|
|
106
106
|
navigate: (url)=>{
|
|
107
107
|
this.navigateUrl = url;
|
|
@@ -118,9 +118,9 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
|
|
|
118
118
|
}
|
|
119
119
|
};
|
|
120
120
|
}
|
|
121
|
-
async getLogtoUserFromRequest(request,
|
|
121
|
+
async getLogtoUserFromRequest(request, configs) {
|
|
122
122
|
const nodeClient = this.createNodeClient(request);
|
|
123
|
-
return nodeClient.getContext(
|
|
123
|
+
return nodeClient.getContext(configs);
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;AAAA;;ACIe;IAEb,YAA6B,
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;AAAA;;ACIe;IAEb,YAA6B,QAA0B;uBAA1B;aADrB,iBAAiB,KAAK;IAC0B;IAExD,MAAM,QAAQ,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;QAC5B,IAAI,CAAC,cAAc,GAAG,IAAI;IAC5B;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;QAC5B,IAAI,CAAC,cAAc,GAAG,IAAI;IAC5B;IAEA,MAAM,OAAO;QACX,IAAI,CAAC,IAAI,CAAC,cAAc,EACtB;QAGF,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI;QAC/B,IAAI,CAAC,cAAc,GAAG,KAAK;IAC7B;AACF;;;;ADvBe;IAGb,YAA6B,OAAyB;sBAAzB;aAE7B,eAAe,CACb,cAAc,CAAC,EAAE,IAAI,CAAC,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I,CAAC,OAAO;YACrB,UAAU,CAAC,MAAQ;gBACjB,IAAI,CAAC,WAAW,GAAG;YACrB;QACF;IACF;IAEA,IAAY,qBAAqB;QAC/B,OAAO;YACL,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACxC,UAAU,IAAI,CAAC,MAAM,CAAC,YAAY;YAClC,eAAe;gBACb,QAAQ,IAAI,CAAC,MAAM,CAAC,YAAY;gBAChC,QAAQ;YACV;QACF;IACF;IAEA,MAAc,wBAAwB,OAAwB,EAAE,OAA6B,EAAE;QAC7F,MAAM,aAAa,IAAI,CAAC,gBAAgB,CAAC;QAEzC,OAAO,WAAW,UAAU,CAAC;IAC/B;AACF","sources":["packages/next/src/index.ts","packages/next/src/storage.ts"],"sourcesContent":["import { IncomingMessage } from 'http';\n\nimport NodeClient, { GetContextParameters } from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext } from '@logto/node';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (\n redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri);\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleSignInCallback = (redirectTo = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);\n await this.storage?.save();\n response.redirect(redirectTo);\n }\n }, this.ironSessionConfigs);\n\n handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signOut(redirectUri);\n\n request.session.destroy();\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleUser = (configs?: GetContextParameters) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, configs);\n\n handleAuthRoutes =\n (configs?: GetContextParameters): NextApiHandler =>\n (request, response) => {\n const { action } = request.query;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response);\n }\n\n if (action === 'user') {\n return this.handleUser(configs)(request, response);\n }\n\n response.status(404).end();\n };\n\n withLogtoApiRoute = (\n handler: NextApiHandler,\n config: GetContextParameters = {}\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config);\n\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n\n return handler(request, response);\n }, this.ironSessionConfigs);\n\n withLogtoSsr = <P extends Record<string, unknown> = Record<string, unknown>>(\n handler: (\n context: GetServerSidePropsContext\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n configs: GetContextParameters = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, configs);\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });\n\n return handler(context);\n }, this.ironSessionConfigs);\n\n private createNodeClient(request: IncomingMessage) {\n this.storage = new NextStorage(request);\n\n return new NodeClient(this.config, {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\n });\n }\n\n private get ironSessionConfigs() {\n return {\n cookieName: `logto:${this.config.appId}`,\n password: this.config.cookieSecret,\n cookieOptions: {\n secure: this.config.cookieSecure,\n maxAge: 14 * 24 * 60 * 60,\n },\n };\n }\n\n private async getLogtoUserFromRequest(request: IncomingMessage, configs: GetContextParameters) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(configs);\n }\n}\n","import { IncomingMessage } from 'http';\n\nimport { Storage, StorageKey } from '@logto/node';\n\nexport default class NextStorage implements Storage {\n private sessionChanged = false;\n constructor(private readonly request: IncomingMessage) {}\n\n async setItem(key: StorageKey, value: string) {\n this.request.session[key] = value;\n this.sessionChanged = true;\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 this.sessionChanged = true;\n }\n\n async save() {\n if (!this.sessionChanged) {\n return;\n }\n\n await this.request.session.save();\n this.sessionChanged = false;\n }\n}\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../../"}
|
package/lib/module.d.mts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { LogtoConfig, GetContextParameters } from "@logto/node";
|
|
2
|
+
import { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from "next";
|
|
3
|
+
declare module 'iron-session' {
|
|
4
|
+
interface IronSessionData {
|
|
5
|
+
accessToken?: string;
|
|
6
|
+
idToken?: string;
|
|
7
|
+
signInSession?: string;
|
|
8
|
+
refreshToken?: string;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
type LogtoNextConfig = LogtoConfig & {
|
|
12
|
+
cookieSecret: string;
|
|
13
|
+
cookieSecure: boolean;
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
};
|
|
16
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
17
|
+
export type { LogtoContext } from '@logto/node';
|
|
18
|
+
export default class LogtoClient {
|
|
19
|
+
constructor(config: LogtoNextConfig);
|
|
20
|
+
handleSignIn: (redirectUri?: string) => NextApiHandler;
|
|
21
|
+
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
22
|
+
handleSignOut: (redirectUri?: string) => NextApiHandler;
|
|
23
|
+
handleUser: (configs?: GetContextParameters) => NextApiHandler<any>;
|
|
24
|
+
handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
|
|
25
|
+
withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters) => NextApiHandler;
|
|
26
|
+
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters) => (context: GetServerSidePropsContext<import("querystring").ParsedUrlQuery, import("next").PreviewData>) => Promise<GetServerSidePropsResult<P>>;
|
|
27
|
+
private get ironSessionConfigs();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -54,9 +54,9 @@ class $7d6712a39300ba99$export$2e2bcd8739ae039 {
|
|
|
54
54
|
await this.storage?.save();
|
|
55
55
|
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
56
56
|
}, this.ironSessionConfigs);
|
|
57
|
-
this.handleUser = (
|
|
57
|
+
this.handleUser = (configs)=>this.withLogtoApiRoute((request, response)=>{
|
|
58
58
|
response.json(request.user);
|
|
59
|
-
},
|
|
59
|
+
}, configs);
|
|
60
60
|
this.handleAuthRoutes = (configs)=>(request, response)=>{
|
|
61
61
|
const { action: action } = request.query;
|
|
62
62
|
if (action === "sign-in") return this.handleSignIn()(request, response);
|
|
@@ -66,7 +66,7 @@ class $7d6712a39300ba99$export$2e2bcd8739ae039 {
|
|
|
66
66
|
response.status(404).end();
|
|
67
67
|
};
|
|
68
68
|
this.withLogtoApiRoute = (handler, config = {})=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
69
|
-
const user = await this.getLogtoUserFromRequest(request, config
|
|
69
|
+
const user = await this.getLogtoUserFromRequest(request, config);
|
|
70
70
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
71
71
|
Object.defineProperty(request, "user", {
|
|
72
72
|
enumerable: true,
|
|
@@ -74,8 +74,8 @@ class $7d6712a39300ba99$export$2e2bcd8739ae039 {
|
|
|
74
74
|
});
|
|
75
75
|
return handler(request, response);
|
|
76
76
|
}, this.ironSessionConfigs);
|
|
77
|
-
this.withLogtoSsr = (handler,
|
|
78
|
-
const user = await this.getLogtoUserFromRequest(context.req,
|
|
77
|
+
this.withLogtoSsr = (handler, configs = {})=>(0, $a8eQ2$withIronSessionSsr)(async (context)=>{
|
|
78
|
+
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
79
79
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
80
80
|
Object.defineProperty(context.req, "user", {
|
|
81
81
|
enumerable: true,
|
|
@@ -103,12 +103,12 @@ class $7d6712a39300ba99$export$2e2bcd8739ae039 {
|
|
|
103
103
|
}
|
|
104
104
|
};
|
|
105
105
|
}
|
|
106
|
-
async getLogtoUserFromRequest(request,
|
|
106
|
+
async getLogtoUserFromRequest(request, configs) {
|
|
107
107
|
const nodeClient = this.createNodeClient(request);
|
|
108
|
-
return nodeClient.getContext(
|
|
108
|
+
return nodeClient.getContext(configs);
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
|
|
113
113
|
export {$7d6712a39300ba99$export$2e2bcd8739ae039 as default, $7d6712a39300ba99$re_export$ReservedScope as ReservedScope, $7d6712a39300ba99$re_export$UserScope as UserScope};
|
|
114
|
-
//# sourceMappingURL=module.
|
|
114
|
+
//# sourceMappingURL=module.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;AAAA;;ACIe;IAEb,YAA6B,QAA0B;uBAA1B;aADrB,iBAAiB,KAAK;IAC0B;IAExD,MAAM,QAAQ,GAAe,EAAE,KA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AAC,SAAS,QAAQ;oBAAE,YAAY,IAAI;oBAAE,KAAK,IAAM;gBAAK;gBAE3E,OAAO,QAAQ,SAAS;YAC1B,GAAG,IAAI,CAAC,kBAAkB;aAE5B,eAAe,CACb,SAGA,UAAgC,CAAC,CAAC,GAElC,CAAA,GAAA,yBAAiB,EAAE,OAAO,UAAY;gBACpC,MAAM,OAAO,MAAM,IAAI,CAAC,uBAAuB,CAAC,QAAQ,GAAG,EAAE;gBAC7D,8DAA8D;gBAC9D,OAAO,cAAc,CAAC,QAAQ,GAAG,EAAE,QAAQ;oBAAE,YAAY,IAAI;oBAAE,KAAK,IAAM;gBAAK;gBAE/E,OAAO,QAAQ;YACjB,GAAG,IAAI,CAAC,kBAAkB;IA7F2B;IA+F/C,iBAAiB,OAAwB,EAAE;QACjD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA,GAAA,wCAAW,AAAD,EAAE;QAE/B,OAAO,IAAI,CAAA,GAAA,gBAAS,EAAE,IAAI,CAAC,MAAM,EAAE;YACjC,SAAS,IAAI,CAAC,OAAO;YACrB,UAAU,CAAC,MAAQ;gBACjB,IAAI,CAAC,WAAW,GAAG;YACrB;QACF;IACF;IAEA,IAAY,qBAAqB;QAC/B,OAAO;YACL,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACxC,UAAU,IAAI,CAAC,MAAM,CAAC,YAAY;YAClC,eAAe;gBACb,QAAQ,IAAI,CAAC,MAAM,CAAC,YAAY;gBAChC,QAAQ;YACV;QACF;IACF;IAEA,MAAc,wBAAwB,OAAwB,EAAE,OAA6B,EAAE;QAC7F,MAAM,aAAa,IAAI,CAAC,gBAAgB,CAAC;QAEzC,OAAO,WAAW,UAAU,CAAC;IAC/B;AACF","sources":["packages/next/src/index.ts","packages/next/src/storage.ts"],"sourcesContent":["import { IncomingMessage } from 'http';\n\nimport NodeClient, { GetContextParameters } from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext } from '@logto/node';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (\n redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri);\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleSignInCallback = (redirectTo = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);\n await this.storage?.save();\n response.redirect(redirectTo);\n }\n }, this.ironSessionConfigs);\n\n handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signOut(redirectUri);\n\n request.session.destroy();\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleUser = (configs?: GetContextParameters) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, configs);\n\n handleAuthRoutes =\n (configs?: GetContextParameters): NextApiHandler =>\n (request, response) => {\n const { action } = request.query;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response);\n }\n\n if (action === 'user') {\n return this.handleUser(configs)(request, response);\n }\n\n response.status(404).end();\n };\n\n withLogtoApiRoute = (\n handler: NextApiHandler,\n config: GetContextParameters = {}\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config);\n\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n\n return handler(request, response);\n }, this.ironSessionConfigs);\n\n withLogtoSsr = <P extends Record<string, unknown> = Record<string, unknown>>(\n handler: (\n context: GetServerSidePropsContext\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n configs: GetContextParameters = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, configs);\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });\n\n return handler(context);\n }, this.ironSessionConfigs);\n\n private createNodeClient(request: IncomingMessage) {\n this.storage = new NextStorage(request);\n\n return new NodeClient(this.config, {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\n });\n }\n\n private get ironSessionConfigs() {\n return {\n cookieName: `logto:${this.config.appId}`,\n password: this.config.cookieSecret,\n cookieOptions: {\n secure: this.config.cookieSecure,\n maxAge: 14 * 24 * 60 * 60,\n },\n };\n }\n\n private async getLogtoUserFromRequest(request: IncomingMessage, configs: GetContextParameters) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(configs);\n }\n}\n","import { IncomingMessage } from 'http';\n\nimport { Storage, StorageKey } from '@logto/node';\n\nexport default class NextStorage implements Storage {\n private sessionChanged = false;\n constructor(private readonly request: IncomingMessage) {}\n\n async setItem(key: StorageKey, value: string) {\n this.request.session[key] = value;\n this.sessionChanged = true;\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 this.sessionChanged = true;\n }\n\n async save() {\n if (!this.sessionChanged) {\n return;\n }\n\n await this.request.session.save();\n this.sessionChanged = false;\n }\n}\n"],"names":[],"version":3,"file":"module.mjs.map"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/next",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-rc.0",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
"require": "./lib/index.js",
|
|
8
|
-
"import": "./lib/module.
|
|
8
|
+
"import": "./lib/module.mjs"
|
|
9
9
|
},
|
|
10
|
-
"module": "./lib/module.
|
|
10
|
+
"module": "./lib/module.mjs",
|
|
11
11
|
"types": "./lib/index.d.ts",
|
|
12
12
|
"files": [
|
|
13
13
|
"lib"
|
|
@@ -22,21 +22,21 @@
|
|
|
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/ && pnpm check && parcel build",
|
|
25
|
+
"build": "rm -rf lib/ && pnpm check && parcel build && cp lib/index.d.ts lib/module.d.mts",
|
|
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.0.0-
|
|
32
|
+
"@logto/node": "^1.0.0-rc.0",
|
|
33
33
|
"iron-session": "^6.1.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@jest/types": "^27.5.1",
|
|
37
|
-
"@parcel/core": "^2.
|
|
38
|
-
"@parcel/packager-ts": "^2.
|
|
39
|
-
"@parcel/transformer-typescript-types": "^2.
|
|
37
|
+
"@parcel/core": "^2.8.3",
|
|
38
|
+
"@parcel/packager-ts": "^2.8.3",
|
|
39
|
+
"@parcel/transformer-typescript-types": "^2.8.3",
|
|
40
40
|
"@silverhand/eslint-config": "^1.0.0",
|
|
41
41
|
"@silverhand/ts-config": "^1.0.0",
|
|
42
42
|
"@silverhand/ts-config-react": "^1.0.0",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"lint-staged": "^13.0.0",
|
|
49
49
|
"next": "^12.2.2",
|
|
50
50
|
"next-test-api-route-handler": "^3.1.6",
|
|
51
|
-
"parcel": "^2.
|
|
51
|
+
"parcel": "^2.8.3",
|
|
52
52
|
"prettier": "^2.7.1",
|
|
53
53
|
"react": "^17.0.2",
|
|
54
54
|
"react-dom": "^17.0.2",
|
|
@@ -69,9 +69,9 @@
|
|
|
69
69
|
"main": {
|
|
70
70
|
"context": "node",
|
|
71
71
|
"engines": {
|
|
72
|
-
"node": "
|
|
72
|
+
"node": ">=18.12.0"
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "98960287d1016efa9e68b6c4d4407885d6ec9dc6"
|
|
77
77
|
}
|
package/lib/module.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;ACIe;IAEb,YAA6B,OAAwB,CAAE;aAA1B,OAAwB,GAAxB,OAAwB;aAD7C,cAAc,GAAG,KAAK;KAC2B;IAEzD,MAAM,OAAO,CAAC,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QAClC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;KAC5B;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,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,CAAC;aACzB,EAAE,IAAI,CAAC,kBAAkB,CAAC;KA1F2B;IA4FxD,AAAQ,gBAAgB,CAAC,OAAwB,EAAE;QACjD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA,GAAA,wCAAW,CAAA,CAAC,OAAO,CAAC,CAAC;QAExC,OAAO,IAAI,CAAA,GAAA,gBAAU,CAAA,CAAC,IAAI,CAAC,MAAM,EAAE;YACjC,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,QAAQ,EAAE,CAAC,GAAG,GAAK;gBACjB,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC;aACxB;SACF,CAAC,CAAC;KACJ;IAED,IAAY,kBAAkB,GAAG;QAC/B,OAAO;YACL,UAAU,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACxC,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,YAAY;YAClC,aAAa,EAAE;gBACb,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,YAAY;gBAChC,MAAM,EAAE,OAAiB;aAC1B;SACF,CAAC;KACH;IAED,MAAc,uBAAuB,CAAC,OAAwB,EAAE,cAAwB,EAAE;QACxF,MAAM,UAAU,GAAG,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,AAAC;QAElD,OAAO,UAAU,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC;KAC9C;CACF","sources":["packages/next/src/index.ts","packages/next/src/storage.ts"],"sourcesContent":["import { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig, WithLogtoConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext } from '@logto/node';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (\n redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri);\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleSignInCallback = (redirectTo = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);\n await this.storage?.save();\n response.redirect(redirectTo);\n }\n }, this.ironSessionConfigs);\n\n handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signOut(redirectUri);\n\n request.session.destroy();\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleUser = (config?: WithLogtoConfig) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, config);\n\n handleAuthRoutes =\n (configs?: WithLogtoConfig): NextApiHandler =>\n (request, response) => {\n const { action } = request.query;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response);\n }\n\n if (action === 'user') {\n return this.handleUser(configs)(request, response);\n }\n\n response.status(404).end();\n };\n\n withLogtoApiRoute = (handler: NextApiHandler, config: WithLogtoConfig = {}): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);\n\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n\n return handler(request, response);\n }, this.ironSessionConfigs);\n\n withLogtoSsr = <P extends Record<string, unknown> = Record<string, unknown>>(\n handler: (\n context: GetServerSidePropsContext\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n config: WithLogtoConfig = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, config.getAccessToken);\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });\n\n return handler(context);\n }, this.ironSessionConfigs);\n\n private createNodeClient(request: IncomingMessage) {\n this.storage = new NextStorage(request);\n\n return new NodeClient(this.config, {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\n });\n }\n\n private get ironSessionConfigs() {\n return {\n cookieName: `logto:${this.config.appId}`,\n password: this.config.cookieSecret,\n cookieOptions: {\n secure: this.config.cookieSecure,\n maxAge: 14 * 24 * 60 * 60,\n },\n };\n }\n\n private async getLogtoUserFromRequest(request: IncomingMessage, getAccessToken?: boolean) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(getAccessToken);\n }\n}\n","import { IncomingMessage } from 'http';\n\nimport { Storage, StorageKey } from '@logto/node';\n\nexport default class NextStorage implements Storage {\n private sessionChanged = false;\n constructor(private readonly request: IncomingMessage) {}\n\n async setItem(key: StorageKey, value: string) {\n this.request.session[key] = value;\n this.sessionChanged = true;\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 this.sessionChanged = true;\n }\n\n async save() {\n if (!this.sessionChanged) {\n return;\n }\n\n await this.request.session.save();\n this.sessionChanged = false;\n }\n}\n"],"names":[],"version":3,"file":"module.js.map"}
|