@logto/next 3.2.6 → 3.3.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.
@@ -18,16 +18,17 @@ const signIn = async (config, redirectUri, interactionMode) => {
18
18
  navigation.redirect(url);
19
19
  };
20
20
  /**
21
- * Handle sign in callback from search params, save tokens to session
21
+ * Handle sign in callback from search params or full redirect URL, save tokens to session
22
22
  */
23
- const handleSignIn = async (config, searchParams) => {
24
- const search = searchParams.toString();
23
+ async function handleSignIn(config, searchParamsOrUrl) {
25
24
  const client$1 = new client.default(config);
26
- const newCookie = await client$1.handleSignInCallback(await cookie.getCookies(config), `${config.baseUrl}/callback?${search}`);
25
+ const newCookie = await client$1.handleSignInCallback(await cookie.getCookies(config), searchParamsOrUrl instanceof URL
26
+ ? searchParamsOrUrl.toString()
27
+ : `${config.baseUrl}/callback?${searchParamsOrUrl.toString()}`);
27
28
  if (newCookie) {
28
29
  await cookie.setCookies(newCookie, config);
29
30
  }
30
- };
31
+ }
31
32
  /**
32
33
  * Init sign out process, clear session, and redirect to the Logto sign-out page
33
34
  */
@@ -5,10 +5,8 @@ export type { LogtoContext, InteractionMode } from '@logto/node';
5
5
  * Init sign in process and redirect to the Logto sign-in page
6
6
  */
7
7
  export declare const signIn: (config: LogtoNextConfig, redirectUri?: string, interactionMode?: InteractionMode) => Promise<void>;
8
- /**
9
- * Handle sign in callback from search params, save tokens to session
10
- */
11
- export declare const handleSignIn: (config: LogtoNextConfig, searchParams: URLSearchParams) => Promise<void>;
8
+ export declare function handleSignIn(config: LogtoNextConfig, searchParams: URLSearchParams): Promise<void>;
9
+ export declare function handleSignIn(config: LogtoNextConfig, url: URL): Promise<void>;
12
10
  /**
13
11
  * Init sign out process, clear session, and redirect to the Logto sign-out page
14
12
  */
@@ -14,16 +14,17 @@ const signIn = async (config, redirectUri, interactionMode) => {
14
14
  redirect(url);
15
15
  };
16
16
  /**
17
- * Handle sign in callback from search params, save tokens to session
17
+ * Handle sign in callback from search params or full redirect URL, save tokens to session
18
18
  */
19
- const handleSignIn = async (config, searchParams) => {
20
- const search = searchParams.toString();
19
+ async function handleSignIn(config, searchParamsOrUrl) {
21
20
  const client = new LogtoClient(config);
22
- const newCookie = await client.handleSignInCallback(await getCookies(config), `${config.baseUrl}/callback?${search}`);
21
+ const newCookie = await client.handleSignInCallback(await getCookies(config), searchParamsOrUrl instanceof URL
22
+ ? searchParamsOrUrl.toString()
23
+ : `${config.baseUrl}/callback?${searchParamsOrUrl.toString()}`);
23
24
  if (newCookie) {
24
25
  await setCookies(newCookie, config);
25
26
  }
26
- };
27
+ }
27
28
  /**
28
29
  * Init sign out process, clear session, and redirect to the Logto sign-out page
29
30
  */
package/lib/src/index.cjs CHANGED
@@ -42,24 +42,32 @@ class LogtoClient extends client.default {
42
42
  this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
43
43
  response.json(request.user);
44
44
  }, configs);
