@logto/next 2.1.4 → 2.2.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.
@@ -71,7 +71,7 @@ class LogtoClient extends client.default {
71
71
  };
72
72
  this.getLogtoContext = async (request, config = {}) => {
73
73
  const { nodeClient } = await this.createNodeClientFromEdgeRequest(request);
74
- const context = await nodeClient.getContext();
74
+ const context = await nodeClient.getContext(config);
75
75
  return context;
76
76
  };
77
77
  }
package/lib/edge/index.js CHANGED
@@ -63,7 +63,7 @@ class LogtoClient extends LogtoNextBaseClient {
63
63
  };
64
64
  this.getLogtoContext = async (request, config = {}) => {
65
65
  const { nodeClient } = await this.createNodeClientFromEdgeRequest(request);
66
- const context = await nodeClient.getContext();
66
+ const context = await nodeClient.getContext(config);
67
67
  return context;
68
68
  };
69
69
  }
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var NodeClient = require('@logto/node/edge');
6
+ var client = require('../src/client.cjs');
7
+ var session = require('../src/session.cjs');
8
+
9
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
+
11
+ var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
12
+
13
+ class LogtoClient extends client.default {
14
+ constructor(config) {
15
+ super(config, {
16
+ NodeClient: NodeClient__default.default,
17
+ });
18
+ }
19
+ /**
20
+ * Init sign-in and return the url to redirect to Logto.
21
+ *
22
+ * @param cookie the raw cookie string
23
+ * @param redirectUri the uri (callbackUri) to redirect to after sign in
24
+ * @param interactionMode OIDC interaction mode
25
+ * @returns the url to redirect to and new cookie if any
26
+ */
27
+ async handleSignIn(cookie, redirectUri, interactionMode) {
28
+ const { nodeClient, session } = await this.createNodeClientFromHeaders(cookie);
29
+ await nodeClient.signIn(redirectUri, interactionMode);
30
+ if (!this.navigateUrl) {
31
+ // Not expected to happen
32
+ throw new Error('navigateUrl is not set');
33
+ }
34
+ return {
35
+ url: this.navigateUrl,
36
+ newCookie: await session.getValues?.(),
37
+ };
38
+ }
39
+ /**
40
+ * Init sign-out and return the url to redirect to Logto.
41
+ *
42
+ * @param cookie the raw cookie string
43
+ * @param redirectUri the uri (postSignOutUri) to redirect to after sign out
44
+ * @returns the url to redirect to
45
+ */
46
+ async handleSignOut(cookie, redirectUri = this.config.baseUrl) {
47
+ const { nodeClient } = await this.createNodeClientFromHeaders(cookie);
48
+ await nodeClient.signOut(redirectUri);
49
+ if (!this.navigateUrl) {
50
+ // Not expected to happen
51
+ throw new Error('navigateUrl is not set');
52
+ }
53
+ return this.navigateUrl;
54
+ }
55
+ /**
56
+ * Handle sign-in callback from Logto.
57
+ *
58
+ * @param cookie the raw cookie string
59
+ * @param callbackUrl the uri (callbackUri) to redirect to after sign in, should match the one used in handleSignIn
60
+ * @returns new cookie if any
61
+ */
62
+ async handleSignInCallback(cookie, callbackUrl) {
63
+ const { nodeClient, session } = await this.createNodeClientFromHeaders(cookie);
64
+ await nodeClient.handleSignInCallback(callbackUrl);
65
+ return session.getValues?.();
66
+ }
67
+ /**
68
+ * Get Logto context from cookies.
69
+ *
70
+ * @param cookie the raw cookie string
71
+ * @param config additional configs of GetContextParameters
72
+ * @returns LogtoContext
73
+ */
74
+ async getLogtoContext(cookie, config = {}) {
75
+ const { nodeClient } = await this.createNodeClientFromHeaders(cookie);
76
+ const context = await nodeClient.getContext(config);
77
+ return context;
78
+ }
79
+ async createNodeClientFromHeaders(cookie) {
80
+ const session$1 = await session.createSession({
81
+ secret: this.config.cookieSecret,
82
+ crypto,
83
+ }, cookie);
84
+ const nodeClient = super.createNodeClient(session$1);
85
+ return { nodeClient, session: session$1 };
86
+ }
87
+ }
88
+
89
+ exports.default = LogtoClient;
@@ -0,0 +1,44 @@
1
+ import { type GetContextParameters, type InteractionMode } from '@logto/node';
2
+ import BaseClient from '../src/client';
3
+ import type { LogtoNextConfig } from '../src/types.js';
4
+ export type { LogtoContext, InteractionMode } from '@logto/node';
5
+ export default class LogtoClient extends BaseClient {
6
+ constructor(config: LogtoNextConfig);
7
+ /**
8
+ * Init sign-in and return the url to redirect to Logto.
9
+ *
10
+ * @param cookie the raw cookie string
11
+ * @param redirectUri the uri (callbackUri) to redirect to after sign in
12
+ * @param interactionMode OIDC interaction mode
13
+ * @returns the url to redirect to and new cookie if any
14
+ */
15
+ handleSignIn(cookie: string, redirectUri: string, interactionMode?: InteractionMode): Promise<{
16
+ url: string;
17
+ newCookie?: string;
18
+ }>;
19
+ /**
20
+ * Init sign-out and return the url to redirect to Logto.
21
+ *
22
+ * @param cookie the raw cookie string
23
+ * @param redirectUri the uri (postSignOutUri) to redirect to after sign out
24
+ * @returns the url to redirect to
25
+ */
26
+ handleSignOut(cookie: string, redirectUri?: string): Promise<string>;
27
+ /**
28
+ * Handle sign-in callback from Logto.
29
+ *
30
+ * @param cookie the raw cookie string
31
+ * @param callbackUrl the uri (callbackUri) to redirect to after sign in, should match the one used in handleSignIn
32
+ * @returns new cookie if any
33
+ */
34
+ handleSignInCallback(cookie: string, callbackUrl: string): Promise<string | undefined>;
35
+ /**
36
+ * Get Logto context from cookies.
37
+ *
38
+ * @param cookie the raw cookie string
39
+ * @param config additional configs of GetContextParameters
40
+ * @returns LogtoContext
41
+ */
42
+ getLogtoContext(cookie: string, config?: GetContextParameters): Promise<import("@logto/node").LogtoContext>;
43
+ private createNodeClientFromHeaders;
44
+ }
@@ -0,0 +1,81 @@
1
+ import NodeClient from '@logto/node/edge';
2
+ import LogtoNextBaseClient from '../src/client.js';
3
+ import { createSession } from '../src/session.js';
4
+
5
+ class LogtoClient extends LogtoNextBaseClient {
6
+ constructor(config) {
7
+ super(config, {
8
+ NodeClient,
9
+ });
10
+ }
11
+ /**
12
+ * Init sign-in and return the url to redirect to Logto.
13
+ *
14
+ * @param cookie the raw cookie string
15
+ * @param redirectUri the uri (callbackUri) to redirect to after sign in
16
+ * @param interactionMode OIDC interaction mode
17
+ * @returns the url to redirect to and new cookie if any
18
+ */
19
+ async handleSignIn(cookie, redirectUri, interactionMode) {
20
+ const { nodeClient, session } = await this.createNodeClientFromHeaders(cookie);
21
+ await nodeClient.signIn(redirectUri, interactionMode);
22
+ if (!this.navigateUrl) {
23
+ // Not expected to happen
24
+ throw new Error('navigateUrl is not set');
25
+ }
26
+ return {
27
+ url: this.navigateUrl,
28
+ newCookie: await session.getValues?.(),
29
+ };
30
+ }
31
+ /**
32
+ * Init sign-out and return the url to redirect to Logto.
33
+ *
34
+ * @param cookie the raw cookie string
35
+ * @param redirectUri the uri (postSignOutUri) to redirect to after sign out
36
+ * @returns the url to redirect to
37
+ */
38
+ async handleSignOut(cookie, redirectUri = this.config.baseUrl) {
39
+ const { nodeClient } = await this.createNodeClientFromHeaders(cookie);
40
+ await nodeClient.signOut(redirectUri);
41
+ if (!this.navigateUrl) {
42
+ // Not expected to happen
43
+ throw new Error('navigateUrl is not set');
44
+ }
45
+ return this.navigateUrl;
46
+ }
47
+ /**
48
+ * Handle sign-in callback from Logto.
49
+ *
50
+ * @param cookie the raw cookie string
51
+ * @param callbackUrl the uri (callbackUri) to redirect to after sign in, should match the one used in handleSignIn
52
+ * @returns new cookie if any
53
+ */
54
+ async handleSignInCallback(cookie, callbackUrl) {
55
+ const { nodeClient, session } = await this.createNodeClientFromHeaders(cookie);
56
+ await nodeClient.handleSignInCallback(callbackUrl);
57
+ return session.getValues?.();
58
+ }
59
+ /**
60
+ * Get Logto context from cookies.
61
+ *
62
+ * @param cookie the raw cookie string
63
+ * @param config additional configs of GetContextParameters
64
+ * @returns LogtoContext
65
+ */
66
+ async getLogtoContext(cookie, config = {}) {
67
+ const { nodeClient } = await this.createNodeClientFromHeaders(cookie);
68
+ const context = await nodeClient.getContext(config);
69
+ return context;
70
+ }
71
+ async createNodeClientFromHeaders(cookie) {
72
+ const session = await createSession({
73
+ secret: this.config.cookieSecret,
74
+ crypto,
75
+ }, cookie);
76
+ const nodeClient = super.createNodeClient(session);
77
+ return { nodeClient, session };
78
+ }
79
+ }
80
+
81
+ export { LogtoClient as default };
@@ -0,0 +1 @@
1
+ export {};
@@ -57,11 +57,13 @@ const wrapSession = async (session, secret, crypto) => {
57
57
  };
