@logto/next 1.1.2 → 2.1.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.
- package/lib/edge/index.cjs +77 -0
- package/lib/edge/index.d.ts +13 -0
- package/lib/edge/index.js +69 -0
- package/lib/src/client.cjs +37 -0
- package/lib/src/client.d.ts +21 -0
- package/lib/src/client.js +33 -0
- package/lib/{index.js → src/index.cjs} +10 -31
- package/lib/{index.d.ts → src/index.d.ts} +5 -10
- package/lib/{index.mjs → src/index.js} +10 -31
- package/lib/{storage.js → src/storage.cjs} +6 -6
- package/lib/{storage.d.ts → src/storage.d.ts} +3 -3
- package/lib/{storage.mjs → src/storage.js} +6 -6
- package/lib/{types.d.ts → src/types.d.ts} +4 -0
- package/package.json +37 -37
- /package/lib/{index.test.d.ts → src/index.test.d.ts} +0 -0
- /package/lib/{storage.test.d.ts → src/storage.test.d.ts} +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var NodeClient = require('@logto/node/edge');
|
|
6
|
+
var edge = require('iron-session/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
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => async (request) => {
|
|
19
|
+
const response = new Response(null, {
|
|
20
|
+
status: 307,
|
|
21
|
+
});
|
|
22
|
+
const session = await edge.getIronSession(request, response, this.ironSessionConfigs);
|
|
23
|
+
const nodeClient = this.createNodeClient(session);
|
|
24
|
+
await nodeClient.signIn(redirectUri, interactionMode);
|
|
25
|
+
await this.storage?.save();
|
|
26
|
+
if (this.navigateUrl) {
|
|
27
|
+
response.headers.append('Location', this.navigateUrl);
|
|
28
|
+
}
|
|
29
|
+
return response;
|
|
30
|
+
};
|
|
31
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl) => async (request) => {
|
|
32
|
+
const response = new Response(null, {
|
|
33
|
+
status: 307,
|
|
34
|
+
});
|
|
35
|
+
const session = await edge.getIronSession(request, response, this.ironSessionConfigs);
|
|
36
|
+
const nodeClient = this.createNodeClient(session);
|
|
37
|
+
await nodeClient.signOut(redirectUri);
|
|
38
|
+
session.destroy();
|
|
39
|
+
await this.storage?.save();
|
|
40
|
+
if (this.navigateUrl) {
|
|
41
|
+
response.headers.append('Location', this.navigateUrl);
|
|
42
|
+
}
|
|
43
|
+
return response;
|
|
44
|
+
};
|
|
45
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => async (request) => {
|
|
46
|
+
const response = new Response(null, {
|
|
47
|
+
status: 307,
|
|
48
|
+
headers: {
|
|
49
|
+
Location: redirectTo,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
const session = await edge.getIronSession(request, response, this.ironSessionConfigs);
|
|
53
|
+
const nodeClient = this.createNodeClient(session);
|
|
54
|
+
if (request.url) {
|
|
55
|
+
await nodeClient.handleSignInCallback(request.url);
|
|
56
|
+
await this.storage?.save();
|
|
57
|
+
}
|
|
58
|
+
return response;
|
|
59
|
+
};
|
|
60
|
+
this.handleUser = (configs) => async (request) => {
|
|
61
|
+
const context = await this.getLogtoContext(request, configs);
|
|
62
|
+
return new Response(JSON.stringify(context), {
|
|
63
|
+
status: 200,
|
|
64
|
+
headers: {
|
|
65
|
+
'content-type': 'application/json',
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
this.getLogtoContext = async (request, config = {}) => {
|
|
70
|
+
const session = await edge.getIronSession(request, new Response(), this.ironSessionConfigs);
|
|
71
|
+
const context = await this.getLogtoUserFromRequest(session, config);
|
|
72
|
+
return context;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
exports.default = LogtoClient;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type GetContextParameters, type InteractionMode } from '@logto/node';
|
|
2
|
+
import { type NextRequest } from 'next/server';
|
|
3
|
+
import BaseClient from '../src/client';
|
|
4
|
+
import type { LogtoNextConfig } from '../src/types.js';
|
|
5
|
+
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
6
|
+
export default class LogtoClient extends BaseClient {
|
|
7
|
+
constructor(config: LogtoNextConfig);
|
|
8
|
+
handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => (request: NextRequest) => Promise<Response>;
|
|
9
|
+
handleSignOut: (redirectUri?: string) => (request: NextRequest) => Promise<Response>;
|
|
10
|
+
handleSignInCallback: (redirectTo?: string) => (request: NextRequest) => Promise<Response>;
|
|
11
|
+
handleUser: (configs?: GetContextParameters) => (request: NextRequest) => Promise<Response>;
|
|
12
|
+
getLogtoContext: (request: NextRequest, config?: GetContextParameters) => Promise<import("@logto/node").LogtoContext>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import NodeClient from '@logto/node/edge';
|
|
2
|
+
import { getIronSession } from 'iron-session/edge';
|
|
3
|
+
import LogtoNextBaseClient from '../src/client.js';
|
|
4
|
+
|
|
5
|
+
class LogtoClient extends LogtoNextBaseClient {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
super(config, {
|
|
8
|
+
NodeClient,
|
|
9
|
+
});
|
|
10
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => async (request) => {
|
|
11
|
+
const response = new Response(null, {
|
|
12
|
+
status: 307,
|
|
13
|
+
});
|
|
14
|
+
const session = await getIronSession(request, response, this.ironSessionConfigs);
|
|
15
|
+
const nodeClient = this.createNodeClient(session);
|
|
16
|
+
await nodeClient.signIn(redirectUri, interactionMode);
|
|
17
|
+
await this.storage?.save();
|
|
18
|
+
if (this.navigateUrl) {
|
|
19
|
+
response.headers.append('Location', this.navigateUrl);
|
|
20
|
+
}
|
|
21
|
+
return response;
|
|
22
|
+
};
|
|
23
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl) => async (request) => {
|
|
24
|
+
const response = new Response(null, {
|
|
25
|
+
status: 307,
|
|
26
|
+
});
|
|
27
|
+
const session = await getIronSession(request, response, this.ironSessionConfigs);
|
|
28
|
+
const nodeClient = this.createNodeClient(session);
|
|
29
|
+
await nodeClient.signOut(redirectUri);
|
|
30
|
+
session.destroy();
|
|
31
|
+
await this.storage?.save();
|
|
32
|
+
if (this.navigateUrl) {
|
|
33
|
+
response.headers.append('Location', this.navigateUrl);
|
|
34
|
+
}
|
|
35
|
+
return response;
|
|
36
|
+
};
|
|
37
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => async (request) => {
|
|
38
|
+
const response = new Response(null, {
|
|
39
|
+
status: 307,
|
|
40
|
+
headers: {
|
|
41
|
+
Location: redirectTo,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
const session = await getIronSession(request, response, this.ironSessionConfigs);
|
|
45
|
+
const nodeClient = this.createNodeClient(session);
|
|
46
|
+
if (request.url) {
|
|
47
|
+
await nodeClient.handleSignInCallback(request.url);
|
|
48
|
+
await this.storage?.save();
|
|
49
|
+
}
|
|
50
|
+
return response;
|
|
51
|
+
};
|
|
52
|
+
this.handleUser = (configs) => async (request) => {
|
|
53
|
+
const context = await this.getLogtoContext(request, configs);
|
|
54
|
+
return new Response(JSON.stringify(context), {
|
|
55
|
+
status: 200,
|
|
56
|
+
headers: {
|
|
57
|
+
'content-type': 'application/json',
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
this.getLogtoContext = async (request, config = {}) => {
|
|
62
|
+
const session = await getIronSession(request, new Response(), this.ironSessionConfigs);
|
|
63
|
+
const context = await this.getLogtoUserFromRequest(session, config);
|
|
64
|
+
return context;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { LogtoClient as default };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var storage = require('./storage.cjs');
|
|
6
|
+
|
|
7
|
+
class LogtoNextBaseClient {
|
|
8
|
+
constructor(config, adapters) {
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.adapters = adapters;
|
|
11
|
+
}
|
|
12
|
+
createNodeClient(session) {
|
|
13
|
+
this.storage = new storage.default(session);
|
|
14
|
+
return new this.adapters.NodeClient(this.config, {
|
|
15
|
+
storage: this.storage,
|
|
16
|
+
navigate: (url) => {
|
|
17
|
+
this.navigateUrl = url;
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
get ironSessionConfigs() {
|
|
22
|
+
return {
|
|
23
|
+
cookieName: `logto:${this.config.appId}`,
|
|
24
|
+
password: this.config.cookieSecret,
|
|
25
|
+
cookieOptions: {
|
|
26
|
+
secure: this.config.cookieSecure,
|
|
27
|
+
maxAge: 14 * 24 * 60 * 60,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async getLogtoUserFromRequest(session, configs) {
|
|
32
|
+
const nodeClient = this.createNodeClient(session);
|
|
33
|
+
return nodeClient.getContext(configs);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.default = LogtoNextBaseClient;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { GetContextParameters } from '@logto/node';
|
|
2
|
+
import { type IronSession } from 'iron-session';
|
|
3
|
+
import NextStorage from './storage';
|
|
4
|
+
import type { Adapters, LogtoNextConfig } from './types';
|
|
5
|
+
export default class LogtoNextBaseClient {
|
|
6
|
+
protected readonly config: LogtoNextConfig;
|
|
7
|
+
protected readonly adapters: Adapters;
|
|
8
|
+
protected navigateUrl?: string;
|
|
9
|
+
protected storage?: NextStorage;
|
|
10
|
+
constructor(config: LogtoNextConfig, adapters: Adapters);
|
|
11
|
+
protected createNodeClient(session: IronSession): import("@logto/node").default;
|
|
12
|
+
protected get ironSessionConfigs(): {
|
|
13
|
+
cookieName: string;
|
|
14
|
+
password: string;
|
|
15
|
+
cookieOptions: {
|
|
16
|
+
secure: boolean;
|
|
17
|
+
maxAge: number;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
protected getLogtoUserFromRequest(session: IronSession, configs: GetContextParameters): Promise<import("@logto/node").LogtoContext>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import NextStorage from './storage.js';
|
|
2
|
+
|
|
3
|
+
class LogtoNextBaseClient {
|
|
4
|
+
constructor(config, adapters) {
|
|
5
|
+
this.config = config;
|
|
6
|
+
this.adapters = adapters;
|
|
7
|
+
}
|
|
8
|
+
createNodeClient(session) {
|
|
9
|
+
this.storage = new NextStorage(session);
|
|
10
|
+
return new this.adapters.NodeClient(this.config, {
|
|
11
|
+
storage: this.storage,
|
|
12
|
+
navigate: (url) => {
|
|
13
|
+
this.navigateUrl = url;
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
get ironSessionConfigs() {
|
|
18
|
+
return {
|
|
19
|
+
cookieName: `logto:${this.config.appId}`,
|
|
20
|
+
password: this.config.cookieSecret,
|
|
21
|
+
cookieOptions: {
|
|
22
|
+
secure: this.config.cookieSecure,
|
|
23
|
+
maxAge: 14 * 24 * 60 * 60,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async getLogtoUserFromRequest(session, configs) {
|
|
28
|
+
const nodeClient = this.createNodeClient(session);
|
|
29
|
+
return nodeClient.getContext(configs);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { LogtoNextBaseClient as default };
|
|
@@ -4,17 +4,19 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var NodeClient = require('@logto/node');
|
|
6
6
|
var next = require('iron-session/next');
|
|
7
|
-
var
|
|
7
|
+
var client = require('./client.cjs');
|
|
8
8
|
|
|
9
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
|
|
11
11
|
var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
|
|
12
12
|
|
|
13
|
-
class LogtoClient {
|
|
13
|
+
class LogtoClient extends client.default {
|
|
14
14
|
constructor(config) {
|
|
15
|
-
|
|
15
|
+
super(config, {
|
|
16
|
+
NodeClient: NodeClient__default.default,
|
|
17
|
+
});
|
|
16
18
|
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => next.withIronSessionApiRoute(async (request, response) => {
|
|
17
|
-
const nodeClient = this.createNodeClient(request);
|
|
19
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
18
20
|
await nodeClient.signIn(redirectUri, interactionMode);
|
|
19
21
|
await this.storage?.save();
|
|
20
22
|
if (this.navigateUrl) {
|
|
@@ -22,7 +24,7 @@ class LogtoClient {
|
|
|
22
24
|
}
|
|
23
25
|
}, this.ironSessionConfigs);
|
|
24
26
|
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => next.withIronSessionApiRoute(async (request, response) => {
|
|
25
|
-
const nodeClient = this.createNodeClient(request);
|
|
27
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
26
28
|
if (request.url) {
|
|
27
29
|
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
28
30
|
await this.storage?.save();
|
|
@@ -30,7 +32,7 @@ class LogtoClient {
|
|
|
30
32
|
}
|
|
31
33
|
}, this.ironSessionConfigs);
|
|
32
34
|
this.handleSignOut = (redirectUri = this.config.baseUrl) => next.withIronSessionApiRoute(async (request, response) => {
|
|
33
|
-
const nodeClient = this.createNodeClient(request);
|
|
35
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
34
36
|
await nodeClient.signOut(redirectUri);
|
|
35
37
|
request.session.destroy();
|
|
36
38
|
await this.storage?.save();
|
|
@@ -61,41 +63,18 @@ class LogtoClient {
|
|
|
61
63
|
response.status(404).end();
|
|
62
64
|
};
|
|
63
65
|
this.withLogtoApiRoute = (handler, config = {}) => next.withIronSessionApiRoute(async (request, response) => {
|
|
64
|
-
const user = await this.getLogtoUserFromRequest(request, config);
|
|
66
|
+
const user = await this.getLogtoUserFromRequest(request.session, config);
|
|
65
67
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
66
68
|
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
67
69
|
return handler(request, response);
|
|
68
70
|
}, this.ironSessionConfigs);
|
|
69
71
|
this.withLogtoSsr = (handler, configs = {}) => next.withIronSessionSsr(async (context) => {
|
|
70
|
-
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
72
|
+
const user = await this.getLogtoUserFromRequest(context.req.session, configs);
|
|
71
73
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
72
74
|
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
|
|
73
75
|
return handler(context);
|
|
74
76
|
}, this.ironSessionConfigs);
|
|
75
77
|
}
|
|
76
|
-
createNodeClient(request) {
|
|
77
|
-
this.storage = new storage.default(request);
|
|
78
|
-
return new NodeClient__default.default(this.config, {
|
|
79
|
-
storage: this.storage,
|
|
80
|
-
navigate: (url) => {
|
|
81
|
-
this.navigateUrl = url;
|
|
82
|
-
},
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
get ironSessionConfigs() {
|
|
86
|
-
return {
|
|
87
|
-
cookieName: `logto:${this.config.appId}`,
|
|
88
|
-
password: this.config.cookieSecret,
|
|
89
|
-
cookieOptions: {
|
|
90
|
-
secure: this.config.cookieSecure,
|
|
91
|
-
maxAge: 14 * 24 * 60 * 60,
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
async getLogtoUserFromRequest(request, configs) {
|
|
96
|
-
const nodeClient = this.createNodeClient(request);
|
|
97
|
-
return nodeClient.getContext(configs);
|
|
98
|
-
}
|
|
99
78
|
}
|
|
100
79
|
|
|
101
80
|
Object.defineProperty(exports, 'ReservedScope', {
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
import
|
|
1
|
+
import { type GetContextParameters, type InteractionMode } from '@logto/node';
|
|
2
|
+
import { type GetServerSidePropsResult, type GetServerSidePropsContext, type NextApiHandler } from 'next';
|
|
3
|
+
import LogtoNextBaseClient from './client.js';
|
|
4
|
+
import type { LogtoNextConfig } from './types.js';
|
|
4
5
|
export { ReservedScope, UserScope } from '@logto/node';
|
|
5
6
|
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
6
|
-
export default class LogtoClient {
|
|
7
|
-
private readonly config;
|
|
8
|
-
private navigateUrl?;
|
|
9
|
-
private storage?;
|
|
7
|
+
export default class LogtoClient extends LogtoNextBaseClient {
|
|
10
8
|
constructor(config: LogtoNextConfig);
|
|
11
9
|
handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => NextApiHandler;
|
|
12
10
|
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
@@ -15,7 +13,4 @@ export default class LogtoClient {
|
|
|
15
13
|
handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
|
|
16
14
|
withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters) => NextApiHandler;
|
|
17
15
|
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters) => (context: GetServerSidePropsContext) => Promise<GetServerSidePropsResult<P>>;
|
|
18
|
-
private createNodeClient;
|
|
19
|
-
private get ironSessionConfigs();
|
|
20
|
-
private getLogtoUserFromRequest;
|
|
21
16
|
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import NodeClient from '@logto/node';
|
|
2
2
|
export { ReservedScope, UserScope } from '@logto/node';
|
|
3
3
|
import { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';
|
|
4
|
-
import
|
|
4
|
+
import LogtoNextBaseClient from './client.js';
|
|
5
5
|
|
|
6
|
-
class LogtoClient {
|
|
6
|
+
class LogtoClient extends LogtoNextBaseClient {
|
|
7
7
|
constructor(config) {
|
|
8
|
-
|
|
8
|
+
super(config, {
|
|
9
|
+
NodeClient,
|
|
10
|
+
});
|
|
9
11
|
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => withIronSessionApiRoute(async (request, response) => {
|
|
10
|
-
const nodeClient = this.createNodeClient(request);
|
|
12
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
11
13
|
await nodeClient.signIn(redirectUri, interactionMode);
|
|
12
14
|
await this.storage?.save();
|
|
13
15
|
if (this.navigateUrl) {
|
|
@@ -15,7 +17,7 @@ class LogtoClient {
|
|
|
15
17
|
}
|
|
16
18
|
}, this.ironSessionConfigs);
|
|
17
19
|
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => withIronSessionApiRoute(async (request, response) => {
|
|
18
|
-
const nodeClient = this.createNodeClient(request);
|
|
20
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
19
21
|
if (request.url) {
|
|
20
22
|
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
21
23
|
await this.storage?.save();
|
|
@@ -23,7 +25,7 @@ class LogtoClient {
|
|
|
23
25
|
}
|
|
24
26
|
}, this.ironSessionConfigs);
|
|
25
27
|
this.handleSignOut = (redirectUri = this.config.baseUrl) => withIronSessionApiRoute(async (request, response) => {
|
|
26
|
-
const nodeClient = this.createNodeClient(request);
|
|
28
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
27
29
|
await nodeClient.signOut(redirectUri);
|
|
28
30
|
request.session.destroy();
|
|
29
31
|
await this.storage?.save();
|
|
@@ -54,41 +56,18 @@ class LogtoClient {
|
|
|
54
56
|
response.status(404).end();
|
|
55
57
|
};
|
|
56
58
|
this.withLogtoApiRoute = (handler, config = {}) => withIronSessionApiRoute(async (request, response) => {
|
|
57
|
-
const user = await this.getLogtoUserFromRequest(request, config);
|
|
59
|
+
const user = await this.getLogtoUserFromRequest(request.session, config);
|
|
58
60
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
59
61
|
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
60
62
|
return handler(request, response);
|
|
61
63
|
}, this.ironSessionConfigs);
|
|
62
64
|
this.withLogtoSsr = (handler, configs = {}) => withIronSessionSsr(async (context) => {
|
|
63
|
-
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
65
|
+
const user = await this.getLogtoUserFromRequest(context.req.session, configs);
|
|
64
66
|
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
65
67
|
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
|
|
66
68
|
return handler(context);
|
|
67
69
|
}, this.ironSessionConfigs);
|
|
68
70
|
}
|
|
69
|
-
createNodeClient(request) {
|
|
70
|
-
this.storage = new NextStorage(request);
|
|
71
|
-
return new NodeClient(this.config, {
|
|
72
|
-
storage: this.storage,
|
|
73
|
-
navigate: (url) => {
|
|
74
|
-
this.navigateUrl = url;
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
get ironSessionConfigs() {
|
|
79
|
-
return {
|
|
80
|
-
cookieName: `logto:${this.config.appId}`,
|
|
81
|
-
password: this.config.cookieSecret,
|
|
82
|
-
cookieOptions: {
|
|
83
|
-
secure: this.config.cookieSecure,
|
|
84
|
-
maxAge: 14 * 24 * 60 * 60,
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
async getLogtoUserFromRequest(request, configs) {
|
|
89
|
-
const nodeClient = this.createNodeClient(request);
|
|
90
|
-
return nodeClient.getContext(configs);
|
|
91
|
-
}
|
|
92
71
|
}
|
|
93
72
|
|
|
94
73
|
export { LogtoClient as default };
|
|
@@ -3,30 +3,30 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
class NextStorage {
|
|
6
|
-
constructor(
|
|
7
|
-
this.
|
|
6
|
+
constructor(session) {
|
|
7
|
+
this.session = session;
|
|
8
8
|
this.sessionChanged = false;
|
|
9
9
|
}
|
|
10
10
|
async setItem(key, value) {
|
|
11
|
-
this.
|
|
11
|
+
this.session[key] = value;
|
|
12
12
|
this.sessionChanged = true;
|
|
13
13
|
}
|
|
14
14
|
async getItem(key) {
|
|
15
|
-
const value = this.
|
|
15
|
+
const value = this.session[key];
|
|
16
16
|
if (value === undefined) {
|
|
17
17
|
return null;
|
|
18
18
|
}
|
|
19
19
|
return String(value);
|
|
20
20
|
}
|
|
21
21
|
async removeItem(key) {
|
|
22
|
-
this.
|
|
22
|
+
this.session[key] = undefined;
|
|
23
23
|
this.sessionChanged = true;
|
|
24
24
|
}
|
|
25
25
|
async save() {
|
|
26
26
|
if (!this.sessionChanged) {
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
|
-
await this.
|
|
29
|
+
await this.session.save();
|
|
30
30
|
this.sessionChanged = false;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { IncomingMessage } from 'http';
|
|
2
1
|
import type { Storage, StorageKey } from '@logto/node';
|
|
2
|
+
import { type IronSession } from 'iron-session';
|
|
3
3
|
export default class NextStorage implements Storage {
|
|
4
|
-
private readonly
|
|
4
|
+
private readonly session;
|
|
5
5
|
private sessionChanged;
|
|
6
|
-
constructor(
|
|
6
|
+
constructor(session: IronSession);
|
|
7
7
|
setItem(key: StorageKey, value: string): Promise<void>;
|
|
8
8
|
getItem(key: StorageKey): Promise<string | null>;
|
|
9
9
|
removeItem(key: StorageKey): Promise<void>;
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
class NextStorage {
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
2
|
+
constructor(session) {
|
|
3
|
+
this.session = session;
|
|
4
4
|
this.sessionChanged = false;
|
|
5
5
|
}
|
|
6
6
|
async setItem(key, value) {
|
|
7
|
-
this.
|
|
7
|
+
this.session[key] = value;
|
|
8
8
|
this.sessionChanged = true;
|
|
9
9
|
}
|
|
10
10
|
async getItem(key) {
|
|
11
|
-
const value = this.
|
|
11
|
+
const value = this.session[key];
|
|
12
12
|
if (value === undefined) {
|
|
13
13
|
return null;
|
|
14
14
|
}
|
|
15
15
|
return String(value);
|
|
16
16
|
}
|
|
17
17
|
async removeItem(key) {
|
|
18
|
-
this.
|
|
18
|
+
this.session[key] = undefined;
|
|
19
19
|
this.sessionChanged = true;
|
|
20
20
|
}
|
|
21
21
|
async save() {
|
|
22
22
|
if (!this.sessionChanged) {
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
|
-
await this.
|
|
25
|
+
await this.session.save();
|
|
26
26
|
this.sessionChanged = false;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { LogtoConfig } from '@logto/node';
|
|
2
|
+
import type NodeClient from '@logto/node';
|
|
2
3
|
import type { IronSession } from 'iron-session';
|
|
3
4
|
import type { NextApiRequest } from 'next';
|
|
4
5
|
export type NextRequestWithIronSession = NextApiRequest & {
|
|
@@ -17,3 +18,6 @@ export type LogtoNextConfig = LogtoConfig & {
|
|
|
17
18
|
cookieSecure: boolean;
|
|
18
19
|
baseUrl: string;
|
|
19
20
|
};
|
|
21
|
+
export type Adapters = {
|
|
22
|
+
NodeClient: typeof NodeClient;
|
|
23
|
+
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/next",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"main": "./lib/index.
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./lib/src/index.cjs",
|
|
6
|
+
"module": "./lib/src/index.js",
|
|
7
|
+
"types": "./lib/src/index.d.ts",
|
|
6
8
|
"exports": {
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./lib/src/index.cjs",
|
|
11
|
+
"import": "./lib/src/index.js",
|
|
12
|
+
"types": "./lib/src/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./edge": {
|
|
15
|
+
"require": "./lib/edge/index.cjs",
|
|
16
|
+
"import": "./lib/edge/index.js",
|
|
17
|
+
"types": "./lib/edge/index.d.ts"
|
|
18
|
+
}
|
|
9
19
|
},
|
|
10
|
-
"module": "./lib/index.mjs",
|
|
11
|
-
"types": "./lib/index.d.ts",
|
|
12
20
|
"files": [
|
|
13
21
|
"lib"
|
|
14
22
|
],
|
|
@@ -18,25 +26,14 @@
|
|
|
18
26
|
"url": "https://github.com/logto-io/js.git",
|
|
19
27
|
"directory": "packages/next"
|
|
20
28
|
},
|
|
21
|
-
"scripts": {
|
|
22
|
-
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
23
|
-
"precommit": "lint-staged",
|
|
24
|
-
"check": "tsc --noEmit",
|
|
25
|
-
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
26
|
-
"lint": "eslint --ext .ts src",
|
|
27
|
-
"test": "jest",
|
|
28
|
-
"test:coverage": "jest --silent --coverage",
|
|
29
|
-
"prepack": "pnpm test"
|
|
30
|
-
},
|
|
31
29
|
"dependencies": {
|
|
32
|
-
"@logto/node": "^
|
|
33
|
-
"iron-session": "^6.1
|
|
30
|
+
"@logto/node": "^2.1.0",
|
|
31
|
+
"iron-session": "^6.3.1"
|
|
34
32
|
},
|
|
35
33
|
"devDependencies": {
|
|
36
|
-
"@
|
|
37
|
-
"@silverhand/
|
|
38
|
-
"@silverhand/ts-config": "^
|
|
39
|
-
"@silverhand/ts-config-react": "^2.0.0",
|
|
34
|
+
"@silverhand/eslint-config": "^3.0.1",
|
|
35
|
+
"@silverhand/ts-config": "^3.0.0",
|
|
36
|
+
"@silverhand/ts-config-react": "^3.0.0",
|
|
40
37
|
"@swc/core": "^1.3.50",
|
|
41
38
|
"@swc/jest": "^0.2.24",
|
|
42
39
|
"@types/jest": "^29.5.0",
|
|
@@ -45,30 +42,33 @@
|
|
|
45
42
|
"jest-location-mock": "^1.0.9",
|
|
46
43
|
"jest-matcher-specific-error": "^1.0.0",
|
|
47
44
|
"lint-staged": "^13.0.0",
|
|
48
|
-
"next": "^
|
|
45
|
+
"next": "^13.0.4",
|
|
49
46
|
"next-test-api-route-handler": "^3.1.6",
|
|
50
47
|
"prettier": "^2.8.7",
|
|
51
|
-
"react": "^
|
|
52
|
-
"react-dom": "^
|
|
48
|
+
"react": "^18.2.0",
|
|
49
|
+
"react-dom": "^18.2.0",
|
|
53
50
|
"typescript": "^5.0.0"
|
|
54
51
|
},
|
|
55
52
|
"peerDependencies": {
|
|
56
53
|
"next": ">=12"
|
|
57
54
|
},
|
|
58
55
|
"eslintConfig": {
|
|
59
|
-
"extends": "@silverhand"
|
|
56
|
+
"extends": "@silverhand",
|
|
57
|
+
"rules": {
|
|
58
|
+
"unicorn/prefer-node-protocol": "off"
|
|
59
|
+
}
|
|
60
60
|
},
|
|
61
61
|
"prettier": "@silverhand/eslint-config/.prettierrc",
|
|
62
62
|
"publishConfig": {
|
|
63
63
|
"access": "public"
|
|
64
64
|
},
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
65
|
+
"scripts": {
|
|
66
|
+
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
67
|
+
"precommit": "lint-staged",
|
|
68
|
+
"check": "tsc --noEmit",
|
|
69
|
+
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
70
|
+
"lint": "eslint --ext .ts src",
|
|
71
|
+
"test": "jest",
|
|
72
|
+
"test:coverage": "node test.cjs && jest --silent --coverage"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
File without changes
|
|
File without changes
|