@logto/next 3.7.1 → 4.0.3

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/lib/edge/index.js CHANGED
@@ -80,7 +80,7 @@ class LogtoClient extends LogtoNextBaseClient {
80
80
  const responseCookies = new ResponseCookies(headers);
81
81
  this.storage = new CookieStorage({
82
82
  encryptionKey: this.config.cookieSecret,
83
- cookieKey: `logto:${this.config.appId}`,
83
+ cookieKey: `logto_${this.config.appId}`,
84
84
  isSecure: this.config.cookieSecure,
85
85
  getCookie: (name) => {
86
86
  return cookies.get(name)?.value ?? '';
@@ -62,7 +62,7 @@ class LogtoClient extends LogtoNextBaseClient {
62
62
  const { cookies } = await import('next/headers');
63
63
  this.storage = new CookieStorage({
64
64
  encryptionKey: this.config.cookieSecret,
65
- cookieKey: `logto:${this.config.appId}`,
65
+ cookieKey: `logto_${this.config.appId}`,
66
66
  isSecure: this.config.cookieSecure,
67
67
  getCookie: (...args) => {
68
68
  return cookies().get(...args)?.value ?? '';
package/lib/src/index.js CHANGED
@@ -88,7 +88,7 @@ class LogtoClient extends LogtoNextBaseClient {
88
88
  async createNodeClientFromNextApi(request, response) {
89
89
  this.storage = new CookieStorage({
90
90
  encryptionKey: this.config.cookieSecret,
91
- cookieKey: `logto:${this.config.appId}`,
91
+ cookieKey: `logto_${this.config.appId}`,
92
92
  isSecure: this.config.cookieSecure,
93
93
  getCookie: (name) => {
94
94
  return request.cookies[name] ?? '';
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@logto/next",
3
- "version": "3.7.1",
3
+ "version": "4.0.3",
4
4
  "type": "module",
5
- "main": "./lib/src/index.cjs",
6
5
  "module": "./lib/src/index.js",
7
6
  "types": "./lib/src/index.d.ts",
8
7
  "typesVersions": {
@@ -20,17 +19,14 @@
20
19
  },
21
20
  "exports": {
22
21
  ".": {
23
- "require": "./lib/src/index.cjs",
24
22
  "import": "./lib/src/index.js",
25
23
  "types": "./lib/src/index.d.ts"
26
24
  },
27
25
  "./edge": {
28
- "require": "./lib/edge/index.cjs",
29
26
  "import": "./lib/edge/index.js",
30
27
  "types": "./lib/edge/index.d.ts"
31
28
  },
32
29
  "./server-actions": {
33
- "require": "./lib/server-actions/index.cjs",
34
30
  "import": "./lib/server-actions/index.js",
35
31
  "types": "./lib/server-actions/index.d.ts"
36
32
  }
@@ -45,9 +41,9 @@
45
41
  "directory": "packages/next"
46
42
  },
47
43
  "dependencies": {
48
- "@edge-runtime/cookies": "^4.0.0",
49
- "cookie": "^0.6.0",
50
- "@logto/node": "^2.5.8"
44
+ "@edge-runtime/cookies": "^5.0.0",
45
+ "cookie": "^1.0.0",
46
+ "@logto/node": "^3.0.3"
51
47
  },
52
48
  "devDependencies": {
53
49
  "@silverhand/eslint-config": "^6.0.1",
@@ -57,7 +53,7 @@
57
53
  "@vitest/coverage-v8": "^1.6.0",
58
54
  "eslint": "^8.57.0",
59
55
  "lint-staged": "^15.0.0",
60
- "next": "^14.1.1",
56
+ "next": "^14.2.10",
61
57
  "next-test-api-route-handler": "^4.0.0",
62
58
  "prettier": "^3.0.0",
63
59
  "react": "^18.2.0",
@@ -82,6 +78,6 @@
82
78
  "build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
83
79
  "lint": "eslint --ext .ts src",
84
80
  "test": "vitest",
85
- "test:coverage": "node test.cjs && vitest --silent --coverage"
81
+ "test:coverage": "vitest --silent --coverage"
86
82
  }
87
83
  }
@@ -1,111 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var cookies = require('@edge-runtime/cookies');
6
- var NodeClient$1 = require('@logto/node');
7
- var NodeClient = require('@logto/node/edge');
8
- var client = require('../src/client.cjs');
9
-
10
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
-
12
- var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
13
-
14
- class LogtoClient extends client.default {
15
- constructor(config) {
16
- super(config, {
17
- NodeClient: NodeClient__default.default,
18
- });
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 });
24
- }
25
- return this.handleSignInImplementation(options ?? {
26
- redirectUri: `${this.config.baseUrl}/api/logto/sign-in-callback`,
27
- });
28
- };
29
- this.handleSignOut = (redirectUri = this.config.baseUrl) => async (request) => {
30
- const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
31
- await nodeClient.signOut(redirectUri);
32
- await this.storage?.destroy();
33
- const response = new Response(null, {
34
- headers,
35
- status: 307,
36
- });
37
- if (this.navigateUrl) {
38
- response.headers.append('Location', this.navigateUrl);
39
- }
40
- return response;
41
- };
42
- this.handleSignInCallback = (redirectTo = this.config.baseUrl) => async (request) => {
43
- const { nodeClient, headers } = await this.createNodeClientFromEdgeRequest(request);
44
- if (request.url) {
45
- // When app is running behind reverse proxy which is common for edge runtime,
46
- // the `request.url`'s domain may not be expected, replace to the configured baseUrl
47
- const requestUrl = new URL(request.url);
48
- const callbackUrl = new URL(`${requestUrl.pathname}${requestUrl.search}${requestUrl.hash}`, this.config.baseUrl);
49
- await nodeClient.handleSignInCallback(callbackUrl.toString());
50
- }
51
- const response = new Response(null, {
52
- status: 307,
53
- headers,
54
- });
55
- response.headers.append('Location', redirectTo);
56
- return response;
57
- };
58
- this.handleUser = (configs) => async (request) => {
59
- const context = await this.getLogtoContext(request, configs);
60
- return new Response(JSON.stringify(context), {
61
- status: 200,
62
- headers: {
63
- 'content-type': 'application/json',
64
- },
65
- });
66
- };
67
- this.getLogtoContext = async (request, config = {}) => {
68
- const { nodeClient } = await this.createNodeClientFromEdgeRequest(request);
69
- const context = await nodeClient.getContext(config);
70
- return context;
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
- };
84
- }
85
- async createNodeClientFromEdgeRequest(request) {
86
- const cookies$1 = new cookies.RequestCookies(request.headers);
87
- const headers = new Headers();
88
- const responseCookies = new cookies.ResponseCookies(headers);
89
- this.storage = new NodeClient$1.CookieStorage({
90
- encryptionKey: this.config.cookieSecret,
91
- cookieKey: `logto:${this.config.appId}`,
92
- isSecure: this.config.cookieSecure,
93
- getCookie: (name) => {
94
- return cookies$1.get(name)?.value ?? '';
95
- },
96
- setCookie: (name, value, options) => {
97
- responseCookies.set(name, value, options);
98
- },
99
- });
100
- await this.storage.init();
101
- const nodeClient = new this.adapters.NodeClient(this.config, {
102
- storage: this.storage,
103
- navigate: (url) => {
104
- this.navigateUrl = url;
105
- },
106
- });
107
- return { nodeClient, headers };
108
- }
109
- }
110
-
111
- exports.default = LogtoClient;
@@ -1,95 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var NodeClient$1 = require('@logto/node');
6
- var NodeClient = require('@logto/node/edge');
7
- var client = require('../src/client.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
- async handleSignIn(options, interactionMode) {
20
- const nodeClient = await this.createNodeClient();
21
- const finalOptions = typeof options === 'string' || options instanceof URL
22
- ? { redirectUri: options, interactionMode }
23
- : options;
24
- await nodeClient.signIn(finalOptions);
25
- if (!this.navigateUrl) {
26
- // Not expected to happen
27
- throw new Error('navigateUrl is not set');
28
- }
29
- return {
30
- url: this.navigateUrl,
31
- };
32
- }
33
- /**
34
- * Init sign-out and return the url to redirect to Logto.
35
- *
36
- * @param redirectUri the uri (postSignOutUri) to redirect to after sign out
37
- * @returns the url to redirect to
38
- */
39
- async handleSignOut(redirectUri = this.config.baseUrl) {
40
- const nodeClient = await this.createNodeClient();
41
- await nodeClient.signOut(redirectUri);
42
- await this.storage?.destroy();
43
- if (!this.navigateUrl) {
44
- // Not expected to happen
45
- throw new Error('navigateUrl is not set');
46
- }
47
- return this.navigateUrl;
48
- }
49
- /**
50
- * Handle sign-in callback from Logto.
51
- *
52
- * @param callbackUrl the uri (callbackUri) to redirect to after sign in, should match the one used in handleSignIn
53
- */
54
- async handleSignInCallback(callbackUrl) {
55
- const nodeClient = await this.createNodeClient();
56
- await nodeClient.handleSignInCallback(callbackUrl);
57
- }
58
- /**
59
- * Get Logto context from cookies.
60
- *
61
- * @param config additional configs of GetContextParameters
62
- * @returns LogtoContext
63
- */
64
- async getLogtoContext(config = {}) {
65
- const nodeClient = await this.createNodeClient({ ignoreCookieChange: true });
66
- const context = await nodeClient.getContext(config);
67
- return context;
68
- }
69
- async createNodeClient({ ignoreCookieChange } = {}) {
70
- const { cookies } = await import('next/headers');
71
- this.storage = new NodeClient$1.CookieStorage({
72
- encryptionKey: this.config.cookieSecret,
73
- cookieKey: `logto:${this.config.appId}`,
74
- isSecure: this.config.cookieSecure,
75
- getCookie: (...args) => {
76
- return cookies().get(...args)?.value ?? '';
77
- },
78
- setCookie: (...args) => {
79
- // In server component (RSC), it is not allowed to modify cookies, see https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options.
80
- if (!ignoreCookieChange) {
81
- cookies().set(...args);
82
- }
83
- },
84
- });
85
- await this.storage.init();
86
- return new this.adapters.NodeClient(this.config, {
87
- storage: this.storage,
88
- navigate: (url) => {
89
- this.navigateUrl = url;
90
- },
91
- });
92
- }
93
- }
94
-
95
- exports.default = LogtoClient;
@@ -1,105 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var navigation = require('next/navigation');
6
- var client = require('./client.cjs');
7
-
8
- async function signIn(config, options, interactionMode) {
9
- const client$1 = new client.default(config);
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
- navigation.redirect(url);
15
- }
16
- /**
17
- * Handle sign in callback from search params or full redirect URL, save tokens to session
18
- */
19
- async function handleSignIn(config, searchParamsOrUrl) {
20
- const client$1 = new client.default(config);
21
- await client$1.handleSignInCallback(searchParamsOrUrl instanceof URL
22
- ? searchParamsOrUrl.toString()
23
- : `${config.baseUrl}/callback?${searchParamsOrUrl.toString()}`);
24
- }
25
- /**
26
- * Init sign out process, clear session, and redirect to the Logto sign-out page
27
- */
28
- const signOut = async (config, redirectUri) => {
29
- const client$1 = new client.default(config);
30
- const url = await client$1.handleSignOut(redirectUri);
31
- navigation.redirect(url);
32
- };
33
- /**
34
- * Get Logto context from session, including auth status and claims
35
- */
36
- const getLogtoContext = async (config, getContextParameters) => {
37
- const client$1 = new client.default(config);
38
- return client$1.getLogtoContext(getContextParameters);
39
- };
40
- /**
41
- * Get organization tokens from session
42
- *
43
- * @deprecated Use getOrganizationToken instead
44
- */
45
- const getOrganizationTokens = async (config) => {
46
- const { isAuthenticated } = await getLogtoContext(config);
47
- if (!isAuthenticated) {
48
- return [];
49
- }
50
- const client$1 = new client.default(config);
51
- const nodeClient = await client$1.createNodeClient();
52
- const { organizations = [] } = await nodeClient.getIdTokenClaims();
53
- return Promise.all(organizations.map(async (organizationId) => ({
54
- id: organizationId,
55
- token: await nodeClient.getOrganizationToken(organizationId),
56
- })));
57
- };
58
- /**
59
- * Get access token for the specified resource or organization,
60
- * this function can be used in server actions or API routes
61
- */
62
- const getAccessToken = async (config, resource, organizationId) => {
63
- const client$1 = new client.default(config);
64
- const nodeClient = await client$1.createNodeClient();
65
- const accessToken = await nodeClient.getAccessToken(resource, organizationId);
66
- return accessToken;
67
- };
68
- /**
69
- * Get organization token from session,
70
- * this function can be used in server actions or API routes
71
- */
72
- const getOrganizationToken = async (config, organizationId) => {
73
- return getAccessToken(config, undefined, organizationId);
74
- };
75
- /**
76
- * Get access token for the specified resource or organization,
77
- * this function can be used in React Server Components (RSC)
78
- * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
79
- * When using server actions or API routes, we highly recommand to use the getAccessToken method
80
- */
81
- const getAccessTokenRSC = async (config, resource, organizationId) => {
82
- const client$1 = new client.default(config);
83
- const nodeClient = await client$1.createNodeClient({ ignoreCookieChange: true });
84
- return nodeClient.getAccessToken(resource, organizationId);
85
- };
86
- /**
87
- * Get organization token from session,
88
- * this function can be used in React Server Components (RSC)
89
- * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
90
- * When using server actions or API routes, we highly recommand to use the getOrganizationToken method
91
- */
92
- const getOrganizationTokenRSC = async (config, organizationId) => {
93
- return getAccessTokenRSC(config, undefined, organizationId);
94
- };
95
-
96
- exports.default = client.default;
97
- exports.getAccessToken = getAccessToken;
98
- exports.getAccessTokenRSC = getAccessTokenRSC;
99
- exports.getLogtoContext = getLogtoContext;
100
- exports.getOrganizationToken = getOrganizationToken;
101
- exports.getOrganizationTokenRSC = getOrganizationTokenRSC;
102
- exports.getOrganizationTokens = getOrganizationTokens;
103
- exports.handleSignIn = handleSignIn;
104
- exports.signIn = signIn;
105
- exports.signOut = signOut;
@@ -1,12 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- class LogtoNextBaseClient {
6
- constructor(config, adapters) {
7
- this.config = config;
8
- this.adapters = adapters;
9
- }
10
- }
11
-
12
- exports.default = LogtoNextBaseClient;
package/lib/src/index.cjs DELETED
@@ -1,165 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var NodeClient = require('@logto/node');
6
- var cookie = require('cookie');
7
- var client = require('./client.cjs');
8
- var utils = require('./utils.cjs');
9
-
10
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
-
12
- var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
13
-
14
- class LogtoClient extends client.default {
15
- constructor(config) {
16
- super(config, {
17
- NodeClient: NodeClient__default.default,
18
- });
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
- }
25
- return this.handleSignInImplementation(options ?? {
26
- redirectUri: `${this.config.baseUrl}/api/logto/sign-in-callback`,
27
- interactionMode,
28
- });
29
- };
30
- this.handleSignInCallback = (redirectTo = this.config.baseUrl, onError) => utils.buildHandler(async (request, response) => {
31
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
32
- if (request.url) {
33
- await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
34
- response.redirect(redirectTo);
35
- }
36
- }, onError);
37
- this.handleSignOut = (redirectUri = this.config.baseUrl, onError) => utils.buildHandler(async (request, response) => {
38
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
39
- await nodeClient.signOut(redirectUri);
40
- await this.storage?.destroy();
41
- if (this.navigateUrl) {
42
- response.redirect(this.navigateUrl);
43
- }
44
- }, onError);
45
- this.handleUser = (configs, onError) => this.withLogtoApiRoute((request, response) => {
46
- response.json(request.user);
47
- }, configs, onError);
48
- this.handleAuthRoutes = (configs, onError) => (request, response) => {
49
- const { action } = request.query;
50
- if (action === 'sign-in') {
51
- return this.handleSignIn(undefined, undefined, onError)(request, response);
52
- }
53
- if (action === 'sign-up') {
54
- return this.handleSignIn(undefined, 'signUp', onError)(request, response);
55
- }
56
- if (action === 'sign-in-callback') {
57
- return this.handleSignInCallback(undefined, onError)(request, response);
58
- }
59
- if (action === 'sign-out') {
60
- return this.handleSignOut(undefined, onError)(request, response);
61
- }
62
- if (action === 'user') {
63
- return this.handleUser(configs)(request, response);
64
- }
65
- response.status(404).end();
66
- };
67
- this.getAccessToken = async (request, response, resource) => {
68
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
69
- return nodeClient.getAccessToken(resource);
70
- };
71
- this.getOrganizationToken = async (request, response, organizationId) => {
72
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
73
- return nodeClient.getOrganizationToken(organizationId);
74
- };
75
- this.withLogtoApiRoute = (handler, config = {}, onError) => utils.buildHandler(async (request, response) => {
76
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
77
- const user = await nodeClient.getContext(config);
78
- // eslint-disable-next-line @silverhand/fp/no-mutating-methods
79
- Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
80
- return handler(request, response);
81
- }, onError);
82
- this.withLogtoSsr = (handler, configs = {}, onError) => async (context) => {
83
- return this.withLogtoApiRoute(async () => {
84
- return handler(context);
85
- }, configs, onError);
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);
94
- }
95
- async createNodeClientFromNextApi(request, response) {
96
- this.storage = new NodeClient.CookieStorage({
97
- encryptionKey: this.config.cookieSecret,
98
- cookieKey: `logto:${this.config.appId}`,
99
- isSecure: this.config.cookieSecure,
100
- getCookie: (name) => {
101
- return request.cookies[name] ?? '';
102
- },
103
- setCookie: (name, value, options) => {
104
- response.setHeader('Set-Cookie', cookie.serialize(name, value, options));
105
- },
106
- });
107
- await this.storage.init();
108
- return new this.adapters.NodeClient(this.config, {
109
- storage: this.storage,
110
- navigate: (url) => {
111
- this.navigateUrl = url;
112
- },
113
- });
114
- }
115
- }
116
-
117
- Object.defineProperty(exports, "LogtoClientError", {
118
- enumerable: true,
119
- get: function () { return NodeClient.LogtoClientError; }
120
- });
121
- Object.defineProperty(exports, "LogtoError", {
122
- enumerable: true,
123
- get: function () { return NodeClient.LogtoError; }
124
- });
125
- Object.defineProperty(exports, "LogtoRequestError", {
126
- enumerable: true,
127
- get: function () { return NodeClient.LogtoRequestError; }
128
- });
129
- Object.defineProperty(exports, "OidcError", {
130
- enumerable: true,
131
- get: function () { return NodeClient.OidcError; }
132
- });
133
- Object.defineProperty(exports, "PersistKey", {
134
- enumerable: true,
135
- get: function () { return NodeClient.PersistKey; }
136
- });
137
- Object.defineProperty(exports, "Prompt", {
138
- enumerable: true,
139
- get: function () { return NodeClient.Prompt; }
140
- });
141
- Object.defineProperty(exports, "ReservedResource", {
142
- enumerable: true,
143
- get: function () { return NodeClient.ReservedResource; }
144
- });
145
- Object.defineProperty(exports, "ReservedScope", {
146
- enumerable: true,
147
- get: function () { return NodeClient.ReservedScope; }
148
- });
149
- Object.defineProperty(exports, "UserScope", {
150
- enumerable: true,
151
- get: function () { return NodeClient.UserScope; }
152
- });
153
- Object.defineProperty(exports, "buildOrganizationUrn", {
154
- enumerable: true,
155
- get: function () { return NodeClient.buildOrganizationUrn; }
156
- });
157
- Object.defineProperty(exports, "getOrganizationIdFromUrn", {
158
- enumerable: true,
159
- get: function () { return NodeClient.getOrganizationIdFromUrn; }
160
- });
161
- Object.defineProperty(exports, "organizationUrnPrefix", {
162
- enumerable: true,
163
- get: function () { return NodeClient.organizationUrnPrefix; }
164
- });
165
- exports.default = LogtoClient;
package/lib/src/utils.cjs DELETED
@@ -1,17 +0,0 @@
1
- 'use strict';
2
-
3
- const buildHandler = (handler, onError) => {
4
- return async (request, response) => {
5
- try {
6
- await handler(request, response);
7
- }
8
- catch (error) {
9
- if (onError) {
10
- return onError(request, response, error);
11
- }
12
- throw error;
13
- }
14
- };
15
- };
16
-
17
- exports.buildHandler = buildHandler;