@logto/next 3.0.0 → 3.1.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.
- package/lib/edge/index.cjs +1 -0
- package/lib/edge/index.js +1 -0
- package/lib/server-actions/client.cjs +89 -0
- package/lib/server-actions/client.d.ts +47 -0
- package/lib/server-actions/client.js +81 -0
- package/lib/server-actions/cookie.cjs +17 -0
- package/lib/server-actions/cookie.d.ts +3 -0
- package/lib/server-actions/cookie.js +14 -0
- package/lib/server-actions/index.cjs +60 -80
- package/lib/server-actions/index.d.ts +25 -45
- package/lib/server-actions/index.js +55 -76
- package/lib/src/index.cjs +1 -1
- package/lib/src/index.d.ts +1 -4
- package/lib/src/index.js +1 -1
- package/package.json +4 -7
- /package/lib/server-actions/{index.test.d.ts → client.test.d.ts} +0 -0
package/lib/edge/index.cjs
CHANGED
package/lib/edge/index.js
CHANGED
|
@@ -80,6 +80,7 @@ class LogtoClient extends LogtoNextBaseClient {
|
|
|
80
80
|
responseCookies.set(cookieName, value, {
|
|
81
81
|
maxAge: 14 * 3600 * 24,
|
|
82
82
|
secure: this.config.cookieSecure,
|
|
83
|
+
sameSite: this.config.cookieSecure ? 'lax' : undefined,
|
|
83
84
|
});
|
|
84
85
|
}));
|
|
85
86
|
return { nodeClient, headers };
|
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
/**
|
|
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 = await NodeClient$1.createSession({
|
|
81
|
+
secret: this.config.cookieSecret,
|
|
82
|
+
crypto,
|
|
83
|
+
}, cookie);
|
|
84
|
+
const nodeClient = super.createNodeClient(session);
|
|
85
|
+
return { nodeClient, session };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
exports.default = LogtoClient;
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
createNodeClientFromHeaders(cookie: string): Promise<{
|
|
44
|
+
nodeClient: import("@logto/node").default;
|
|
45
|
+
session: import("@logto/node").Session;
|
|
46
|
+
}>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createSession } from '@logto/node';
|
|
2
|
+
import NodeClient from '@logto/node/edge';
|
|
3
|
+
import LogtoNextBaseClient from '../src/client.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,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const getCookies = async (config) => {
|
|
4
|
+
const { cookies } = await import('next/headers');
|
|
5
|
+
return cookies().get(`logto:${config.appId}`)?.value ?? '';
|
|
6
|
+
};
|
|
7
|
+
const setCookies = async (newCookie, config) => {
|
|
8
|
+
const { cookies } = await import('next/headers');
|
|
9
|
+
cookies().set(`logto:${config.appId}`, newCookie, {
|
|
10
|
+
maxAge: 14 * 3600 * 24,
|
|
11
|
+
secure: config.cookieSecure,
|
|
12
|
+
sameSite: config.cookieSecure ? 'lax' : undefined,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
exports.getCookies = getCookies;
|
|
17
|
+
exports.setCookies = setCookies;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const getCookies = async (config) => {
|
|
2
|
+
const { cookies } = await import('next/headers');
|
|
3
|
+
return cookies().get(`logto:${config.appId}`)?.value ?? '';
|
|
4
|
+
};
|
|
5
|
+
const setCookies = async (newCookie, config) => {
|
|
6
|
+
const { cookies } = await import('next/headers');
|
|
7
|
+
cookies().set(`logto:${config.appId}`, newCookie, {
|
|
8
|
+
maxAge: 14 * 3600 * 24,
|
|
9
|
+
secure: config.cookieSecure,
|
|
10
|
+
sameSite: config.cookieSecure ? 'lax' : undefined,
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { getCookies, setCookies };
|
|
@@ -2,88 +2,68 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
5
|
+
var navigation = require('next/navigation');
|
|
6
|
+
var client = require('./client.cjs');
|
|
7
|
+
var cookie = require('./cookie.cjs');
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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?.();
|
|
9
|
+
/**
|
|
10
|
+
* Init sign in process and redirect to the Logto sign-in page
|
|
11
|
+
*/
|
|
12
|
+
const signIn = async (config, redirectUri, interactionMode) => {
|
|
13
|
+
const client$1 = new client.default(config);
|
|
14
|
+
const { url, newCookie } = await client$1.handleSignIn(await cookie.getCookies(config), redirectUri ?? `${config.baseUrl}/callback`, interactionMode);
|
|
15
|
+
if (newCookie) {
|
|
16
|
+
await cookie.setCookies(newCookie, config);
|
|
66
17
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
18
|
+
navigation.redirect(url);
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Handle sign in callback from search params, save tokens to session
|
|
22
|
+
*/
|
|
23
|
+
const handleSignIn = async (config, searchParams) => {
|
|
24
|
+
const search = searchParams.toString();
|
|
25
|
+
const client$1 = new client.default(config);
|
|
26
|
+
const newCookie = await client$1.handleSignInCallback(await cookie.getCookies(config), `${config.baseUrl}/callback?${search}`);
|
|
27
|
+
if (newCookie) {
|
|
28
|
+
await cookie.setCookies(newCookie, config);
|
|
78
29
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Init sign out process, clear session, and redirect to the Logto sign-out page
|
|
33
|
+
*/
|
|
34
|
+
const signOut = async (config, redirectUri) => {
|
|
35
|
+
const client$1 = new client.default(config);
|
|
36
|
+
const url = await client$1.handleSignOut(await cookie.getCookies(config), redirectUri);
|
|
37
|
+
await cookie.setCookies('', config);
|
|
38
|
+
navigation.redirect(url);
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Get Logto context from session, including auth status and claims
|
|
42
|
+
*/
|
|
43
|
+
const getLogtoContext = async (config, getContextParameters) => {
|
|
44
|
+
const client$1 = new client.default(config);
|
|
45
|
+
return client$1.getLogtoContext(await cookie.getCookies(config), getContextParameters);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Get organization tokens from session
|
|
49
|
+
*/
|
|
50
|
+
const getOrganizationTokens = async (config) => {
|
|
51
|
+
const { isAuthenticated } = await getLogtoContext(config);
|
|
52
|
+
if (!isAuthenticated) {
|
|
53
|
+
return [];
|
|
86
54
|
}
|
|
87
|
-
|
|
55
|
+
const client$1 = new client.default(config);
|
|
56
|
+
const { nodeClient } = await client$1.createNodeClientFromHeaders(await cookie.getCookies(config));
|
|
57
|
+
const { organizations = [] } = await nodeClient.getIdTokenClaims();
|
|
58
|
+
return Promise.all(organizations.map(async (organizationId) => ({
|
|
59
|
+
id: organizationId,
|
|
60
|
+
token: await nodeClient.getOrganizationToken(organizationId),
|
|
61
|
+
})));
|
|
62
|
+
};
|
|
88
63
|
|
|
89
|
-
exports.default =
|
|
64
|
+
exports.default = client.default;
|
|
65
|
+
exports.getLogtoContext = getLogtoContext;
|
|
66
|
+
exports.getOrganizationTokens = getOrganizationTokens;
|
|
67
|
+
exports.handleSignIn = handleSignIn;
|
|
68
|
+
exports.signIn = signIn;
|
|
69
|
+
exports.signOut = signOut;
|
|
@@ -1,47 +1,27 @@
|
|
|
1
|
-
import { type GetContextParameters, type InteractionMode } from '@logto/node';
|
|
2
|
-
import BaseClient from '../src/client';
|
|
1
|
+
import { type LogtoContext, type GetContextParameters, type InteractionMode } from '@logto/node';
|
|
3
2
|
import type { LogtoNextConfig } from '../src/types.js';
|
|
4
3
|
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
createNodeClientFromHeaders(cookie: string): Promise<{
|
|
44
|
-
nodeClient: import("@logto/node").default;
|
|
45
|
-
session: import("@logto/node").Session;
|
|
46
|
-
}>;
|
|
47
|
-
}
|
|
4
|
+
/**
|
|
5
|
+
* Init sign in process and redirect to the Logto sign-in page
|
|
6
|
+
*/
|
|
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>;
|
|
12
|
+
/**
|
|
13
|
+
* Init sign out process, clear session, and redirect to the Logto sign-out page
|
|
14
|
+
*/
|
|
15
|
+
export declare const signOut: (config: LogtoNextConfig, redirectUri?: string) => Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Get Logto context from session, including auth status and claims
|
|
18
|
+
*/
|
|
19
|
+
export declare const getLogtoContext: (config: LogtoNextConfig, getContextParameters?: GetContextParameters) => Promise<LogtoContext>;
|
|
20
|
+
/**
|
|
21
|
+
* Get organization tokens from session
|
|
22
|
+
*/
|
|
23
|
+
export declare const getOrganizationTokens: (config: LogtoNextConfig) => Promise<{
|
|
24
|
+
id: string;
|
|
25
|
+
token: string;
|
|
26
|
+
}[]>;
|
|
27
|
+
export { default } from './client';
|
|
@@ -1,81 +1,60 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { redirect } from 'next/navigation';
|
|
2
|
+
import LogtoClient from './client.js';
|
|
3
|
+
import { getCookies, setCookies } from './cookie.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Init sign in process and redirect to the Logto sign-in page
|
|
7
|
+
*/
|
|
8
|
+
const signIn = async (config, redirectUri, interactionMode) => {
|
|
9
|
+
const client = new LogtoClient(config);
|
|
10
|
+
const { url, newCookie } = await client.handleSignIn(await getCookies(config), redirectUri ?? `${config.baseUrl}/callback`, interactionMode);
|
|
11
|
+
if (newCookie) {
|
|
12
|
+
await setCookies(newCookie, config);
|
|
10
13
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
await
|
|
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
|
-
};
|
|
14
|
+
redirect(url);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Handle sign in callback from search params, save tokens to session
|
|
18
|
+
*/
|
|
19
|
+
const handleSignIn = async (config, searchParams) => {
|
|
20
|
+
const search = searchParams.toString();
|
|
21
|
+
const client = new LogtoClient(config);
|
|
22
|
+
const newCookie = await client.handleSignInCallback(await getCookies(config), `${config.baseUrl}/callback?${search}`);
|
|
23
|
+
if (newCookie) {
|
|
24
|
+
await setCookies(newCookie, config);
|
|
30
25
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Init sign out process, clear session, and redirect to the Logto sign-out page
|
|
29
|
+
*/
|
|
30
|
+
const signOut = async (config, redirectUri) => {
|
|
31
|
+
const client = new LogtoClient(config);
|
|
32
|
+
const url = await client.handleSignOut(await getCookies(config), redirectUri);
|
|
33
|
+
await setCookies('', config);
|
|
34
|
+
redirect(url);
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Get Logto context from session, including auth status and claims
|
|
38
|
+
*/
|
|
39
|
+
const getLogtoContext = async (config, getContextParameters) => {
|
|
40
|
+
const client = new LogtoClient(config);
|
|
41
|
+
return client.getLogtoContext(await getCookies(config), getContextParameters);
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Get organization tokens from session
|
|
45
|
+
*/
|
|
46
|
+
const getOrganizationTokens = async (config) => {
|
|
47
|
+
const { isAuthenticated } = await getLogtoContext(config);
|
|
48
|
+
if (!isAuthenticated) {
|
|
49
|
+
return [];
|
|
46
50
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
}
|
|
51
|
+
const client = new LogtoClient(config);
|
|
52
|
+
const { nodeClient } = await client.createNodeClientFromHeaders(await getCookies(config));
|
|
53
|
+
const { organizations = [] } = await nodeClient.getIdTokenClaims();
|
|
54
|
+
return Promise.all(organizations.map(async (organizationId) => ({
|
|
55
|
+
id: organizationId,
|
|
56
|
+
token: await nodeClient.getOrganizationToken(organizationId),
|
|
57
|
+
})));
|
|
58
|
+
};
|
|
80
59
|
|
|
81
|
-
export { LogtoClient as default };
|
|
60
|
+
export { LogtoClient as default, getLogtoContext, getOrganizationTokens, handleSignIn, signIn, signOut };
|
package/lib/src/index.cjs
CHANGED
|
@@ -86,7 +86,7 @@ class LogtoClient extends client.default {
|
|
|
86
86
|
}, request.cookies[cookieName] ?? '', (value) => {
|
|
87
87
|
const secure = this.config.cookieSecure;
|
|
88
88
|
const maxAge = 14 * 3600 * 24;
|
|
89
|
-
response.setHeader('Set-Cookie', `${cookieName}=${value}; Path=/; Max-Age=${maxAge}; ${secure ? 'Secure; SameSite=
|
|
89
|
+
response.setHeader('Set-Cookie', `${cookieName}=${value}; Path=/; Max-Age=${maxAge}; ${secure ? 'Secure; SameSite=Lax' : ''}`);
|
|
90
90
|
}));
|
|
91
91
|
}
|
|
92
92
|
}
|
package/lib/src/index.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
|
|
3
|
-
/// <reference types="@logto/node/lib/src/types.js" />
|
|
4
|
-
/// <reference types="@logto/node/src/types.js" />
|
|
5
|
-
import { type IncomingMessage, type ServerResponse } from 'http';
|
|
2
|
+
import { type IncomingMessage, type ServerResponse } from 'node:http';
|
|
6
3
|
import NodeClient, { type GetContextParameters, type InteractionMode } from '@logto/node';
|
|
7
4
|
import { type GetServerSidePropsResult, type GetServerSidePropsContext, type NextApiHandler } from 'next';
|
|
8
5
|
import { type NextApiRequestCookies } from 'next/dist/server/api-utils/index.js';
|
package/lib/src/index.js
CHANGED
|
@@ -79,7 +79,7 @@ class LogtoClient extends LogtoNextBaseClient {
|
|
|
79
79
|
}, request.cookies[cookieName] ?? '', (value) => {
|
|
80
80
|
const secure = this.config.cookieSecure;
|
|
81
81
|
const maxAge = 14 * 3600 * 24;
|
|
82
|
-
response.setHeader('Set-Cookie', `${cookieName}=${value}; Path=/; Max-Age=${maxAge}; ${secure ? 'Secure; SameSite=
|
|
82
|
+
response.setHeader('Set-Cookie', `${cookieName}=${value}; Path=/; Max-Age=${maxAge}; ${secure ? 'Secure; SameSite=Lax' : ''}`);
|
|
83
83
|
}));
|
|
84
84
|
}
|
|
85
85
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/next",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/src/index.cjs",
|
|
6
6
|
"module": "./lib/src/index.js",
|
|
@@ -60,8 +60,8 @@
|
|
|
60
60
|
"jest-location-mock": "^2.0.0",
|
|
61
61
|
"jest-matcher-specific-error": "^1.0.0",
|
|
62
62
|
"lint-staged": "^15.0.0",
|
|
63
|
-
"next": "^
|
|
64
|
-
"next-test-api-route-handler": "^
|
|
63
|
+
"next": "^14.0.0",
|
|
64
|
+
"next-test-api-route-handler": "^4.0.0",
|
|
65
65
|
"prettier": "^3.0.0",
|
|
66
66
|
"react": "^18.2.0",
|
|
67
67
|
"react-dom": "^18.2.0",
|
|
@@ -71,10 +71,7 @@
|
|
|
71
71
|
"next": ">=12"
|
|
72
72
|
},
|
|
73
73
|
"eslintConfig": {
|
|
74
|
-
"extends": "@silverhand"
|
|
75
|
-
"rules": {
|
|
76
|
-
"unicorn/prefer-node-protocol": "off"
|
|
77
|
-
}
|
|
74
|
+
"extends": "@silverhand"
|
|
78
75
|
},
|
|
79
76
|
"prettier": "@silverhand/eslint-config/.prettierrc",
|
|
80
77
|
"publishConfig": {
|
|
File without changes
|