@logto/next 4.2.5 → 4.2.7

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.
@@ -36,8 +36,9 @@ export default class LogtoClient extends BaseClient {
36
36
  * Handle sign-in callback from Logto.
37
37
  *
38
38
  * @param callbackUrl the uri (callbackUri) to redirect to after sign in, should match the one used in handleSignIn
39
+ * @returns the postRedirectUri if configured, otherwise undefined
39
40
  */
40
- handleSignInCallback(callbackUrl: string): Promise<void>;
41
+ handleSignInCallback(callbackUrl: string): Promise<string | undefined>;
41
42
  /**
42
43
  * Get Logto context from cookies.
43
44
  *
@@ -42,10 +42,12 @@ class LogtoClient extends LogtoNextBaseClient {
42
42
  * Handle sign-in callback from Logto.
43
43
  *
44
44
  * @param callbackUrl the uri (callbackUri) to redirect to after sign in, should match the one used in handleSignIn
45
+ * @returns the postRedirectUri if configured, otherwise undefined
45
46
  */
46
47
  async handleSignInCallback(callbackUrl) {
47
48
  const nodeClient = await this.createNodeClient();
48
49
  await nodeClient.handleSignInCallback(callbackUrl);
50
+ return this.navigateUrl;
49
51
  }
50
52
  /**
51
53
  * Get Logto context from cookies.
@@ -10,15 +10,19 @@ async function signIn(config, options, interactionMode) {
10
10
  redirect(url);
11
11
  }
12
12
  /**
13
- * Handle sign in callback from search params or full redirect URL, save tokens to session
13
+ * Handle sign in callback from search params or full redirect URL, save tokens to session,
14
+ * and redirect to postRedirectUri if configured
14
15
  * @param config The Logto configuration object
15
16
  * @param searchParamsOrUrl Either URLSearchParams from the callback URL or the complete URL object
16
17
  */
17
18
  async function handleSignIn(config, searchParamsOrUrl) {
18
19
  const client = new LogtoClient(config);
19
- await client.handleSignInCallback(searchParamsOrUrl instanceof URL
20
+ const postRedirectUri = await client.handleSignInCallback(searchParamsOrUrl instanceof URL
20
21
  ? searchParamsOrUrl.toString()
21
22
  : `${config.baseUrl}/callback?${searchParamsOrUrl.toString()}`);
23
+ if (postRedirectUri) {
24
+ redirect(postRedirectUri);
25
+ }
22
26
  }
23
27
  /**
24
28
  * Init sign out process, clear session, and redirect to the Logto sign-out page
@@ -20,7 +20,7 @@ export default class LogtoClient extends LogtoNextBaseClient {
20
20
  getAccessToken: (request: NextApiRequest, response: NextApiResponse, resource: string) => Promise<string>;
21
21
  getOrganizationToken: (request: NextApiRequest, response: NextApiResponse, organizationId: string) => Promise<string>;
22
22
  withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters, onError?: ErrorHandler) => NextApiHandler;
23
- withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters, onError?: ((error: unknown) => unknown) | undefined) => (context: GetServerSidePropsContext) => Promise<NextApiHandler>;
23
+ withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters, onError?: ((error: unknown) => unknown) | undefined) => (context: GetServerSidePropsContext) => Promise<unknown>;
24
24
  createNodeClientFromNextApi(request: IncomingMessage & {
25
25
  cookies: NextApiRequestCookies;
26
26
  }, response: ServerResponse): Promise<NodeClient>;
package/lib/src/index.js CHANGED
@@ -24,7 +24,13 @@ class LogtoClient extends LogtoNextBaseClient {
24
24
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
25
25
  if (request.url) {
26
26
  await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
27
- response.redirect(redirectTo);
27
+ // Check if there's a stored navigation URL (from postRedirectUri) first
28
+ if (this.navigateUrl) {
29
+ response.redirect(this.navigateUrl);
30
+ }
31
+ else {
32
+ response.redirect(redirectTo);
33
+ }
28
34
  }
29
35
  }, onError);
30
36
  this.handleSignOut = (redirectUri = this.config.baseUrl, onError) => buildHandler(async (request, response) => {
@@ -73,9 +79,19 @@ class LogtoClient extends LogtoNextBaseClient {
73
79
  return handler(request, response);
74
80
  }, onError);
75
81
  this.withLogtoSsr = (handler, configs = {}, onError) => async (context) => {
76
- return this.withLogtoApiRoute(async () => {
77
- return handler(context);
78
- }, configs, onError);
82
+ try {
83
+ const nodeClient = await this.createNodeClientFromNextApi(context.req, context.res);
84
+ const user = await nodeClient.getContext(configs);
85
+ // eslint-disable-next-line @silverhand/fp/no-mutating-methods
86
+ Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
87
+ return await handler(context);
88
+ }
89
+ catch (error) {
90
+ if (onError) {
91
+ return onError(error);
92
+ }
93
+ throw error;
94
+ }
79
95
  };
80
96
  this.handleSignInImplementation = (options) => buildHandler(async (request, response) => {
81
97
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/next",
3
- "version": "4.2.5",
3
+ "version": "4.2.7",
4
4
  "type": "module",
5
5
  "module": "./lib/src/index.js",
6
6
  "types": "./lib/src/index.d.ts",
@@ -43,7 +43,7 @@
43
43
  "dependencies": {
44
44
  "@edge-runtime/cookies": "^5.0.0",
45
45
  "cookie": "^1.0.0",
46
- "@logto/node": "^3.1.6"
46
+ "@logto/node": "^3.1.7"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@silverhand/eslint-config": "^6.0.1",
@@ -53,11 +53,11 @@
53
53
  "@vitest/coverage-v8": "^2.1.9",
54
54
  "eslint": "^8.57.0",
55
55
  "lint-staged": "^15.0.0",
56
- "next": "^15.2.4",
56
+ "next": "^15.5.9",
57
57
  "next-test-api-route-handler": "^4.0.14",
58
58
  "prettier": "^3.0.0",
59
- "react": "19.0.0",
60
- "react-dom": "19.0.0",
59
+ "react": "19.2.3",
60
+ "react-dom": "19.2.3",
61
61
  "typescript": "^5.3.3",
62
62
  "vitest": "^2.1.9"
63
63
  },