@logto/next 2.0.0 → 2.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 +63 -12
- package/lib/edge/index.d.ts +7 -1
- package/lib/edge/index.js +59 -4
- package/lib/src/client.cjs +5 -69
- package/lib/src/client.d.ts +17 -16
- package/lib/src/client.js +5 -65
- package/lib/src/index.cjs +65 -3
- package/lib/src/index.d.ts +9 -0
- package/lib/src/index.js +62 -3
- package/lib/src/storage.cjs +6 -6
- package/lib/src/storage.d.ts +3 -3
- package/lib/src/storage.js +6 -6
- package/lib/src/types.d.ts +2 -3
- package/package.json +6 -6
- package/lib/edge/iron-session-edge/get-property-descriptor-for-request-session.cjs +0 -36
- package/lib/edge/iron-session-edge/get-property-descriptor-for-request-session.d.ts +0 -2
- package/lib/edge/iron-session-edge/get-property-descriptor-for-request-session.js +0 -32
- package/lib/edge/iron-session-edge/index.cjs +0 -30
- package/lib/edge/iron-session-edge/index.d.ts +0 -9
- package/lib/edge/iron-session-edge/index.js +0 -27
package/lib/edge/index.cjs
CHANGED
|
@@ -2,25 +2,76 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var NodeClient = require('@logto/node/edge');
|
|
6
|
+
var edge = require('iron-session/edge');
|
|
5
7
|
var client = require('../src/client.cjs');
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
|
|
8
12
|
|
|
9
13
|
class LogtoClient extends client.default {
|
|
10
14
|
constructor(config) {
|
|
11
15
|
super(config, {
|
|
12
|
-
|
|
13
|
-
withIronSessionSsr: index.withIronSessionSsr,
|
|
16
|
+
NodeClient: NodeClient__default.default,
|
|
14
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
|
+
};
|
|
15
74
|
}
|
|
16
75
|
}
|
|
17
76
|
|
|
18
|
-
Object.defineProperty(exports, 'ReservedScope', {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
get: function () { return NodeClient.ReservedScope; }
|
|
21
|
-
});
|
|
22
|
-
Object.defineProperty(exports, 'UserScope', {
|
|
23
|
-
enumerable: true,
|
|
24
|
-
get: function () { return NodeClient.UserScope; }
|
|
25
|
-
});
|
|
26
77
|
exports.default = LogtoClient;
|
package/lib/edge/index.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { type GetContextParameters, type InteractionMode } from '@logto/node';
|
|
2
|
+
import { type NextRequest } from 'next/server';
|
|
1
3
|
import BaseClient from '../src/client';
|
|
2
4
|
import type { LogtoNextConfig } from '../src/types.js';
|
|
3
|
-
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
5
|
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
5
6
|
export default class LogtoClient extends BaseClient {
|
|
6
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>;
|
|
7
13
|
}
|
package/lib/edge/index.js
CHANGED
|
@@ -1,13 +1,68 @@
|
|
|
1
|
+
import NodeClient from '@logto/node/edge';
|
|
2
|
+
import { getIronSession } from 'iron-session/edge';
|
|
1
3
|
import LogtoNextBaseClient from '../src/client.js';
|
|
2
|
-
import { withIronSessionApiRoute, withIronSessionSsr } from './iron-session-edge/index.js';
|
|
3
|
-
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
4
|
|
|
5
5
|
class LogtoClient extends LogtoNextBaseClient {
|
|
6
6
|
constructor(config) {
|
|
7
7
|
super(config, {
|
|
8
|
-
|
|
9
|
-
withIronSessionSsr,
|
|
8
|
+
NodeClient,
|
|
10
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
|
+
};
|
|
11
66
|
}
|
|
12
67
|
}
|
|
13
68
|
|
package/lib/src/client.cjs
CHANGED
|
@@ -2,80 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var NodeClient = require('@logto/node');
|
|
6
5
|
var storage = require('./storage.cjs');
|
|
7
6
|
|
|
8
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
-
|
|
10
|
-
var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
|
|
11
|
-
|
|
12
7
|
class LogtoNextBaseClient {
|
|
13
8
|
constructor(config, adapters) {
|
|
14
9
|
this.config = config;
|
|
15
10
|
this.adapters = adapters;
|
|
16
|
-
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
17
|
-
const nodeClient = this.createNodeClient(request);
|
|
18
|
-
await nodeClient.signIn(redirectUri, interactionMode);
|
|
19
|
-
await this.storage?.save();
|
|
20
|
-
if (this.navigateUrl) {
|
|
21
|
-
response.redirect(this.navigateUrl);
|
|
22
|
-
}
|
|
23
|
-
}, this.ironSessionConfigs);
|
|
24
|
-
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
25
|
-
const nodeClient = this.createNodeClient(request);
|
|
26
|
-
if (request.url) {
|
|
27
|
-
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
28
|
-
await this.storage?.save();
|
|
29
|
-
response.redirect(redirectTo);
|
|
30
|
-
}
|
|
31
|
-
}, this.ironSessionConfigs);
|
|
32
|
-
this.handleSignOut = (redirectUri = this.config.baseUrl) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
33
|
-
const nodeClient = this.createNodeClient(request);
|
|
34
|
-
await nodeClient.signOut(redirectUri);
|
|
35
|
-
request.session.destroy();
|
|
36
|
-
await this.storage?.save();
|
|
37
|
-
if (this.navigateUrl) {
|
|
38
|
-
response.redirect(this.navigateUrl);
|
|
39
|
-
}
|
|
40
|
-
}, this.ironSessionConfigs);
|
|
41
|
-
this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
|
|
42
|
-
response.json(request.user);
|
|
43
|
-
}, configs);
|
|
44
|
-
this.handleAuthRoutes = (configs) => (request, response) => {
|
|
45
|
-
const { action } = request.query;
|
|
46
|
-
if (action === 'sign-in') {
|
|
47
|
-
return this.handleSignIn()(request, response);
|
|
48
|
-
}
|
|
49
|
-
if (action === 'sign-up') {
|
|
50
|
-
return this.handleSignIn(undefined, 'signUp')(request, response);
|
|
51
|
-
}
|
|
52
|
-
if (action === 'sign-in-callback') {
|
|
53
|
-
return this.handleSignInCallback()(request, response);
|
|
54
|
-
}
|
|
55
|
-
if (action === 'sign-out') {
|
|
56
|
-
return this.handleSignOut()(request, response);
|
|
57
|
-
}
|
|
58
|
-
if (action === 'user') {
|
|
59
|
-
return this.handleUser(configs)(request, response);
|
|
60
|
-
}
|
|
61
|
-
response.status(404).end();
|
|
62
|
-
};
|
|
63
|
-
this.withLogtoApiRoute = (handler, config = {}) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
64
|
-
const user = await this.getLogtoUserFromRequest(request, config);
|
|
65
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
66
|
-
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
67
|
-
return handler(request, response);
|
|
68
|
-
}, this.ironSessionConfigs);
|
|
69
|
-
this.withLogtoSsr = (handler, configs = {}) => this.adapters.withIronSessionSsr(async (context) => {
|
|
70
|
-
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
71
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
72
|
-
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
|
|
73
|
-
return handler(context);
|
|
74
|
-
}, this.ironSessionConfigs);
|
|
75
11
|
}
|
|
76
|
-
createNodeClient(
|
|
77
|
-
this.storage = new storage.default(
|
|
78
|
-
return new
|
|
12
|
+
createNodeClient(session) {
|
|
13
|
+
this.storage = new storage.default(session);
|
|
14
|
+
return new this.adapters.NodeClient(this.config, {
|
|
79
15
|
storage: this.storage,
|
|
80
16
|
navigate: (url) => {
|
|
81
17
|
this.navigateUrl = url;
|
|
@@ -92,8 +28,8 @@ class LogtoNextBaseClient {
|
|
|
92
28
|
},
|
|
93
29
|
};
|
|
94
30
|
}
|
|
95
|
-
async getLogtoUserFromRequest(
|
|
96
|
-
const nodeClient = this.createNodeClient(
|
|
31
|
+
async getLogtoUserFromRequest(session, configs) {
|
|
32
|
+
const nodeClient = this.createNodeClient(session);
|
|
97
33
|
return nodeClient.getContext(configs);
|
|
98
34
|
}
|
|
99
35
|
}
|
package/lib/src/client.d.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import type { GetContextParameters
|
|
2
|
-
import
|
|
1
|
+
import type { GetContextParameters } from '@logto/node';
|
|
2
|
+
import { type IronSession } from 'iron-session';
|
|
3
|
+
import NextStorage from './storage';
|
|
3
4
|
import type { Adapters, LogtoNextConfig } from './types';
|
|
4
5
|
export default class LogtoNextBaseClient {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
protected readonly config: LogtoNextConfig;
|
|
7
|
+
protected readonly adapters: Adapters;
|
|
8
|
+
protected navigateUrl?: string;
|
|
9
|
+
protected storage?: NextStorage;
|
|
9
10
|
constructor(config: LogtoNextConfig, adapters: Adapters);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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>;
|
|
20
21
|
}
|
package/lib/src/client.js
CHANGED
|
@@ -1,73 +1,13 @@
|
|
|
1
|
-
import NodeClient from '@logto/node';
|
|
2
1
|
import NextStorage from './storage.js';
|
|
3
2
|
|
|
4
3
|
class LogtoNextBaseClient {
|
|
5
4
|
constructor(config, adapters) {
|
|
6
5
|
this.config = config;
|
|
7
6
|
this.adapters = adapters;
|
|
8
|
-
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
9
|
-
const nodeClient = this.createNodeClient(request);
|
|
10
|
-
await nodeClient.signIn(redirectUri, interactionMode);
|
|
11
|
-
await this.storage?.save();
|
|
12
|
-
if (this.navigateUrl) {
|
|
13
|
-
response.redirect(this.navigateUrl);
|
|
14
|
-
}
|
|
15
|
-
}, this.ironSessionConfigs);
|
|
16
|
-
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
17
|
-
const nodeClient = this.createNodeClient(request);
|
|
18
|
-
if (request.url) {
|
|
19
|
-
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
20
|
-
await this.storage?.save();
|
|
21
|
-
response.redirect(redirectTo);
|
|
22
|
-
}
|
|
23
|
-
}, this.ironSessionConfigs);
|
|
24
|
-
this.handleSignOut = (redirectUri = this.config.baseUrl) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
25
|
-
const nodeClient = this.createNodeClient(request);
|
|
26
|
-
await nodeClient.signOut(redirectUri);
|
|
27
|
-
request.session.destroy();
|
|
28
|
-
await this.storage?.save();
|
|
29
|
-
if (this.navigateUrl) {
|
|
30
|
-
response.redirect(this.navigateUrl);
|
|
31
|
-
}
|
|
32
|
-
}, this.ironSessionConfigs);
|
|
33
|
-
this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
|
|
34
|
-
response.json(request.user);
|
|
35
|
-
}, configs);
|
|
36
|
-
this.handleAuthRoutes = (configs) => (request, response) => {
|
|
37
|
-
const { action } = request.query;
|
|
38
|
-
if (action === 'sign-in') {
|
|
39
|
-
return this.handleSignIn()(request, response);
|
|
40
|
-
}
|
|
41
|
-
if (action === 'sign-up') {
|
|
42
|
-
return this.handleSignIn(undefined, 'signUp')(request, response);
|
|
43
|
-
}
|
|
44
|
-
if (action === 'sign-in-callback') {
|
|
45
|
-
return this.handleSignInCallback()(request, response);
|
|
46
|
-
}
|
|
47
|
-
if (action === 'sign-out') {
|
|
48
|
-
return this.handleSignOut()(request, response);
|
|
49
|
-
}
|
|
50
|
-
if (action === 'user') {
|
|
51
|
-
return this.handleUser(configs)(request, response);
|
|
52
|
-
}
|
|
53
|
-
response.status(404).end();
|
|
54
|
-
};
|
|
55
|
-
this.withLogtoApiRoute = (handler, config = {}) => this.adapters.withIronSessionApiRoute(async (request, response) => {
|
|
56
|
-
const user = await this.getLogtoUserFromRequest(request, config);
|
|
57
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
58
|
-
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
59
|
-
return handler(request, response);
|
|
60
|
-
}, this.ironSessionConfigs);
|
|
61
|
-
this.withLogtoSsr = (handler, configs = {}) => this.adapters.withIronSessionSsr(async (context) => {
|
|
62
|
-
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
63
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
64
|
-
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
|
|
65
|
-
return handler(context);
|
|
66
|
-
}, this.ironSessionConfigs);
|
|
67
7
|
}
|
|
68
|
-
createNodeClient(
|
|
69
|
-
this.storage = new NextStorage(
|
|
70
|
-
return new NodeClient(this.config, {
|
|
8
|
+
createNodeClient(session) {
|
|
9
|
+
this.storage = new NextStorage(session);
|
|
10
|
+
return new this.adapters.NodeClient(this.config, {
|
|
71
11
|
storage: this.storage,
|
|
72
12
|
navigate: (url) => {
|
|
73
13
|
this.navigateUrl = url;
|
|
@@ -84,8 +24,8 @@ class LogtoNextBaseClient {
|
|
|
84
24
|
},
|
|
85
25
|
};
|
|
86
26
|
}
|
|
87
|
-
async getLogtoUserFromRequest(
|
|
88
|
-
const nodeClient = this.createNodeClient(
|
|
27
|
+
async getLogtoUserFromRequest(session, configs) {
|
|
28
|
+
const nodeClient = this.createNodeClient(session);
|
|
89
29
|
return nodeClient.getContext(configs);
|
|
90
30
|
}
|
|
91
31
|
}
|
package/lib/src/index.cjs
CHANGED
|
@@ -2,16 +2,78 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var NodeClient = require('@logto/node');
|
|
5
6
|
var next = require('iron-session/next');
|
|
6
7
|
var client = require('./client.cjs');
|
|
7
|
-
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
|
|
8
12
|
|
|
9
13
|
class LogtoClient extends client.default {
|
|
10
14
|
constructor(config) {
|
|
11
15
|
super(config, {
|
|
12
|
-
|
|
13
|
-
withIronSessionSsr: next.withIronSessionSsr,
|
|
16
|
+
NodeClient: NodeClient__default.default,
|
|
14
17
|
});
|
|
18
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => next.withIronSessionApiRoute(async (request, response) => {
|
|
19
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
20
|
+
await nodeClient.signIn(redirectUri, interactionMode);
|
|
21
|
+
await this.storage?.save();
|
|
22
|
+
if (this.navigateUrl) {
|
|
23
|
+
response.redirect(this.navigateUrl);
|
|
24
|
+
}
|
|
25
|
+
}, this.ironSessionConfigs);
|
|
26
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => next.withIronSessionApiRoute(async (request, response) => {
|
|
27
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
28
|
+
if (request.url) {
|
|
29
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
30
|
+
await this.storage?.save();
|
|
31
|
+
response.redirect(redirectTo);
|
|
32
|
+
}
|
|
33
|
+
}, this.ironSessionConfigs);
|
|
34
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl) => next.withIronSessionApiRoute(async (request, response) => {
|
|
35
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
36
|
+
await nodeClient.signOut(redirectUri);
|
|
37
|
+
request.session.destroy();
|
|
38
|
+
await this.storage?.save();
|
|
39
|
+
if (this.navigateUrl) {
|
|
40
|
+
response.redirect(this.navigateUrl);
|
|
41
|
+
}
|
|
42
|
+
}, this.ironSessionConfigs);
|
|
43
|
+
this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
|
|
44
|
+
response.json(request.user);
|
|
45
|
+
}, configs);
|
|
46
|
+
this.handleAuthRoutes = (configs) => (request, response) => {
|
|
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();
|
|
64
|
+
};
|
|
65
|
+
this.withLogtoApiRoute = (handler, config = {}) => next.withIronSessionApiRoute(async (request, response) => {
|
|
66
|
+
const user = await this.getLogtoUserFromRequest(request.session, config);
|
|
67
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
68
|
+
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
69
|
+
return handler(request, response);
|
|
70
|
+
}, this.ironSessionConfigs);
|
|
71
|
+
this.withLogtoSsr = (handler, configs = {}) => next.withIronSessionSsr(async (context) => {
|
|
72
|
+
const user = await this.getLogtoUserFromRequest(context.req.session, configs);
|
|
73
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
74
|
+
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
|
|
75
|
+
return handler(context);
|
|
76
|
+
}, this.ironSessionConfigs);
|
|
15
77
|
}
|
|
16
78
|
}
|
|
17
79
|
|
package/lib/src/index.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
import { type GetContextParameters, type InteractionMode } from '@logto/node';
|
|
2
|
+
import { type GetServerSidePropsResult, type GetServerSidePropsContext, type NextApiHandler } from 'next';
|
|
1
3
|
import LogtoNextBaseClient from './client.js';
|
|
2
4
|
import type { LogtoNextConfig } from './types.js';
|
|
3
5
|
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
6
|
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
5
7
|
export default class LogtoClient extends LogtoNextBaseClient {
|
|
6
8
|
constructor(config: LogtoNextConfig);
|
|
9
|
+
handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => NextApiHandler;
|
|
10
|
+
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
11
|
+
handleSignOut: (redirectUri?: string) => NextApiHandler;
|
|
12
|
+
handleUser: (configs?: GetContextParameters) => NextApiHandler;
|
|
13
|
+
handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
|
|
14
|
+
withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters) => NextApiHandler;
|
|
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>>;
|
|
7
16
|
}
|
package/lib/src/index.js
CHANGED
|
@@ -1,13 +1,72 @@
|
|
|
1
|
+
import NodeClient from '@logto/node';
|
|
2
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
1
3
|
import { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';
|
|
2
4
|
import LogtoNextBaseClient from './client.js';
|
|
3
|
-
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
5
|
|
|
5
6
|
class LogtoClient extends LogtoNextBaseClient {
|
|
6
7
|
constructor(config) {
|
|
7
8
|
super(config, {
|
|
8
|
-
|
|
9
|
-
withIronSessionSsr,
|
|
9
|
+
NodeClient,
|
|
10
10
|
});
|
|
11
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => withIronSessionApiRoute(async (request, response) => {
|
|
12
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
13
|
+
await nodeClient.signIn(redirectUri, interactionMode);
|
|
14
|
+
await this.storage?.save();
|
|
15
|
+
if (this.navigateUrl) {
|
|
16
|
+
response.redirect(this.navigateUrl);
|
|
17
|
+
}
|
|
18
|
+
}, this.ironSessionConfigs);
|
|
19
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => withIronSessionApiRoute(async (request, response) => {
|
|
20
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
21
|
+
if (request.url) {
|
|
22
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
23
|
+
await this.storage?.save();
|
|
24
|
+
response.redirect(redirectTo);
|
|
25
|
+
}
|
|
26
|
+
}, this.ironSessionConfigs);
|
|
27
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl) => withIronSessionApiRoute(async (request, response) => {
|
|
28
|
+
const nodeClient = this.createNodeClient(request.session);
|
|
29
|
+
await nodeClient.signOut(redirectUri);
|
|
30
|
+
request.session.destroy();
|
|
31
|
+
await this.storage?.save();
|
|
32
|
+
if (this.navigateUrl) {
|
|
33
|
+
response.redirect(this.navigateUrl);
|
|
34
|
+
}
|
|
35
|
+
}, this.ironSessionConfigs);
|
|
36
|
+
this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
|
|
37
|
+
response.json(request.user);
|
|
38
|
+
}, configs);
|
|
39
|
+
this.handleAuthRoutes = (configs) => (request, response) => {
|
|
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();
|
|
57
|
+
};
|
|
58
|
+
this.withLogtoApiRoute = (handler, config = {}) => withIronSessionApiRoute(async (request, response) => {
|
|
59
|
+
const user = await this.getLogtoUserFromRequest(request.session, config);
|
|
60
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
61
|
+
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
62
|
+
return handler(request, response);
|
|
63
|
+
}, this.ironSessionConfigs);
|
|
64
|
+
this.withLogtoSsr = (handler, configs = {}) => withIronSessionSsr(async (context) => {
|
|
65
|
+
const user = await this.getLogtoUserFromRequest(context.req.session, configs);
|
|
66
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
67
|
+
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
|
|
68
|
+
return handler(context);
|
|
69
|
+
}, this.ironSessionConfigs);
|
|
11
70
|
}
|
|
12
71
|
}
|
|
13
72
|
|
package/lib/src/storage.cjs
CHANGED
|
@@ -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
|
}
|
package/lib/src/storage.d.ts
CHANGED
|
@@ -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>;
|
package/lib/src/storage.js
CHANGED
|
@@ -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
|
}
|
package/lib/src/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { LogtoConfig } from '@logto/node';
|
|
2
|
+
import type NodeClient from '@logto/node';
|
|
2
3
|
import type { IronSession } from 'iron-session';
|
|
3
|
-
import type { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';
|
|
4
4
|
import type { NextApiRequest } from 'next';
|
|
5
5
|
export type NextRequestWithIronSession = NextApiRequest & {
|
|
6
6
|
session: IronSession;
|
|
@@ -19,6 +19,5 @@ export type LogtoNextConfig = LogtoConfig & {
|
|
|
19
19
|
baseUrl: string;
|
|
20
20
|
};
|
|
21
21
|
export type Adapters = {
|
|
22
|
-
|
|
23
|
-
withIronSessionSsr: typeof withIronSessionSsr;
|
|
22
|
+
NodeClient: typeof NodeClient;
|
|
24
23
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/next",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/src/index.cjs",
|
|
6
6
|
"module": "./lib/src/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"directory": "packages/next"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@logto/node": "^2.
|
|
30
|
+
"@logto/node": "^2.1.1",
|
|
31
31
|
"iron-session": "^6.3.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"jest-location-mock": "^1.0.9",
|
|
43
43
|
"jest-matcher-specific-error": "^1.0.0",
|
|
44
44
|
"lint-staged": "^13.0.0",
|
|
45
|
-
"next": "^
|
|
45
|
+
"next": "^13.0.4",
|
|
46
46
|
"next-test-api-route-handler": "^3.1.6",
|
|
47
47
|
"prettier": "^2.8.7",
|
|
48
|
-
"react": "^
|
|
49
|
-
"react-dom": "^
|
|
48
|
+
"react": "^18.2.0",
|
|
49
|
+
"react-dom": "^18.2.0",
|
|
50
50
|
"typescript": "^5.0.0"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"check": "tsc --noEmit",
|
|
69
69
|
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
70
70
|
"lint": "eslint --ext .ts src",
|
|
71
|
-
"test": "
|
|
71
|
+
"test": "jest",
|
|
72
72
|
"test:coverage": "node test.cjs && jest --silent --coverage"
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function getPropertyDescriptorForRequestSession(session) {
|
|
6
|
-
return {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get() {
|
|
9
|
-
return session;
|
|
10
|
-
},
|
|
11
|
-
set(value) {
|
|
12
|
-
const keys = Object.keys(value);
|
|
13
|
-
const currentKeys = Object.keys(session);
|
|
14
|
-
for (const key of currentKeys) {
|
|
15
|
-
if (!keys.includes(key)) {
|
|
16
|
-
/* eslint-disable @typescript-eslint/no-dynamic-delete */
|
|
17
|
-
/* eslint-disable @silverhand/fp/no-delete */
|
|
18
|
-
// @ts-expect-error See comment in IronSessionData interface
|
|
19
|
-
delete session[key];
|
|
20
|
-
/* eslint-enable @silverhand/fp/no-delete */
|
|
21
|
-
/* eslint-enable @typescript-eslint/no-dynamic-delete */
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
for (const key of keys) {
|
|
25
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
26
|
-
/* eslint-disable @silverhand/fp/no-mutation */
|
|
27
|
-
// @ts-expect-error See comment in IronSessionData interface
|
|
28
|
-
session[key] = value[key];
|
|
29
|
-
/* eslint-enable @silverhand/fp/no-mutation */
|
|
30
|
-
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
exports.default = getPropertyDescriptorForRequestSession;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
function getPropertyDescriptorForRequestSession(session) {
|
|
2
|
-
return {
|
|
3
|
-
enumerable: true,
|
|
4
|
-
get() {
|
|
5
|
-
return session;
|
|
6
|
-
},
|
|
7
|
-
set(value) {
|
|
8
|
-
const keys = Object.keys(value);
|
|
9
|
-
const currentKeys = Object.keys(session);
|
|
10
|
-
for (const key of currentKeys) {
|
|
11
|
-
if (!keys.includes(key)) {
|
|
12
|
-
/* eslint-disable @typescript-eslint/no-dynamic-delete */
|
|
13
|
-
/* eslint-disable @silverhand/fp/no-delete */
|
|
14
|
-
// @ts-expect-error See comment in IronSessionData interface
|
|
15
|
-
delete session[key];
|
|
16
|
-
/* eslint-enable @silverhand/fp/no-delete */
|
|
17
|
-
/* eslint-enable @typescript-eslint/no-dynamic-delete */
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
for (const key of keys) {
|
|
21
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
22
|
-
/* eslint-disable @silverhand/fp/no-mutation */
|
|
23
|
-
// @ts-expect-error See comment in IronSessionData interface
|
|
24
|
-
session[key] = value[key];
|
|
25
|
-
/* eslint-enable @silverhand/fp/no-mutation */
|
|
26
|
-
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export { getPropertyDescriptorForRequestSession as default };
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var edge = require('iron-session/edge');
|
|
4
|
-
var getPropertyDescriptorForRequestSession = require('./get-property-descriptor-for-request-session.cjs');
|
|
5
|
-
|
|
6
|
-
function withIronSessionApiRoute(handler, options) {
|
|
7
|
-
return async (request, response) => {
|
|
8
|
-
const sessionOptions = options instanceof Function ? await options(request, response) : options;
|
|
9
|
-
const session = await edge.getIronSession(request, response, sessionOptions);
|
|
10
|
-
// We define req.session as being enumerable (so console.log(req) shows it)
|
|
11
|
-
// and we also want to allow people to do:
|
|
12
|
-
// req.session = { admin: true }; or req.session = {...req.session, admin: true};
|
|
13
|
-
// req.session.save();
|
|
14
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
15
|
-
Object.defineProperty(request, 'session', getPropertyDescriptorForRequestSession.default(session));
|
|
16
|
-
return handler(request, response);
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function withIronSessionSsr(handler, options) {
|
|
20
|
-
return async (context) => {
|
|
21
|
-
const sessionOptions = options instanceof Function ? await options(context.req, context.res) : options;
|
|
22
|
-
const session = await edge.getIronSession(context.req, context.res, sessionOptions);
|
|
23
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
24
|
-
Object.defineProperty(context.req, 'session', getPropertyDescriptorForRequestSession.default(session));
|
|
25
|
-
return handler(context);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
exports.withIronSessionApiRoute = withIronSessionApiRoute;
|
|
30
|
-
exports.withIronSessionSsr = withIronSessionSsr;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import type { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
-
import type { IronSessionOptions } from 'iron-session';
|
|
4
|
-
import type { NextApiHandler, GetServerSidePropsContext, GetServerSidePropsResult, NextApiRequest, NextApiResponse } from 'next';
|
|
5
|
-
type GetIronSessionApiOptions = (request: NextApiRequest, response: NextApiResponse) => Promise<IronSessionOptions> | IronSessionOptions;
|
|
6
|
-
export declare function withIronSessionApiRoute(handler: NextApiHandler, options: IronSessionOptions | GetIronSessionApiOptions): NextApiHandler;
|
|
7
|
-
type GetIronSessionSsrOptions = (request: IncomingMessage, response: ServerResponse) => Promise<IronSessionOptions> | IronSessionOptions;
|
|
8
|
-
export declare function withIronSessionSsr<P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, options: IronSessionOptions | GetIronSessionSsrOptions): (context: GetServerSidePropsContext) => Promise<GetServerSidePropsResult<P>>;
|
|
9
|
-
export {};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { getIronSession } from 'iron-session/edge';
|
|
2
|
-
import getPropertyDescriptorForRequestSession from './get-property-descriptor-for-request-session.js';
|
|
3
|
-
|
|
4
|
-
function withIronSessionApiRoute(handler, options) {
|
|
5
|
-
return async (request, response) => {
|
|
6
|
-
const sessionOptions = options instanceof Function ? await options(request, response) : options;
|
|
7
|
-
const session = await getIronSession(request, response, sessionOptions);
|
|
8
|
-
// We define req.session as being enumerable (so console.log(req) shows it)
|
|
9
|
-
// and we also want to allow people to do:
|
|
10
|
-
// req.session = { admin: true }; or req.session = {...req.session, admin: true};
|
|
11
|
-
// req.session.save();
|
|
12
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
13
|
-
Object.defineProperty(request, 'session', getPropertyDescriptorForRequestSession(session));
|
|
14
|
-
return handler(request, response);
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function withIronSessionSsr(handler, options) {
|
|
18
|
-
return async (context) => {
|
|
19
|
-
const sessionOptions = options instanceof Function ? await options(context.req, context.res) : options;
|
|
20
|
-
const session = await getIronSession(context.req, context.res, sessionOptions);
|
|
21
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
22
|
-
Object.defineProperty(context.req, 'session', getPropertyDescriptorForRequestSession(session));
|
|
23
|
-
return handler(context);
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export { withIronSessionApiRoute, withIronSessionSsr };
|