@logto/next 3.6.1 → 3.7.1

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.
@@ -16,17 +16,15 @@ class LogtoClient extends client.default {
16
16
  super(config, {
17
17
  NodeClient: NodeClient__default.default,
18
18
  });
19
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => async (request) => {
20
- const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
21
- await nodeClient.signIn(redirectUri, interactionMode);
22
- const response = new Response(null, {
23
- headers,
24
- status: 307,
25
- });
26
- if (this.navigateUrl) {
27
- response.headers.append('Location', this.navigateUrl);
19
+ this.handleSignIn = (options, interactionMode) => {
20
+ // The array function can not have multiple signatures, have to warn the deprecated usage
21
+ if (typeof options === 'string') {
22
+ console.warn('Deprecated: Use the object parameter for handleSignIn instead.');
23
+ return this.handleSignInImplementation({ redirectUri: options, interactionMode });
28
24
  }
29
- return response;
25
+ return this.handleSignInImplementation(options ?? {
26
+ redirectUri: `${this.config.baseUrl}/api/logto/sign-in-callback`,
27
+ });
30
28
  };
31
29
  this.handleSignOut = (redirectUri = this.config.baseUrl) => async (request) => {
32
30
  const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
@@ -71,6 +69,18 @@ class LogtoClient extends client.default {
71
69
  const context = await nodeClient.getContext(config);
72
70
  return context;
73
71
  };
72
+ this.handleSignInImplementation = (options) => async (request) => {
73
+ const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
74
+ await nodeClient.signIn(options);
75
+ const response = new Response(null, {
76
+ headers,
77
+ status: 307,
78
+ });
79
+ if (this.navigateUrl) {
80
+ response.headers.append('Location', this.navigateUrl);
81
+ }
82
+ return response;
83
+ };
74
84
  }
75
85
  async createNodeClientFromEdgeRequest(request) {
76
86
  const cookies$1 = new cookies.RequestCookies(request.headers);
@@ -1,11 +1,11 @@
1
- import { type GetContextParameters, type InteractionMode } from '@logto/node';
1
+ import { type SignInOptions, type GetContextParameters, type InteractionMode } from '@logto/node';
2
2
  import { type NextRequest } from 'next/server';
3
3
  import BaseClient from '../src/client';
4
4
  import type { LogtoNextConfig } from '../src/types.js';
5
5
  export type { AccessTokenClaims, IdTokenClaims, LogtoContext, InteractionMode, LogtoErrorCode, UserInfoResponse, } from '@logto/node';
6
6
  export default class LogtoClient extends BaseClient {
7
7
  constructor(config: LogtoNextConfig);
8
- handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => (request: Request) => Promise<Response>;
8
+ handleSignIn: (options?: SignInOptions | string, interactionMode?: InteractionMode) => (request: Request) => Promise<Response>;
9
9
  handleSignOut: (redirectUri?: string) => (request: NextRequest) => Promise<Response>;
10
10
  handleSignInCallback: (redirectTo?: string) => (request: NextRequest) => Promise<Response>;
11
11
  handleUser: (configs?: GetContextParameters) => (request: NextRequest) => Promise<Response>;
@@ -14,4 +14,5 @@ export default class LogtoClient extends BaseClient {
14
14
  nodeClient: import("@logto/node").default;
15
15
  headers: Headers;
16
16
  }>;
17
+ private readonly handleSignInImplementation;
17
18
  }
package/lib/edge/index.js CHANGED
@@ -8,17 +8,15 @@ class LogtoClient extends LogtoNextBaseClient {
8
8
  super(config, {
9
9
  NodeClient,
10
10
  });
11
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => async (request) => {
12
- const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
13
- await nodeClient.signIn(redirectUri, interactionMode);
14
- const response = new Response(null, {
15
- headers,
16
- status: 307,
17
- });
18
- if (this.navigateUrl) {
19
- response.headers.append('Location', this.navigateUrl);
11
+ this.handleSignIn = (options, interactionMode) => {
12
+ // The array function can not have multiple signatures, have to warn the deprecated usage
13
+ if (typeof options === 'string') {
14
+ console.warn('Deprecated: Use the object parameter for handleSignIn instead.');
15
+ return this.handleSignInImplementation({ redirectUri: options, interactionMode });
20
16
  }
21
- return response;
17
+ return this.handleSignInImplementation(options ?? {
18
+ redirectUri: `${this.config.baseUrl}/api/logto/sign-in-callback`,
19
+ });
22
20
  };
23
21
  this.handleSignOut = (redirectUri = this.config.baseUrl) => async (request) => {
24
22
  const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
@@ -63,6 +61,18 @@ class LogtoClient extends LogtoNextBaseClient {
63
61
  const context = await nodeClient.getContext(config);
64
62
  return context;
65
63
  };
64
+ this.handleSignInImplementation = (options) => async (request) => {
65
+ const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
66
+ await nodeClient.signIn(options);
67
+ const response = new Response(null, {
68
+ headers,
69
+ status: 307,
70
+ });
71
+ if (this.navigateUrl) {
72
+ response.headers.append('Location', this.navigateUrl);
73
+ }
74
+ return response;
75
+ };
66
76
  }
67
77
  async createNodeClientFromEdgeRequest(request) {
68
78
  const cookies = new RequestCookies(request.headers);
@@ -16,16 +16,12 @@ class LogtoClient extends client.default {
16
16
  NodeClient: NodeClient__default.default,
17
17
  });
18
18
  }
19
- /**
20
- * Init sign-in and return the url to redirect to Logto.
21
- *
22
- * @param redirectUri the uri (callbackUri) to redirect to after sign in
23
- * @param interactionMode OIDC interaction mode
24
- * @returns the url to redirect
25
- */
26
- async handleSignIn(redirectUri, interactionMode) {
19
+ async handleSignIn(options, interactionMode) {
27
20
  const nodeClient = await this.createNodeClient();
28
- await nodeClient.signIn(redirectUri, interactionMode);
21
+ const finalOptions = typeof options === 'string' || options instanceof URL
22
+ ? { redirectUri: options, interactionMode }
23
+ : options;
24
+ await nodeClient.signIn(finalOptions);
29
25
  if (!this.navigateUrl) {
30
26
  // Not expected to happen
31
27
  throw new Error('navigateUrl is not set');
@@ -1,12 +1,22 @@
1
- import { type GetContextParameters, type InteractionMode } from '@logto/node';
1
+ import { type SignInOptions, type GetContextParameters, type InteractionMode } from '@logto/node';
2
2
  import BaseClient from '../src/client';
3
3
  import type { LogtoNextConfig } from '../src/types.js';
4
4
  export type { LogtoContext, InteractionMode } from '@logto/node';
5
5
  export default class LogtoClient extends BaseClient {
6
6
  constructor(config: LogtoNextConfig);
7
+ /**
8
+ * Start the sign-in flow with the specified options.
9
+ *
10
+ * @param options The options for the sign-in flow.
11
+ */
12
+ handleSignIn(options: SignInOptions): Promise<{
13
+ url: string;
14
+ newCookie?: string;
15
+ }>;
7
16
  /**
8
17
  * Init sign-in and return the url to redirect to Logto.
9
18
  *
19
+ * @deprecated Use the object parameter instead.
10
20
  * @param redirectUri the uri (callbackUri) to redirect to after sign in
11
21
  * @param interactionMode OIDC interaction mode
12
22
  * @returns the url to redirect
@@ -8,16 +8,12 @@ class LogtoClient extends LogtoNextBaseClient {
8
8
  NodeClient,
9
9
  });
10
10
  }
11
- /**
12
- * Init sign-in and return the url to redirect to Logto.
13
- *
14
- * @param redirectUri the uri (callbackUri) to redirect to after sign in
15
- * @param interactionMode OIDC interaction mode
16
- * @returns the url to redirect
17
- */
18
- async handleSignIn(redirectUri, interactionMode) {
11
+ async handleSignIn(options, interactionMode) {
19
12
  const nodeClient = await this.createNodeClient();
20
- await nodeClient.signIn(redirectUri, interactionMode);
13
+ const finalOptions = typeof options === 'string' || options instanceof URL
14
+ ? { redirectUri: options, interactionMode }
15
+ : options;
16
+ await nodeClient.signIn(finalOptions);
21
17
  if (!this.navigateUrl) {
22
18
  // Not expected to happen
23
19
  throw new Error('navigateUrl is not set');
@@ -5,14 +5,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var navigation = require('next/navigation');
6
6
  var client = require('./client.cjs');
7
7
 
8
- /**
9
- * Init sign in process and redirect to the Logto sign-in page
10
- */
11
- const signIn = async (config, redirectUri, interactionMode) => {
8
+ async function signIn(config, options, interactionMode) {
12
9
  const client$1 = new client.default(config);
13
- const { url } = await client$1.handleSignIn(redirectUri ?? `${config.baseUrl}/callback`, interactionMode);
10
+ const finalOptions = typeof options === 'string' || options === undefined
11
+ ? { redirectUri: options ?? `${config.baseUrl}/callback`, interactionMode }
12
+ : options;
13
+ const { url } = await client$1.handleSignIn(finalOptions);
14
14
  navigation.redirect(url);
15
- };
15
+ }
16
16
  /**
17
17
  * Handle sign in callback from search params or full redirect URL, save tokens to session
18
18
  */
@@ -1,10 +1,16 @@
1
- import { type LogtoContext, type GetContextParameters, type InteractionMode } from '@logto/node';
1
+ import { type LogtoContext, type GetContextParameters, type InteractionMode, type SignInOptions } from '@logto/node';
2
2
  import type { LogtoNextConfig } from '../src/types.js';
3
3
  export type { LogtoContext, InteractionMode } from '@logto/node';
4
4
  /**
5
5
  * Init sign in process and redirect to the Logto sign-in page
6
6
  */
7
- export declare const signIn: (config: LogtoNextConfig, redirectUri?: string, interactionMode?: InteractionMode) => Promise<void>;
7
+ export declare function signIn(config: LogtoNextConfig, options?: SignInOptions): Promise<void>;
8
+ /**
9
+ * Init sign in process and redirect to the Logto sign-in page
10
+ *
11
+ * @deprecated Use the object parameter instead.
12
+ */
13
+ export declare function signIn(config: LogtoNextConfig, redirectUri?: string, interactionMode?: InteractionMode): Promise<void>;
8
14
  export declare function handleSignIn(config: LogtoNextConfig, searchParams: URLSearchParams): Promise<void>;
9
15
  export declare function handleSignIn(config: LogtoNextConfig, url: URL): Promise<void>;
10
16
  /**
@@ -1,14 +1,14 @@
1
1
  import { redirect } from 'next/navigation';
2
2
  import LogtoClient from './client.js';
3
3
 
4
- /**
5
- * Init sign in process and redirect to the Logto sign-in page
6
- */
7
- const signIn = async (config, redirectUri, interactionMode) => {
4
+ async function signIn(config, options, interactionMode) {
8
5
  const client = new LogtoClient(config);
9
- const { url } = await client.handleSignIn(redirectUri ?? `${config.baseUrl}/callback`, interactionMode);
6
+ const finalOptions = typeof options === 'string' || options === undefined
7
+ ? { redirectUri: options ?? `${config.baseUrl}/callback`, interactionMode }
8
+ : options;
9
+ const { url } = await client.handleSignIn(finalOptions);
10
10
  redirect(url);
11
- };
11
+ }
12
12
  /**
13
13
  * Handle sign in callback from search params or full redirect URL, save tokens to session
14
14
  */
package/lib/src/index.cjs CHANGED
@@ -16,13 +16,17 @@ class LogtoClient extends client.default {
16
16
  super(config, {
17
17
  NodeClient: NodeClient__default.default,
18
18
  });
19
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode, onError) => utils.buildHandler(async (request, response) => {
20
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
21
- await nodeClient.signIn(redirectUri, interactionMode);
22
- if (this.navigateUrl) {
23
- response.redirect(this.navigateUrl);
19
+ this.handleSignIn = (options, interactionMode, onError) => {
20
+ // The array function can not have multiple signatures, have to warn the deprecated usage
21
+ if (typeof options === 'string') {
22
+ console.warn('Deprecated: Use the object parameter for handleSignIn instead.');
23
+ return this.handleSignInImplementation({ redirectUri: options, interactionMode, onError });
24
24
  }
25
- }, onError);
25
+ return this.handleSignInImplementation(options ?? {
26
+ redirectUri: `${this.config.baseUrl}/api/logto/sign-in-callback`,
27
+ interactionMode,
28
+ });
29
+ };
26
30
  this.handleSignInCallback = (redirectTo = this.config.baseUrl, onError) => utils.buildHandler(async (request, response) => {
27
31
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
28
32
  if (request.url) {
@@ -80,6 +84,13 @@ class LogtoClient extends client.default {
80
84
  return handler(context);
81
85
  }, configs, onError);
82
86
  };
87
+ this.handleSignInImplementation = (options) => utils.buildHandler(async (request, response) => {
88
+ const nodeClient = await this.createNodeClientFromNextApi(request, response);
89
+ await nodeClient.signIn(options);
90
+ if (this.navigateUrl) {
91
+ response.redirect(this.navigateUrl);
92
+ }
93
+ }, options.onError);
83
94
  }
84
95
  async createNodeClientFromNextApi(request, response) {
85
96
  this.storage = new NodeClient.CookieStorage({
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { type IncomingMessage, type ServerResponse } from 'node:http';
3
- import NodeClient, { type GetContextParameters, type InteractionMode } from '@logto/node';
3
+ import NodeClient, { type SignInOptions, type GetContextParameters, type InteractionMode } from '@logto/node';
4
4
  import { type GetServerSidePropsResult, type GetServerSidePropsContext, type NextApiHandler, type NextApiRequest, type NextApiResponse } from 'next';
5
5
  import { type NextApiRequestCookies } from 'next/dist/server/api-utils/index.js';
6
6
  import LogtoNextBaseClient from './client.js';
@@ -10,7 +10,9 @@ export { LogtoError, LogtoRequestError, LogtoClientError, OidcError, Prompt, Res
10
10
  export type { AccessTokenClaims, IdTokenClaims, LogtoContext, InteractionMode, LogtoErrorCode, UserInfoResponse, } from '@logto/node';
11
11
  export default class LogtoClient extends LogtoNextBaseClient {
12
12
  constructor(config: LogtoNextConfig);
13
- handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode, onError?: ErrorHandler) => NextApiHandler;
13
+ handleSignIn: (options?: (SignInOptions & {
14
+ onError?: ErrorHandler;
15
+ }) | string, interactionMode?: InteractionMode, onError?: ErrorHandler) => NextApiHandler;
14
16
  handleSignInCallback: (redirectTo?: string, onError?: ErrorHandler) => NextApiHandler;
15
17
  handleSignOut: (redirectUri?: string, onError?: ErrorHandler) => NextApiHandler;
16
18
  handleUser: (configs?: GetContextParameters, onError?: ErrorHandler) => NextApiHandler;
@@ -22,4 +24,5 @@ export default class LogtoClient extends LogtoNextBaseClient {
22
24
  createNodeClientFromNextApi(request: IncomingMessage & {
23
25
  cookies: NextApiRequestCookies;
24
26
  }, response: ServerResponse): Promise<NodeClient>;
27
+ private readonly handleSignInImplementation;
25
28
  }
package/lib/src/index.js CHANGED
@@ -9,13 +9,17 @@ class LogtoClient extends LogtoNextBaseClient {
9
9
  super(config, {
10
10
  NodeClient,
11
11
  });
12
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode, onError) => buildHandler(async (request, response) => {
13
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
14
- await nodeClient.signIn(redirectUri, interactionMode);
15
- if (this.navigateUrl) {
16
- response.redirect(this.navigateUrl);
12
+ this.handleSignIn = (options, interactionMode, onError) => {
13
+ // The array function can not have multiple signatures, have to warn the deprecated usage
14
+ if (typeof options === 'string') {
15
+ console.warn('Deprecated: Use the object parameter for handleSignIn instead.');
16
+ return this.handleSignInImplementation({ redirectUri: options, interactionMode, onError });
17
17
  }
18
- }, onError);
18
+ return this.handleSignInImplementation(options ?? {
19
+ redirectUri: `${this.config.baseUrl}/api/logto/sign-in-callback`,
20
+ interactionMode,
21
+ });
22
+ };
19
23
  this.handleSignInCallback = (redirectTo = this.config.baseUrl, onError) => buildHandler(async (request, response) => {
20
24
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
21
25
  if (request.url) {
@@ -73,6 +77,13 @@ class LogtoClient extends LogtoNextBaseClient {
73
77
  return handler(context);
74
78
  }, configs, onError);
75
79
  };
80
+ this.handleSignInImplementation = (options) => buildHandler(async (request, response) => {
81
+ const nodeClient = await this.createNodeClientFromNextApi(request, response);
82
+ await nodeClient.signIn(options);
83
+ if (this.navigateUrl) {
84
+ response.redirect(this.navigateUrl);
85
+ }
86
+ }, options.onError);
76
87
  }
77
88
  async createNodeClientFromNextApi(request, response) {
78
89
  this.storage = new CookieStorage({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/next",
3
- "version": "3.6.1",
3
+ "version": "3.7.1",
4
4
  "type": "module",
5
5
  "main": "./lib/src/index.cjs",
6
6
  "module": "./lib/src/index.js",
@@ -47,7 +47,7 @@
47
47
  "dependencies": {
48
48
  "@edge-runtime/cookies": "^4.0.0",
49
49
  "cookie": "^0.6.0",
50
- "@logto/node": "^2.5.7"
50
+ "@logto/node": "^2.5.8"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@silverhand/eslint-config": "^6.0.1",