58
58
  const createSession = async ({ secret, crypto }, cookie, setCookie) => {
59
59
  const data = await unwrapSession(cookie, secret, crypto);
60
+ const getValues = async () => wrapSession(session, secret, crypto);
60
61
  const session = {
61
62
  ...data,
62
63
  save: async () => {
63
- setCookie(await wrapSession(session, secret, crypto));
64
+ setCookie?.(await getValues());
64
65
  },
66
+ getValues,
65
67
  };
66
68
  return session;
67
69
  };
@@ -5,5 +5,5 @@ type SessionConfigs = {
5
5
  secret: string;
6
6
  crypto: Crypto;
7
7
  };
8
- export declare const createSession: ({ secret, crypto }: SessionConfigs, cookie: string, setCookie: (value: string) => void) => Promise<Session>;
8
+ export declare const createSession: ({ secret, crypto }: SessionConfigs, cookie: string, setCookie?: ((value: string) => void) | undefined) => Promise<Session>;
9
9
  export {};
@@ -55,11 +55,13 @@ const wrapSession = async (session, secret, crypto) => {
55
55
  };
56
56
  const createSession = async ({ secret, crypto }, cookie, setCookie) => {
57
57
  const data = await unwrapSession(cookie, secret, crypto);
58
+ const getValues = async () => wrapSession(session, secret, crypto);
58
59
  const session = {
59
60
  ...data,
60
61
  save: async () => {
61
- setCookie(await wrapSession(session, secret, crypto));
62
+ setCookie?.(await getValues());
62
63
  },
64
+ getValues,
63
65
  };
64
66
  return session;
65
67
  };