45
- this.handleAuthRoutes = (configs) => (request, response) => {
46
- const { action } = request.query;
47
- if (action === 'sign-in') {
48
- return this.handleSignIn()(request, response);
49
- }
50
- if (action === 'sign-up') {
51
- return this.handleSignIn(undefined, 'signUp')(request, response);
52
- }
53
- if (action === 'sign-in-callback') {
54
- return this.handleSignInCallback()(request, response);
55
- }
56
- if (action === 'sign-out') {
57
- return this.handleSignOut()(request, response);
45
+ this.handleAuthRoutes = (configs, onError) => (request, response) => {
46
+ try {
47
+ const { action } = request.query;
48
+ if (action === 'sign-in') {
49
+ return this.handleSignIn()(request, response);
50
+ }
51
+ if (action === 'sign-up') {
52
+ return this.handleSignIn(undefined, 'signUp')(request, response);
53
+ }
54
+ if (action === 'sign-in-callback') {
55
+ return this.handleSignInCallback()(request, response);
56
+ }
57
+ if (action === 'sign-out') {
58
+ return this.handleSignOut()(request, response);
59
+ }
60
+ if (action === 'user') {
61
+ return this.handleUser(configs)(request, response);
62
+ }
63
+ response.status(404).end();
58
64
  }
59
- if (action === 'user') {
60
- return this.handleUser(configs)(request, response);
65
+ catch (error) {
66
+ if (onError) {
67
+ return onError(request, response, error);
68
+ }
69
+ throw error;
61
70
  }
62
- response.status(404).end();
63
71
  };
64
72
  this.withLogtoApiRoute = (handler, config = {}, onError) => async (request, response) => {
65
73
  try {
@@ -14,7 +14,7 @@ export default class LogtoClient extends LogtoNextBaseClient {
14
14
  handleSignInCallback: (redirectTo?: string) => NextApiHandler;
15
15
  handleSignOut: (redirectUri?: string) => NextApiHandler;
16
16
  handleUser: (configs?: GetContextParameters) => NextApiHandler;
17
- handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
17
+ handleAuthRoutes: (configs?: GetContextParameters, onError?: ((request: NextApiRequest, response: NextApiResponse, error: unknown) => unknown) | undefined) => NextApiHandler;
18
18
  withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters, onError?: ((request: NextApiRequest, response: NextApiResponse, error: unknown) => unknown) | undefined) => NextApiHandler;
19
19
  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>;
20
20
  createNodeClientFromNextApi(request: IncomingMessage & {
package/lib/src/index.js CHANGED
@@ -35,24 +35,32 @@ class LogtoClient extends LogtoNextBaseClient {
35
35
  this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
36
36
  response.json(request.user);
37
37
  }, configs);
38
- this.handleAuthRoutes = (configs) => (request, response) => {
39
- const { action } = request.query;
40
- if (action === 'sign-in') {
41
- return this.handleSignIn()(request, response);
42
- }
43
- if (action === 'sign-up') {
44
- return this.handleSignIn(undefined, 'signUp')(request, response);
45
- }
46
- if (action === 'sign-in-callback') {
47
- return this.handleSignInCallback()(request, response);
48
- }
49
- if (action === 'sign-out') {
50
- return this.handleSignOut()(request, response);
38
+ this.handleAuthRoutes = (configs, onError) => (request, response) => {
39
+ try {
40
+ const { action } = request.query;
41
+ if (action === 'sign-in') {
42
+ return this.handleSignIn()(request, response);
43
+ }
44
+ if (action === 'sign-up') {
45
+ return this.handleSignIn(undefined, 'signUp')(request, response);
46
+ }
47
+ if (action === 'sign-in-callback') {
48
+ return this.handleSignInCallback()(request, response);
49
+ }
50
+ if (action === 'sign-out') {
51
+ return this.handleSignOut()(request, response);
52
+ }
53
+ if (action === 'user') {
54
+ return this.handleUser(configs)(request, response);
55
+ }
56
+ response.status(404).end();
51
57
  }
52
- if (action === 'user') {
53
- return this.handleUser(configs)(request, response);
58
+ catch (error) {
59
+ if (onError) {
60
+ return onError(request, response, error);
61
+ }
62
+ throw error;
54
63
  }
55
- response.status(404).end();
56
64
  };
57
65
  this.withLogtoApiRoute = (handler, config = {}, onError) => async (request, response) => {
58
66
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/next",
3
- "version": "3.2.6",
3
+ "version": "3.3.0",
4
4
  "type": "module",
5
5
  "main": "./lib/src/index.cjs",
6
6
  "module": "./lib/src/index.js",
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@edge-runtime/cookies": "^4.0.0",
49
- "@logto/node": "^2.5.0"
49
+ "@logto/node": "^2.5.1"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@silverhand/eslint-config": "^6.0.1",
@@ -55,7 +55,7 @@
55
55
  "@vitest/coverage-v8": "^1.4.0",
56
56
  "eslint": "^8.57.0",
57
57
  "lint-staged": "^15.0.0",
58
- "next": "^14.0.0",
58
+ "next": "^14.1.1",
59
59
  "next-test-api-route-handler": "^4.0.0",
60
60
  "prettier": "^3.0.0",
61
61
  "react": "^18.2.0",