@logto/next 1.0.0-beta.0 → 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/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Logto Next.js SDK
2
+ [![Version](https://img.shields.io/npm/v/@logto/next)](https://www.npmjs.com/package/@logto/next)
3
+ [![Build Status](https://github.com/logto-io/js/actions/workflows/main.yml/badge.svg)](https://github.com/logto-io/js/actions/workflows/main.yml)
4
+ [![Codecov](https://img.shields.io/codecov/c/github/logto-io/js)](https://app.codecov.io/gh/logto-io/js?branch=master)
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.
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.
9
+
10
+ ## Installation
11
+
12
+ ### Using npm
13
+
14
+ ```bash
15
+ npm install @logto/next
16
+ ```
17
+
18
+ ### Using yarn
19
+
20
+ ```bash
21
+ yarn add @logto/next
22
+ ```
23
+
24
+ ### Using pnpm
25
+
26
+ ```bash
27
+ pnpm add @logto/next
28
+ ```
29
+
30
+ ## Get sample
31
+
32
+ A sample project can be found at [Next.js Sample](https://github.com/logto-io/js/tree/master/packages/next-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
+ [![Website](https://img.shields.io/badge/website-logto.io-8262F8.svg)](https://logto.io/)
43
+ [![Docs](https://img.shields.io/badge/docs-logto.io-green.svg)](https://docs.logto.io/sdk/JavaScript/next/)
44
+ [![Discord](https://img.shields.io/discord/965845662535147551?logo=discord&logoColor=ffffff&color=7389D8&cacheSeconds=600)](https://discord.gg/UEPaF3j5e6)
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IdTokenClaims, LogtoConfig } from "@logto/node";
1
+ import { LogtoConfig } from "@logto/node";
2
2
  import { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from "next";
3
3
  declare module 'iron-session' {
4
4
  interface IronSessionData {
@@ -8,21 +8,11 @@ declare module 'iron-session' {
8
8
  refreshToken?: string;
9
9
  }
10
10
  }
11
- declare module 'http' {
12
- interface IncomingMessage {
13
- user: LogtoUser;
14
- }
15
- }
16
11
  type LogtoNextConfig = LogtoConfig & {
17
12
  cookieSecret: string;
18
13
  cookieSecure: boolean;
19
14
  baseUrl: string;
20
15
  };
21
- export type LogtoUser = {
22
- isAuthenticated: boolean;
23
- claims?: IdTokenClaims;
24
- accessToken?: string;
25
- };
26
16
  /**
27
17
  * @getAccessToken: if set to true, will try to get an access token and attach to req.user,
28
18
  * if unable to grant an access token, will set req.user.isAuthenticated to false,
@@ -30,13 +20,17 @@ export type LogtoUser = {
30
20
  */
31
21
  type WithLogtoConfig = {
32
22
  getAccessToken?: boolean;
23
+ fetchUserInfo?: boolean;
33
24
  };
25
+ export { ReservedScope, UserScope } from '@logto/node';
26
+ export type { LogtoContext } from '@logto/node';
34
27
  export default class LogtoClient {
35
28
  constructor(config: LogtoNextConfig);
36
29
  handleSignIn: (redirectUri?: string) => NextApiHandler;
37
30
  handleSignInCallback: (redirectTo?: string) => NextApiHandler;
38
31
  handleSignOut: (redirectUri?: string) => NextApiHandler;
39
- handleUser: (config?: WithLogtoConfig | undefined) => NextApiHandler<any>;
32
+ handleUser: (config?: WithLogtoConfig) => NextApiHandler<any>;
33
+ handleAuthRoutes: (configs?: WithLogtoConfig) => NextApiHandler;
40
34
  withLogtoApiRoute: (handler: NextApiHandler, config?: WithLogtoConfig) => NextApiHandler;
41
35
  withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, config?: WithLogtoConfig) => (context: GetServerSidePropsContext<import("querystring").ParsedUrlQuery, import("next").PreviewData>) => Promise<GetServerSidePropsResult<P>>;
42
36
  private get ironSessionConfigs();
@@ -1 +1 @@
1
- {"mappings":";;ACMA,eAAe,cAAc,CAAC;IAC5B,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,eAAe,MAAM,CAAC;IACpB,UAAU,eAAe;QACvB,IAAI,EAAE,SAAS,CAAC;KACjB;CACF;AAED,uBAA8B,WAAW,GAAG;IAC1C,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,wBAAwB;IACtB,eAAe,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AACH,uBAA8B;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AC7BF;gBAG+B,MAAM,EAAE,eAAe;IAEpD,YAAY,4BAAkE,cAAc,CAS9D;IAE9B,oBAAoB,2BAAuC,cAAc,CAS3C;IAE9B,aAAa,4BAAwC,cAAc,CAWrC;IAE9B,UAAU,gEAGG;IAEb,iBAAiB,YAAa,cAAc,WAAU,eAAe,KAAQ,cAAc,CAQ7D;IAE9B,YAAY,mFAEC,yBAAyB,kFAE5B,eAAe,oJAQK;IAmB9B,OAAO,KAAK,kBAAkB,GAS7B;CAwCF","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 from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig, LogtoUser, WithLogtoConfig } from './types';\n\nexport type { LogtoUser } from './types';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`): 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 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(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\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 const { isAuthenticated } = nodeClient;\n\n if (!isAuthenticated) {\n const user: LogtoUser = {\n isAuthenticated,\n };\n\n return user;\n }\n\n if (!getAccessToken) {\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n };\n\n return user;\n }\n\n try {\n const accessToken = await nodeClient.getAccessToken();\n await this.storage?.save();\n\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n accessToken,\n };\n\n return user;\n } catch {\n return {\n isAuthenticated: false,\n };\n }\n }\n}\n"],"names":[],"version":3,"file":"index.d.ts.map"}
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;AAEF;;;;GAIG;AACH,uBAA8B;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;ACtBF,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,YAAa,eAAe,yBAGzB;IAEb,gBAAgB,aACH,eAAe,KAAG,cAAc,CAqBzC;IAEJ,iBAAiB,YAAa,cAAc,WAAU,eAAe,KAAQ,cAAc,CAY7D;IAE9B,YAAY,mFAEC,yBAAyB,kFAE5B,eAAe,oJAQK;IAa9B,OAAO,KAAK,kBAAkB,GAS7B;CAWF","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 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(\n request,\n config.getAccessToken,\n config.fetchUserInfo\n );\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(\n request: IncomingMessage,\n getAccessToken?: boolean,\n fetchUserInfo?: boolean\n ) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(getAccessToken, fetchUserInfo);\n }\n}\n"],"names":[],"version":3,"file":"index.d.ts.map"}
package/lib/index.js CHANGED
@@ -14,6 +14,8 @@ function $parcel$export(e, n, v, s) {
14
14
  $parcel$defineInteropFlag(module.exports);
15
15
 
16
16
  $parcel$export(module.exports, "default", () => $3556748a90dddf6c$export$2e2bcd8739ae039);
17
+ $parcel$export(module.exports, "ReservedScope", () => $3556748a90dddf6c$re_export$ReservedScope);
18
+ $parcel$export(module.exports, "UserScope", () => $3556748a90dddf6c$re_export$UserScope);
17
19
 
18
20
 
19
21
  class $1abf01985e812f74$export$2e2bcd8739ae039 {
@@ -25,12 +27,12 @@ class $1abf01985e812f74$export$2e2bcd8739ae039 {
25
27
  this.request.session[key] = value;
26
28
  this.sessionChanged = true;
27
29
  }
28
- getItem(key) {
30
+ async getItem(key) {
29
31
  const value = this.request.session[key];
30
32
  if (value === undefined) return null;
31
33
  return String(value);
32
34
  }
33
- removeItem(key) {
35
+ async removeItem(key) {
34
36
  this.request.session[key] = undefined;
35
37
  this.sessionChanged = true;
36
38
  }
@@ -42,10 +44,11 @@ class $1abf01985e812f74$export$2e2bcd8739ae039 {
42
44
  }
43
45
 
44
46
 
47
+
45
48
  class $3556748a90dddf6c$export$2e2bcd8739ae039 {
46
- constructor(config1){
47
- this.config = config1;
48
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`)=>(0, $j3Dy6$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
49
+ constructor(config){
50
+ this.config = config;
51
+ this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`)=>(0, $j3Dy6$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
49
52
  const nodeClient = this.createNodeClient(request);
50
53
  await nodeClient.signIn(redirectUri);
51
54
  await this.storage?.save();
@@ -69,8 +72,16 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
69
72
  this.handleUser = (config)=>this.withLogtoApiRoute((request, response)=>{
70
73
  response.json(request.user);
71
74
  }, config);
75
+ this.handleAuthRoutes = (configs)=>(request, response)=>{
76
+ const { action: action } = request.query;
77
+ if (action === "sign-in") return this.handleSignIn()(request, response);
78
+ if (action === "sign-in-callback") return this.handleSignInCallback()(request, response);
79
+ if (action === "sign-out") return this.handleSignOut()(request, response);
80
+ if (action === "user") return this.handleUser(configs)(request, response);
81
+ response.status(404).end();
82
+ };
72
83
  this.withLogtoApiRoute = (handler, config = {})=>(0, $j3Dy6$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
73
- const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);
84
+ const user = await this.getLogtoUserFromRequest(request, config.getAccessToken, config.fetchUserInfo);
74
85
  // eslint-disable-next-line @silverhand/fp/no-mutating-methods
75
86
  Object.defineProperty(request, "user", {
76
87
  enumerable: true,
@@ -90,10 +101,7 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
90
101
  }
91
102
  createNodeClient(request) {
92
103
  this.storage = new (0, $1abf01985e812f74$export$2e2bcd8739ae039)(request);
93
- return new (0, ($parcel$interopDefault($j3Dy6$logtonode)))({
94
- ...this.config,
95
- persistAccessToken: this.config.persistAccessToken ?? true
96
- }, {
104
+ return new (0, ($parcel$interopDefault($j3Dy6$logtonode)))(this.config, {
97
105
  storage: this.storage,
98
106
  navigate: (url)=>{
99
107
  this.navigateUrl = url;
@@ -110,36 +118,9 @@ class $3556748a90dddf6c$export$2e2bcd8739ae039 {
110
118
  }
111
119
  };
112
120
  }
113
- async getLogtoUserFromRequest(request, getAccessToken) {
121
+ async getLogtoUserFromRequest(request, getAccessToken, fetchUserInfo) {
114
122
  const nodeClient = this.createNodeClient(request);
115
- const { isAuthenticated: isAuthenticated } = nodeClient;
116
- if (!isAuthenticated) {
117
- const user = {
118
- isAuthenticated: isAuthenticated
119
- };
120
- return user;
121
- }
122
- if (!getAccessToken) {
123
- const user = {
124
- isAuthenticated: isAuthenticated,
125
- claims: nodeClient.getIdTokenClaims()
126
- };
127
- return user;
128
- }
129
- try {
130
- const accessToken = await nodeClient.getAccessToken();
131
- await this.storage?.save();
132
- const user = {
133
- isAuthenticated: isAuthenticated,
134
- claims: nodeClient.getIdTokenClaims(),
135
- accessToken: accessToken
136
- };
137
- return user;
138
- } catch {
139
- return {
140
- isAuthenticated: false
141
- };
142
- }
123
+ return nodeClient.getContext(getAccessToken, fetchUserInfo);
143
124
  }
144
125
  }
145
126
 
package/lib/index.js.map CHANGED
@@ -1 +1 @@
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,OAAO,CAAC,GAAe,EAAE;QACvB,MAAM,KAAK,GAAG,I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,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;QAClD,MAAM,mBAAE,eAAe,CAAA,EAAE,GAAG,UAAU,AAAC;QAEvC,IAAI,CAAC,eAAe,EAAE;YACpB,MAAM,IAAI,GAAc;iCACtB,eAAe;aAChB,AAAC;YAEF,OAAO,IAAI,CAAC;SACb;QAED,IAAI,CAAC,cAAc,EAAE;YACnB,MAAM,IAAI,GAAc;iCACtB,eAAe;gBACf,MAAM,EAAE,UAAU,CAAC,gBAAgB,EAAE;aACtC,AAAC;YAEF,OAAO,IAAI,CAAC;SACb;QAED,IAAI;YACF,MAAM,WAAW,GAAG,MAAM,UAAU,CAAC,cAAc,EAAE,AAAC;YACtD,MAAM,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC;YAE3B,MAAM,IAAI,GAAc;iCACtB,eAAe;gBACf,MAAM,EAAE,UAAU,CAAC,gBAAgB,EAAE;6BACrC,WAAW;aACZ,AAAC;YAEF,OAAO,IAAI,CAAC;SACb,CAAC,OAAM;YACN,OAAO;gBACL,eAAe,EAAE,KAAK;aACvB,CAAC;SACH;KACF;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, LogtoUser, WithLogtoConfig } from './types';\n\nexport type { LogtoUser } from './types';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`): 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 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(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\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 const { isAuthenticated } = nodeClient;\n\n if (!isAuthenticated) {\n const user: LogtoUser = {\n isAuthenticated,\n };\n\n return user;\n }\n\n if (!getAccessToken) {\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n };\n\n return user;\n }\n\n try {\n const accessToken = await nodeClient.getAccessToken();\n await this.storage?.save();\n\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n accessToken,\n };\n\n return user;\n } catch {\n return {\n isAuthenticated: false,\n };\n }\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 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 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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AAE,OAAiB;aAC1B;SACF,CAAC;KACH;IAED,MAAc,uBAAuB,CACnC,OAAwB,EACxB,cAAwB,EACxB,aAAuB,EACvB;QACA,MAAM,UAAU,GAAG,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,AAAC;QAElD,OAAO,UAAU,CAAC,UAAU,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;KAC7D;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(\n request,\n config.getAccessToken,\n config.fetchUserInfo\n );\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(\n request: IncomingMessage,\n getAccessToken?: boolean,\n fetchUserInfo?: boolean\n ) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(getAccessToken, fetchUserInfo);\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.js CHANGED
@@ -1,4 +1,4 @@
1
- import $a8eQ2$logtonode from "@logto/node";
1
+ import $a8eQ2$logtonode, {ReservedScope as $7d6712a39300ba99$re_export$ReservedScope, UserScope as $7d6712a39300ba99$re_export$UserScope} from "@logto/node";
2
2
  import {withIronSessionApiRoute as $a8eQ2$withIronSessionApiRoute, withIronSessionSsr as $a8eQ2$withIronSessionSsr} from "iron-session/next";
3
3
 
4
4
 
@@ -12,12 +12,12 @@ class $8fa7922f9b3d24ed$export$2e2bcd8739ae039 {
12
12
  this.request.session[key] = value;
13
13
  this.sessionChanged = true;
14
14
  }
15
- getItem(key) {
15
+ async getItem(key) {
16
16
  const value = this.request.session[key];
17
17
  if (value === undefined) return null;
18
18
  return String(value);
19
19
  }
20
- removeItem(key) {
20
+ async removeItem(key) {
21
21
  this.request.session[key] = undefined;
22
22
  this.sessionChanged = true;
23
23
  }
@@ -29,10 +29,11 @@ class $8fa7922f9b3d24ed$export$2e2bcd8739ae039 {
29
29
  }
30
30
 
31
31
 
32
+
32
33
  class $7d6712a39300ba99$export$2e2bcd8739ae039 {
33
- constructor(config1){
34
- this.config = config1;
35
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
34
+ constructor(config){
35
+ this.config = config;
36
+ this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
36
37
  const nodeClient = this.createNodeClient(request);
37
38
  await nodeClient.signIn(redirectUri);
38
39
  await this.storage?.save();
@@ -56,8 +57,16 @@ class $7d6712a39300ba99$export$2e2bcd8739ae039 {
56
57
  this.handleUser = (config)=>this.withLogtoApiRoute((request, response)=>{
57
58
  response.json(request.user);
58
59
  }, config);
60
+ this.handleAuthRoutes = (configs)=>(request, response)=>{
61
+ const { action: action } = request.query;
62
+ if (action === "sign-in") return this.handleSignIn()(request, response);
63
+ if (action === "sign-in-callback") return this.handleSignInCallback()(request, response);
64
+ if (action === "sign-out") return this.handleSignOut()(request, response);
65
+ if (action === "user") return this.handleUser(configs)(request, response);
66
+ response.status(404).end();
67
+ };
59
68
  this.withLogtoApiRoute = (handler, config = {})=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
60
- const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);
69
+ const user = await this.getLogtoUserFromRequest(request, config.getAccessToken, config.fetchUserInfo);
61
70
  // eslint-disable-next-line @silverhand/fp/no-mutating-methods
62
71
  Object.defineProperty(request, "user", {
63
72
  enumerable: true,
@@ -77,10 +86,7 @@ class $7d6712a39300ba99$export$2e2bcd8739ae039 {
77
86
  }
78
87
  createNodeClient(request) {
79
88
  this.storage = new (0, $8fa7922f9b3d24ed$export$2e2bcd8739ae039)(request);
80
- return new (0, $a8eQ2$logtonode)({
81
- ...this.config,
82
- persistAccessToken: this.config.persistAccessToken ?? true
83
- }, {
89
+ return new (0, $a8eQ2$logtonode)(this.config, {
84
90
  storage: this.storage,
85
91
  navigate: (url)=>{
86
92
  this.navigateUrl = url;
@@ -97,39 +103,12 @@ class $7d6712a39300ba99$export$2e2bcd8739ae039 {
97
103
  }
98
104
  };
99
105
  }
100
- async getLogtoUserFromRequest(request, getAccessToken) {
106
+ async getLogtoUserFromRequest(request, getAccessToken, fetchUserInfo) {
101
107
  const nodeClient = this.createNodeClient(request);
102
- const { isAuthenticated: isAuthenticated } = nodeClient;
103
- if (!isAuthenticated) {
104
- const user = {
105
- isAuthenticated: isAuthenticated
106
- };
107
- return user;
108
- }
109
- if (!getAccessToken) {
110
- const user = {
111
- isAuthenticated: isAuthenticated,
112
- claims: nodeClient.getIdTokenClaims()
113
- };
114
- return user;
115
- }
116
- try {
117
- const accessToken = await nodeClient.getAccessToken();
118
- await this.storage?.save();
119
- const user = {
120
- isAuthenticated: isAuthenticated,
121
- claims: nodeClient.getIdTokenClaims(),
122
- accessToken: accessToken
123
- };
124
- return user;
125
- } catch {
126
- return {
127
- isAuthenticated: false
128
- };
129
- }
108
+ return nodeClient.getContext(getAccessToken, fetchUserInfo);
130
109
  }
131
110
  }
132
111
 
133
112
 
134
- export {$7d6712a39300ba99$export$2e2bcd8739ae039 as default};
113
+ export {$7d6712a39300ba99$export$2e2bcd8739ae039 as default, $7d6712a39300ba99$re_export$ReservedScope as ReservedScope, $7d6712a39300ba99$re_export$UserScope as UserScope};
135
114
  //# sourceMappingURL=module.js.map
package/lib/module.js.map CHANGED
@@ -1 +1 @@
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,OAAO,CAAC,GAAe,EAAE;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,C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,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;QAClD,MAAM,mBAAE,eAAe,CAAA,EAAE,GAAG,UAAU,AAAC;QAEvC,IAAI,CAAC,eAAe,EAAE;YACpB,MAAM,IAAI,GAAc;iCACtB,eAAe;aAChB,AAAC;YAEF,OAAO,IAAI,CAAC;SACb;QAED,IAAI,CAAC,cAAc,EAAE;YACnB,MAAM,IAAI,GAAc;iCACtB,eAAe;gBACf,MAAM,EAAE,UAAU,CAAC,gBAAgB,EAAE;aACtC,AAAC;YAEF,OAAO,IAAI,CAAC;SACb;QAED,IAAI;YACF,MAAM,WAAW,GAAG,MAAM,UAAU,CAAC,cAAc,EAAE,AAAC;YACtD,MAAM,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC;YAE3B,MAAM,IAAI,GAAc;iCACtB,eAAe;gBACf,MAAM,EAAE,UAAU,CAAC,gBAAgB,EAAE;6BACrC,WAAW;aACZ,AAAC;YAEF,OAAO,IAAI,CAAC;SACb,CAAC,OAAM;YACN,OAAO;gBACL,eAAe,EAAE,KAAK;aACvB,CAAC;SACH;KACF;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, LogtoUser, WithLogtoConfig } from './types';\n\nexport type { LogtoUser } from './types';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`): 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 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(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\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 const { isAuthenticated } = nodeClient;\n\n if (!isAuthenticated) {\n const user: LogtoUser = {\n isAuthenticated,\n };\n\n return user;\n }\n\n if (!getAccessToken) {\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n };\n\n return user;\n }\n\n try {\n const accessToken = await nodeClient.getAccessToken();\n await this.storage?.save();\n\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n accessToken,\n };\n\n return user;\n } catch {\n return {\n isAuthenticated: false,\n };\n }\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 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 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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uB,CACnC,OAAwB,EACxB,cAAwB,EACxB,aAAuB,EACvB;QACA,MAAM,UAAU,GAAG,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,AAAC;QAElD,OAAO,UAAU,CAAC,UAAU,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;KAC7D;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(\n request,\n config.getAccessToken,\n config.fetchUserInfo\n );\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(\n request: IncomingMessage,\n getAccessToken?: boolean,\n fetchUserInfo?: boolean\n ) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(getAccessToken, fetchUserInfo);\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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/next",
3
- "version": "1.0.0-beta.0",
3
+ "version": "1.0.0-beta.10",
4
4
  "source": "./src/index.ts",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {
@@ -29,31 +29,31 @@
29
29
  "prepack": "pnpm test"
30
30
  },
31
31
  "dependencies": {
32
- "@logto/node": "^1.0.0-beta.0",
32
+ "@logto/node": "^1.0.0-beta.10",
33
33
  "iron-session": "^6.1.3"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@jest/types": "^27.5.1",
37
- "@parcel/core": "^2.6.2",
38
- "@parcel/packager-ts": "^2.6.2",
39
- "@parcel/transformer-typescript-types": "^2.6.2",
40
- "@silverhand/eslint-config": "^0.17.0",
41
- "@silverhand/ts-config": "^0.14.0",
42
- "@silverhand/ts-config-react": "^0.14.0",
37
+ "@parcel/core": "^2.7.0",
38
+ "@parcel/packager-ts": "^2.7.0",
39
+ "@parcel/transformer-typescript-types": "^2.7.0",
40
+ "@silverhand/eslint-config": "^1.0.0",
41
+ "@silverhand/ts-config": "^1.0.0",
42
+ "@silverhand/ts-config-react": "^1.0.0",
43
43
  "@types/jest": "^27.4.0",
44
- "eslint": "^8.9.0",
44
+ "eslint": "^8.23.0",
45
45
  "jest": "^27.5.1",
46
46
  "jest-location-mock": "^1.0.9",
47
47
  "jest-matcher-specific-error": "^1.0.0",
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.6.2",
52
- "prettier": "^2.3.2",
51
+ "parcel": "^2.7.0",
52
+ "prettier": "^2.7.1",
53
53
  "react": "^17.0.2",
54
54
  "react-dom": "^17.0.2",
55
55
  "ts-jest": "^27.0.4",
56
- "typescript": "^4.5.5"
56
+ "typescript": "4.7.4"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "next": ">=12"
@@ -73,5 +73,5 @@
73
73
  }
74
74
  }
75
75
  },
76
- "gitHead": "f0f78e6f0b97174de98588b35d1d12c8396206ba"
76
+ "gitHead": "c79c3759b7f15c44bf5b476dcebd82e9c96e9dd8"
77
77
  }