@@ -8,6 +8,7 @@ export type SessionData = {
8
8
  };
9
9
  export type Session = SessionData & {
10
10
  save: () => Promise<void>;
11
+ getValues?: () => Promise<string>;
11
12
  };
12
13
  export type LogtoNextConfig = LogtoConfig & {
13
14
  cookieSecret: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/next",
3
- "version": "2.1.4",
3
+ "version": "2.2.1",
4
4
  "type": "module",
5
5
  "main": "./lib/src/index.cjs",
6
6
  "module": "./lib/src/index.js",
@@ -12,6 +12,9 @@
12
12
  ],
13
13
  "edge": [
14
14
  "./lib/edge/index.d.ts"
15
+ ],
16
+ "server-actions": [
17
+ "./lib/server-actions/index.d.ts"
15
18
  ]
16
19
  }
17
20
  },
@@ -25,6 +28,11 @@
25
28
  "require": "./lib/edge/index.cjs",
26
29
  "import": "./lib/edge/index.js",
27
30
  "types": "./lib/edge/index.d.ts"
31
+ },
32
+ "./server-actions": {
33
+ "require": "./lib/server-actions/index.cjs",
34
+ "import": "./lib/server-actions/index.js",
35
+ "types": "./lib/server-actions/index.d.ts"
28
36
  }
29
37
  },
30
38
  "files": [
@@ -52,7 +60,7 @@
52
60
  "jest-location-mock": "^2.0.0",
53
61
  "jest-matcher-specific-error": "^1.0.0",
54
62
  "lint-staged": "^14.0.0",
55
- "next": "^13.0.4",
63
+ "next": "^13.5.3",
56
64
  "next-test-api-route-handler": "^3.1.6",
57
65
  "prettier": "^3.0.0",
58
66
  "react": "^18.2.0",