@logto/next 1.1.1 → 1.1.2
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/index.d.ts +10 -19
- package/lib/index.js +82 -102
- package/lib/index.mjs +94 -0
- package/lib/index.test.d.ts +1 -0
- package/lib/storage.d.ts +11 -0
- package/lib/storage.js +34 -0
- package/lib/storage.mjs +30 -0
- package/lib/storage.test.d.ts +1 -0
- package/lib/types.d.ts +19 -0
- package/package.json +14 -17
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/module.d.mts +0 -30
- package/lib/module.mjs +0 -115
- package/lib/module.mjs.map +0 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,30 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from
|
|
3
|
-
|
|
4
|
-
interface IronSessionData {
|
|
5
|
-
accessToken?: string;
|
|
6
|
-
idToken?: string;
|
|
7
|
-
signInSession?: string;
|
|
8
|
-
refreshToken?: string;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
type LogtoNextConfig = LogtoConfig & {
|
|
12
|
-
cookieSecret: string;
|
|
13
|
-
cookieSecure: boolean;
|
|
14
|
-
baseUrl: string;
|
|
15
|
-
};
|
|
1
|
+
import type { GetContextParameters, InteractionMode } from '@logto/node';
|
|
2
|
+
import type { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';
|
|
3
|
+
import type { LogtoNextConfig } from './types';
|
|
16
4
|
export { ReservedScope, UserScope } from '@logto/node';
|
|
17
5
|
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
18
6
|
export default class LogtoClient {
|
|
7
|
+
private readonly config;
|
|
8
|
+
private navigateUrl?;
|
|
9
|
+
private storage?;
|
|
19
10
|
constructor(config: LogtoNextConfig);
|
|
20
11
|
handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => NextApiHandler;
|
|
21
12
|
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
22
13
|
handleSignOut: (redirectUri?: string) => NextApiHandler;
|
|
23
|
-
handleUser: (configs?: GetContextParameters) => NextApiHandler
|
|
14
|
+
handleUser: (configs?: GetContextParameters) => NextApiHandler;
|
|
24
15
|
handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
|
|
25
16
|
withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters) => NextApiHandler;
|
|
26
|
-
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters) => (context: GetServerSidePropsContext
|
|
17
|
+
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;
|
|
27
19
|
private get ironSessionConfigs();
|
|
20
|
+
private getLogtoUserFromRequest;
|
|
28
21
|
}
|
|
29
|
-
|
|
30
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -1,112 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
var $gpzKJ$ironsessionnext = require("iron-session/next");
|
|
1
|
+
'use strict';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
return a && a.__esModule ? a.default : a;
|
|
6
|
-
}
|
|
7
|
-
function $parcel$defineInteropFlag(a) {
|
|
8
|
-
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
9
|
-
}
|
|
10
|
-
function $parcel$export(e, n, v, s) {
|
|
11
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
12
|
-
}
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
13
4
|
|
|
14
|
-
|
|
5
|
+
var NodeClient = require('@logto/node');
|
|
6
|
+
var next = require('iron-session/next');
|
|
7
|
+
var storage = require('./storage.js');
|
|
15
8
|
|
|
16
|
-
|
|
17
|
-
$parcel$export(module.exports, "ReservedScope", () => $dee2a34a598960d9$re_export$ReservedScope);
|
|
18
|
-
$parcel$export(module.exports, "UserScope", () => $dee2a34a598960d9$re_export$UserScope);
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
10
|
|
|
11
|
+
var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
|
|
20
12
|
|
|
21
|
-
class
|
|
22
|
-
constructor(
|
|
23
|
-
this.request = request;
|
|
24
|
-
this.sessionChanged = false;
|
|
25
|
-
}
|
|
26
|
-
async setItem(key, value) {
|
|
27
|
-
this.request.session[key] = value;
|
|
28
|
-
this.sessionChanged = true;
|
|
29
|
-
}
|
|
30
|
-
async getItem(key) {
|
|
31
|
-
const value = this.request.session[key];
|
|
32
|
-
if (value === undefined) return null;
|
|
33
|
-
return String(value);
|
|
34
|
-
}
|
|
35
|
-
async removeItem(key) {
|
|
36
|
-
this.request.session[key] = undefined;
|
|
37
|
-
this.sessionChanged = true;
|
|
38
|
-
}
|
|
39
|
-
async save() {
|
|
40
|
-
if (!this.sessionChanged) return;
|
|
41
|
-
await this.request.session.save();
|
|
42
|
-
this.sessionChanged = false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
class $dee2a34a598960d9$export$2e2bcd8739ae039 {
|
|
49
|
-
constructor(config){
|
|
13
|
+
class LogtoClient {
|
|
14
|
+
constructor(config) {
|
|
50
15
|
this.config = config;
|
|
51
|
-
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode)=>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
}, this.ironSessionConfigs);
|
|
65
|
-
this.handleSignOut = (redirectUri = this.config.baseUrl)=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
66
|
-
const nodeClient = this.createNodeClient(request);
|
|
67
|
-
await nodeClient.signOut(redirectUri);
|
|
68
|
-
request.session.destroy();
|
|
16
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => next.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) => next.withIronSessionApiRoute(async (request, response) => {
|
|
25
|
+
const nodeClient = this.createNodeClient(request);
|
|
26
|
+
if (request.url) {
|
|
27
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
69
28
|
await this.storage?.save();
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
}
|
|
29
|
+
response.redirect(redirectTo);
|
|
30
|
+
}
|
|
31
|
+
}, this.ironSessionConfigs);
|
|
32
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl) => next.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 = {}) => next.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 = {}) => next.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);
|
|
102
75
|
}
|
|
103
76
|
createNodeClient(request) {
|
|
104
|
-
this.storage = new (
|
|
105
|
-
return new (
|
|
77
|
+
this.storage = new storage.default(request);
|
|
78
|
+
return new NodeClient__default.default(this.config, {
|
|
106
79
|
storage: this.storage,
|
|
107
|
-
navigate: (url)=>{
|
|
80
|
+
navigate: (url) => {
|
|
108
81
|
this.navigateUrl = url;
|
|
109
|
-
}
|
|
82
|
+
},
|
|
110
83
|
});
|
|
111
84
|
}
|
|
112
85
|
get ironSessionConfigs() {
|
|
@@ -115,8 +88,8 @@ class $dee2a34a598960d9$export$2e2bcd8739ae039 {
|
|
|
115
88
|
password: this.config.cookieSecret,
|
|
116
89
|
cookieOptions: {
|
|
117
90
|
secure: this.config.cookieSecure,
|
|
118
|
-
maxAge:
|
|
119
|
-
}
|
|
91
|
+
maxAge: 14 * 24 * 60 * 60,
|
|
92
|
+
},
|
|
120
93
|
};
|
|
121
94
|
}
|
|
122
95
|
async getLogtoUserFromRequest(request, configs) {
|
|
@@ -125,5 +98,12 @@ class $dee2a34a598960d9$export$2e2bcd8739ae039 {
|
|
|
125
98
|
}
|
|
126
99
|
}
|
|
127
100
|
|
|
128
|
-
|
|
129
|
-
|
|
101
|
+
Object.defineProperty(exports, 'ReservedScope', {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
get: function () { return NodeClient.ReservedScope; }
|
|
104
|
+
});
|
|
105
|
+
Object.defineProperty(exports, 'UserScope', {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
get: function () { return NodeClient.UserScope; }
|
|
108
|
+
});
|
|
109
|
+
exports.default = LogtoClient;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import NodeClient from '@logto/node';
|
|
2
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
3
|
+
import { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';
|
|
4
|
+
import NextStorage from './storage.mjs';
|
|
5
|
+
|
|
6
|
+
class LogtoClient {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => withIronSessionApiRoute(async (request, response) => {
|
|
10
|
+
const nodeClient = this.createNodeClient(request);
|
|
11
|
+
await nodeClient.signIn(redirectUri, interactionMode);
|
|
12
|
+
await this.storage?.save();
|
|
13
|
+
if (this.navigateUrl) {
|
|
14
|
+
response.redirect(this.navigateUrl);
|
|
15
|
+
}
|
|
16
|
+
}, this.ironSessionConfigs);
|
|
17
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl) => withIronSessionApiRoute(async (request, response) => {
|
|
18
|
+
const nodeClient = this.createNodeClient(request);
|
|
19
|
+
if (request.url) {
|
|
20
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
21
|
+
await this.storage?.save();
|
|
22
|
+
response.redirect(redirectTo);
|
|
23
|
+
}
|
|
24
|
+
}, this.ironSessionConfigs);
|
|
25
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl) => withIronSessionApiRoute(async (request, response) => {
|
|
26
|
+
const nodeClient = this.createNodeClient(request);
|
|
27
|
+
await nodeClient.signOut(redirectUri);
|
|
28
|
+
request.session.destroy();
|
|
29
|
+
await this.storage?.save();
|
|
30
|
+
if (this.navigateUrl) {
|
|
31
|
+
response.redirect(this.navigateUrl);
|
|
32
|
+
}
|
|
33
|
+
}, this.ironSessionConfigs);
|
|
34
|
+
this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
|
|
35
|
+
response.json(request.user);
|
|
36
|
+
}, configs);
|
|
37
|
+
this.handleAuthRoutes = (configs) => (request, response) => {
|
|
38
|
+
const { action } = request.query;
|
|
39
|
+
if (action === 'sign-in') {
|
|
40
|
+
return this.handleSignIn()(request, response);
|
|
41
|
+
}
|
|
42
|
+
if (action === 'sign-up') {
|
|
43
|
+
return this.handleSignIn(undefined, 'signUp')(request, response);
|
|
44
|
+
}
|
|
45
|
+
if (action === 'sign-in-callback') {
|
|
46
|
+
return this.handleSignInCallback()(request, response);
|
|
47
|
+
}
|
|
48
|
+
if (action === 'sign-out') {
|
|
49
|
+
return this.handleSignOut()(request, response);
|
|
50
|
+
}
|
|
51
|
+
if (action === 'user') {
|
|
52
|
+
return this.handleUser(configs)(request, response);
|
|
53
|
+
}
|
|
54
|
+
response.status(404).end();
|
|
55
|
+
};
|
|
56
|
+
this.withLogtoApiRoute = (handler, config = {}) => withIronSessionApiRoute(async (request, response) => {
|
|
57
|
+
const user = await this.getLogtoUserFromRequest(request, config);
|
|
58
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
59
|
+
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
|
|
60
|
+
return handler(request, response);
|
|
61
|
+
}, this.ironSessionConfigs);
|
|
62
|
+
this.withLogtoSsr = (handler, configs = {}) => withIronSessionSsr(async (context) => {
|
|
63
|
+
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
64
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
65
|
+
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
|
|
66
|
+
return handler(context);
|
|
67
|
+
}, this.ironSessionConfigs);
|
|
68
|
+
}
|
|
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
|
+
}
|
|
93
|
+
|
|
94
|
+
export { LogtoClient as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/storage.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'http';
|
|
2
|
+
import type { Storage, StorageKey } from '@logto/node';
|
|
3
|
+
export default class NextStorage implements Storage {
|
|
4
|
+
private readonly request;
|
|
5
|
+
private sessionChanged;
|
|
6
|
+
constructor(request: IncomingMessage);
|
|
7
|
+
setItem(key: StorageKey, value: string): Promise<void>;
|
|
8
|
+
getItem(key: StorageKey): Promise<string | null>;
|
|
9
|
+
removeItem(key: StorageKey): Promise<void>;
|
|
10
|
+
save(): Promise<void>;
|
|
11
|
+
}
|
package/lib/storage.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
class NextStorage {
|
|
6
|
+
constructor(request) {
|
|
7
|
+
this.request = request;
|
|
8
|
+
this.sessionChanged = false;
|
|
9
|
+
}
|
|
10
|
+
async setItem(key, value) {
|
|
11
|
+
this.request.session[key] = value;
|
|
12
|
+
this.sessionChanged = true;
|
|
13
|
+
}
|
|
14
|
+
async getItem(key) {
|
|
15
|
+
const value = this.request.session[key];
|
|
16
|
+
if (value === undefined) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return String(value);
|
|
20
|
+
}
|
|
21
|
+
async removeItem(key) {
|
|
22
|
+
this.request.session[key] = undefined;
|
|
23
|
+
this.sessionChanged = true;
|
|
24
|
+
}
|
|
25
|
+
async save() {
|
|
26
|
+
if (!this.sessionChanged) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
await this.request.session.save();
|
|
30
|
+
this.sessionChanged = false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exports.default = NextStorage;
|
package/lib/storage.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class NextStorage {
|
|
2
|
+
constructor(request) {
|
|
3
|
+
this.request = request;
|
|
4
|
+
this.sessionChanged = false;
|
|
5
|
+
}
|
|
6
|
+
async setItem(key, value) {
|
|
7
|
+
this.request.session[key] = value;
|
|
8
|
+
this.sessionChanged = true;
|
|
9
|
+
}
|
|
10
|
+
async getItem(key) {
|
|
11
|
+
const value = this.request.session[key];
|
|
12
|
+
if (value === undefined) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return String(value);
|
|
16
|
+
}
|
|
17
|
+
async removeItem(key) {
|
|
18
|
+
this.request.session[key] = undefined;
|
|
19
|
+
this.sessionChanged = true;
|
|
20
|
+
}
|
|
21
|
+
async save() {
|
|
22
|
+
if (!this.sessionChanged) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
await this.request.session.save();
|
|
26
|
+
this.sessionChanged = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { NextStorage as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { LogtoConfig } from '@logto/node';
|
|
2
|
+
import type { IronSession } from 'iron-session';
|
|
3
|
+
import type { NextApiRequest } from 'next';
|
|
4
|
+
export type NextRequestWithIronSession = NextApiRequest & {
|
|
5
|
+
session: IronSession;
|
|
6
|
+
};
|
|
7
|
+
declare module 'iron-session' {
|
|
8
|
+
interface IronSessionData {
|
|
9
|
+
accessToken?: string;
|
|
10
|
+
idToken?: string;
|
|
11
|
+
signInSession?: string;
|
|
12
|
+
refreshToken?: string;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export type LogtoNextConfig = LogtoConfig & {
|
|
16
|
+
cookieSecret: string;
|
|
17
|
+
cookieSecure: boolean;
|
|
18
|
+
baseUrl: string;
|
|
19
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/next",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
"require": "./lib/index.js",
|
|
8
|
-
"import": "./lib/
|
|
8
|
+
"import": "./lib/index.mjs"
|
|
9
9
|
},
|
|
10
|
-
"module": "./lib/
|
|
10
|
+
"module": "./lib/index.mjs",
|
|
11
11
|
"types": "./lib/index.d.ts",
|
|
12
12
|
"files": [
|
|
13
13
|
"lib"
|
|
@@ -22,38 +22,35 @@
|
|
|
22
22
|
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
23
23
|
"precommit": "lint-staged",
|
|
24
24
|
"check": "tsc --noEmit",
|
|
25
|
-
"build": "rm -rf lib/ &&
|
|
25
|
+
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
26
26
|
"lint": "eslint --ext .ts src",
|
|
27
27
|
"test": "jest",
|
|
28
28
|
"test:coverage": "jest --silent --coverage",
|
|
29
29
|
"prepack": "pnpm test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@logto/node": "^1.1.
|
|
32
|
+
"@logto/node": "^1.1.2",
|
|
33
33
|
"iron-session": "^6.1.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@jest/types": "^
|
|
37
|
-
"@parcel/core": "^2.8.3",
|
|
38
|
-
"@parcel/packager-ts": "^2.8.3",
|
|
39
|
-
"@parcel/transformer-typescript-types": "^2.8.3",
|
|
36
|
+
"@jest/types": "^29.5.0",
|
|
40
37
|
"@silverhand/eslint-config": "^2.0.0",
|
|
41
38
|
"@silverhand/ts-config": "^1.0.0",
|
|
42
39
|
"@silverhand/ts-config-react": "^2.0.0",
|
|
43
|
-
"@
|
|
44
|
-
"
|
|
45
|
-
"jest": "^
|
|
40
|
+
"@swc/core": "^1.3.50",
|
|
41
|
+
"@swc/jest": "^0.2.24",
|
|
42
|
+
"@types/jest": "^29.5.0",
|
|
43
|
+
"eslint": "^8.38.0",
|
|
44
|
+
"jest": "^29.5.0",
|
|
46
45
|
"jest-location-mock": "^1.0.9",
|
|
47
46
|
"jest-matcher-specific-error": "^1.0.0",
|
|
48
47
|
"lint-staged": "^13.0.0",
|
|
49
48
|
"next": "^12.2.2",
|
|
50
49
|
"next-test-api-route-handler": "^3.1.6",
|
|
51
|
-
"
|
|
52
|
-
"prettier": "^2.7.1",
|
|
50
|
+
"prettier": "^2.8.7",
|
|
53
51
|
"react": "^17.0.2",
|
|
54
52
|
"react-dom": "^17.0.2",
|
|
55
|
-
"
|
|
56
|
-
"typescript": "4.9.5"
|
|
53
|
+
"typescript": "^5.0.0"
|
|
57
54
|
},
|
|
58
55
|
"peerDependencies": {
|
|
59
56
|
"next": ">=12"
|
|
@@ -73,5 +70,5 @@
|
|
|
73
70
|
}
|
|
74
71
|
}
|
|
75
72
|
},
|
|
76
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "9e9a8b0887ef67baa7c3c564590bb06e7801d03e"
|
|
77
74
|
}
|
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;ACMA,eAAe,cAAc,CAAC;IAG5B,UAAU,eAAe;QACvB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;CACF;AAED,uBAA8B,WAAW,GAAG;IAC1C,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;ACXF,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvD,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEjE;gBAG+B,MAAM,EAAE,eAAe;IAEpD,YAAY,2CAEQ,eAAe,KAChC,cAAc,CASa;IAE9B,oBAAoB,2BAAuC,cAAc,CAS3C;IAE9B,aAAa,4BAAwC,cAAc,CAWrC;IAE9B,UAAU,aAAc,oBAAoB,yBAG9B;IAEd,gBAAgB,aACH,oBAAoB,KAAG,cAAc,CAyB9C;IAEJ,iBAAiB,YACN,cAAc,WACf,oBAAoB,KAC3B,cAAc,CAQa;IAE9B,YAAY,mFAEC,yBAAyB,mFAE3B,oBAAoB,oJAQD;IAa9B,OAAO,KAAK,kBAAkB,GAS7B;CAOF","sources":["packages/next/src/src/storage.ts","packages/next/src/src/types.ts","packages/next/src/src/index.ts","packages/next/src/index.ts"],"sourcesContent":[null,null,null,"import type { IncomingMessage } from 'http';\n\nimport type { GetContextParameters, InteractionMode } from '@logto/node';\nimport NodeClient from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport type { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport type { LogtoNextConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext, InteractionMode } from '@logto/node';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (\n redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`,\n interactionMode?: InteractionMode\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri, interactionMode);\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleSignInCallback = (redirectTo = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);\n await this.storage?.save();\n response.redirect(redirectTo);\n }\n }, this.ironSessionConfigs);\n\n handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signOut(redirectUri);\n\n request.session.destroy();\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleUser = (configs?: GetContextParameters) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, configs);\n\n handleAuthRoutes =\n (configs?: GetContextParameters): NextApiHandler =>\n (request, response) => {\n const { action } = request.query;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response);\n }\n\n if (action === 'sign-up') {\n return this.handleSignIn(undefined, 'signUp')(request, response);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response);\n }\n\n if (action === 'user') {\n return this.handleUser(configs)(request, response);\n }\n\n response.status(404).end();\n };\n\n withLogtoApiRoute = (\n handler: NextApiHandler,\n config: GetContextParameters = {}\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config);\n\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n\n return handler(request, response);\n }, this.ironSessionConfigs);\n\n withLogtoSsr = <P extends Record<string, unknown> = Record<string, unknown>>(\n handler: (\n context: GetServerSidePropsContext\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n configs: GetContextParameters = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, configs);\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });\n\n return handler(context);\n }, this.ironSessionConfigs);\n\n private createNodeClient(request: IncomingMessage) {\n this.storage = new NextStorage(request);\n\n return new NodeClient(this.config, {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\n });\n }\n\n private get ironSessionConfigs() {\n return {\n cookieName: `logto:${this.config.appId}`,\n password: this.config.cookieSecret,\n cookieOptions: {\n secure: this.config.cookieSecure,\n maxAge: 14 * 24 * 60 * 60,\n },\n };\n }\n\n private async getLogtoUserFromRequest(request: IncomingMessage, configs: GetContextParameters) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(configs);\n }\n}\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;AAAA;;ACIe;IAEb,YAA6B,QAA0B;uBAA1B;aADrB,iBAAiB,KAAK;IAC0B;IAExD,MAAM,QAAQ,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;QAC5B,IAAI,CAAC,cAAc,GAAG,IAAI;IAC5B;IAEA,MAAM,QAAQ,GAAe,EAAE;QAC7B,MAAM,QAAQ,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI;QAEvC,IAAI,UAAU,WACZ,OAAO,IAAI;QAGb,OAAO,OAAO;IAChB;IAEA,MAAM,WAAW,GAAe,EAAE;QAChC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;QAC5B,IAAI,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,EAAE;gBAC7D,8DAA8D;gBAC9D,OAAO,cAAc,CAAC,QAAQ,GAAG,EAAE,QAAQ;oBAAE,YAAY,IAAI;oBAAE,KAAK,IAAM;gBAAK;gBAE/E,OAAO,QAAQ;YACjB,GAAG,IAAI,CAAC,kBAAkB;IAlG2B;IAoG/C,iBAAiB,OAAwB,EAAE;QACjD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA,GAAA,wCAAW,AAAD,EAAE;QAE/B,OAAO,IAAI,CAAA,GAAA,0CAAS,EAAE,IAAI,CAAC,MAAM,EAAE;YACjC,SAAS,IAAI,CAAC,OAAO;YACrB,UAAU,CAAC,MAAQ;gBACjB,IAAI,CAAC,WAAW,GAAG;YACrB;QACF;IACF;IAEA,IAAY,qBAAqB;QAC/B,OAAO;YACL,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACxC,UAAU,IAAI,CAAC,MAAM,CAAC,YAAY;YAClC,eAAe;gBACb,QAAQ,IAAI,CAAC,MAAM,CAAC,YAAY;gBAChC,QAAQ;YACV;QACF;IACF;IAEA,MAAc,wBAAwB,OAAwB,EAAE,OAA6B,EAAE;QAC7F,MAAM,aAAa,IAAI,CAAC,gBAAgB,CAAC;QAEzC,OAAO,WAAW,UAAU,CAAC;IAC/B;AACF","sources":["packages/next/src/index.ts","packages/next/src/storage.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\n\nimport type { GetContextParameters, InteractionMode } from '@logto/node';\nimport NodeClient from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport type { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport type { LogtoNextConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext, InteractionMode } from '@logto/node';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (\n redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`,\n interactionMode?: InteractionMode\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri, interactionMode);\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleSignInCallback = (redirectTo = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);\n await this.storage?.save();\n response.redirect(redirectTo);\n }\n }, this.ironSessionConfigs);\n\n handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signOut(redirectUri);\n\n request.session.destroy();\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleUser = (configs?: GetContextParameters) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, configs);\n\n handleAuthRoutes =\n (configs?: GetContextParameters): NextApiHandler =>\n (request, response) => {\n const { action } = request.query;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response);\n }\n\n if (action === 'sign-up') {\n return this.handleSignIn(undefined, 'signUp')(request, response);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response);\n }\n\n if (action === 'user') {\n return this.handleUser(configs)(request, response);\n }\n\n response.status(404).end();\n };\n\n withLogtoApiRoute = (\n handler: NextApiHandler,\n config: GetContextParameters = {}\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config);\n\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n\n return handler(request, response);\n }, this.ironSessionConfigs);\n\n withLogtoSsr = <P extends Record<string, unknown> = Record<string, unknown>>(\n handler: (\n context: GetServerSidePropsContext\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n configs: GetContextParameters = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, configs);\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });\n\n return handler(context);\n }, this.ironSessionConfigs);\n\n private createNodeClient(request: IncomingMessage) {\n this.storage = new NextStorage(request);\n\n return new NodeClient(this.config, {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\n });\n }\n\n private get ironSessionConfigs() {\n return {\n cookieName: `logto:${this.config.appId}`,\n password: this.config.cookieSecret,\n cookieOptions: {\n secure: this.config.cookieSecure,\n maxAge: 14 * 24 * 60 * 60,\n },\n };\n }\n\n private async getLogtoUserFromRequest(request: IncomingMessage, configs: GetContextParameters) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(configs);\n }\n}\n","import type { IncomingMessage } from 'http';\n\nimport type { Storage, StorageKey } from '@logto/node';\n\nexport default class NextStorage implements Storage {\n private sessionChanged = false;\n constructor(private readonly request: IncomingMessage) {}\n\n async setItem(key: StorageKey, value: string) {\n this.request.session[key] = value;\n this.sessionChanged = true;\n }\n\n async getItem(key: StorageKey) {\n const value = this.request.session[key];\n\n if (value === undefined) {\n return null;\n }\n\n return String(value);\n }\n\n async removeItem(key: StorageKey) {\n this.request.session[key] = undefined;\n this.sessionChanged = true;\n }\n\n async save() {\n if (!this.sessionChanged) {\n return;\n }\n\n await this.request.session.save();\n this.sessionChanged = false;\n }\n}\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../../"}
|
package/lib/module.d.mts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { LogtoConfig, GetContextParameters, InteractionMode } from "@logto/node";
|
|
2
|
-
import { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from "next";
|
|
3
|
-
declare module 'iron-session' {
|
|
4
|
-
interface IronSessionData {
|
|
5
|
-
accessToken?: string;
|
|
6
|
-
idToken?: string;
|
|
7
|
-
signInSession?: string;
|
|
8
|
-
refreshToken?: string;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
type LogtoNextConfig = LogtoConfig & {
|
|
12
|
-
cookieSecret: string;
|
|
13
|
-
cookieSecure: boolean;
|
|
14
|
-
baseUrl: string;
|
|
15
|
-
};
|
|
16
|
-
export { ReservedScope, UserScope } from '@logto/node';
|
|
17
|
-
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
18
|
-
export default class LogtoClient {
|
|
19
|
-
constructor(config: LogtoNextConfig);
|
|
20
|
-
handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => NextApiHandler;
|
|
21
|
-
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
22
|
-
handleSignOut: (redirectUri?: string) => NextApiHandler;
|
|
23
|
-
handleUser: (configs?: GetContextParameters) => NextApiHandler<any>;
|
|
24
|
-
handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
|
|
25
|
-
withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters) => NextApiHandler;
|
|
26
|
-
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters) => (context: GetServerSidePropsContext<import("querystring").ParsedUrlQuery, import("next").PreviewData>) => Promise<GetServerSidePropsResult<P>>;
|
|
27
|
-
private get ironSessionConfigs();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/module.mjs
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import $a8eQ2$logtonode, {ReservedScope as $7d6712a39300ba99$re_export$ReservedScope, UserScope as $7d6712a39300ba99$re_export$UserScope} from "@logto/node";
|
|
2
|
-
import {withIronSessionApiRoute as $a8eQ2$withIronSessionApiRoute, withIronSessionSsr as $a8eQ2$withIronSessionSsr} from "iron-session/next";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class $8fa7922f9b3d24ed$export$2e2bcd8739ae039 {
|
|
7
|
-
constructor(request){
|
|
8
|
-
this.request = request;
|
|
9
|
-
this.sessionChanged = false;
|
|
10
|
-
}
|
|
11
|
-
async setItem(key, value) {
|
|
12
|
-
this.request.session[key] = value;
|
|
13
|
-
this.sessionChanged = true;
|
|
14
|
-
}
|
|
15
|
-
async getItem(key) {
|
|
16
|
-
const value = this.request.session[key];
|
|
17
|
-
if (value === undefined) return null;
|
|
18
|
-
return String(value);
|
|
19
|
-
}
|
|
20
|
-
async removeItem(key) {
|
|
21
|
-
this.request.session[key] = undefined;
|
|
22
|
-
this.sessionChanged = true;
|
|
23
|
-
}
|
|
24
|
-
async save() {
|
|
25
|
-
if (!this.sessionChanged) return;
|
|
26
|
-
await this.request.session.save();
|
|
27
|
-
this.sessionChanged = false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class $7d6712a39300ba99$export$2e2bcd8739ae039 {
|
|
34
|
-
constructor(config){
|
|
35
|
-
this.config = config;
|
|
36
|
-
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
37
|
-
const nodeClient = this.createNodeClient(request);
|
|
38
|
-
await nodeClient.signIn(redirectUri, interactionMode);
|
|
39
|
-
await this.storage?.save();
|
|
40
|
-
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
41
|
-
}, this.ironSessionConfigs);
|
|
42
|
-
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
43
|
-
const nodeClient = this.createNodeClient(request);
|
|
44
|
-
if (request.url) {
|
|
45
|
-
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
46
|
-
await this.storage?.save();
|
|
47
|
-
response.redirect(redirectTo);
|
|
48
|
-
}
|
|
49
|
-
}, this.ironSessionConfigs);
|
|
50
|
-
this.handleSignOut = (redirectUri = this.config.baseUrl)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
51
|
-
const nodeClient = this.createNodeClient(request);
|
|
52
|
-
await nodeClient.signOut(redirectUri);
|
|
53
|
-
request.session.destroy();
|
|
54
|
-
await this.storage?.save();
|
|
55
|
-
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
56
|
-
}, this.ironSessionConfigs);
|
|
57
|
-
this.handleUser = (configs)=>this.withLogtoApiRoute((request, response)=>{
|
|
58
|
-
response.json(request.user);
|
|
59
|
-
}, configs);
|
|
60
|
-
this.handleAuthRoutes = (configs)=>(request, response)=>{
|
|
61
|
-
const { action: action } = request.query;
|
|
62
|
-
if (action === "sign-in") return this.handleSignIn()(request, response);
|
|
63
|
-
if (action === "sign-up") return this.handleSignIn(undefined, "signUp")(request, response);
|
|
64
|
-
if (action === "sign-in-callback") return this.handleSignInCallback()(request, response);
|
|
65
|
-
if (action === "sign-out") return this.handleSignOut()(request, response);
|
|
66
|
-
if (action === "user") return this.handleUser(configs)(request, response);
|
|
67
|
-
response.status(404).end();
|
|
68
|
-
};
|
|
69
|
-
this.withLogtoApiRoute = (handler, config = {})=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
70
|
-
const user = await this.getLogtoUserFromRequest(request, config);
|
|
71
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
72
|
-
Object.defineProperty(request, "user", {
|
|
73
|
-
enumerable: true,
|
|
74
|
-
get: ()=>user
|
|
75
|
-
});
|
|
76
|
-
return handler(request, response);
|
|
77
|
-
}, this.ironSessionConfigs);
|
|
78
|
-
this.withLogtoSsr = (handler, configs = {})=>(0, $a8eQ2$withIronSessionSsr)(async (context)=>{
|
|
79
|
-
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
80
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
81
|
-
Object.defineProperty(context.req, "user", {
|
|
82
|
-
enumerable: true,
|
|
83
|
-
get: ()=>user
|
|
84
|
-
});
|
|
85
|
-
return handler(context);
|
|
86
|
-
}, this.ironSessionConfigs);
|
|
87
|
-
}
|
|
88
|
-
createNodeClient(request) {
|
|
89
|
-
this.storage = new (0, $8fa7922f9b3d24ed$export$2e2bcd8739ae039)(request);
|
|
90
|
-
return new (0, $a8eQ2$logtonode)(this.config, {
|
|
91
|
-
storage: this.storage,
|
|
92
|
-
navigate: (url)=>{
|
|
93
|
-
this.navigateUrl = url;
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
get ironSessionConfigs() {
|
|
98
|
-
return {
|
|
99
|
-
cookieName: `logto:${this.config.appId}`,
|
|
100
|
-
password: this.config.cookieSecret,
|
|
101
|
-
cookieOptions: {
|
|
102
|
-
secure: this.config.cookieSecure,
|
|
103
|
-
maxAge: 1209600
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
async getLogtoUserFromRequest(request, configs) {
|
|
108
|
-
const nodeClient = this.createNodeClient(request);
|
|
109
|
-
return nodeClient.getContext(configs);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
export {$7d6712a39300ba99$export$2e2bcd8739ae039 as default, $7d6712a39300ba99$re_export$ReservedScope as ReservedScope, $7d6712a39300ba99$re_export$UserScope as UserScope};
|
|
115
|
-
//# sourceMappingURL=module.mjs.map
|
package/lib/module.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;ACIe;IAEb,YAA6B,QAA0B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AU,CAAC;IAC/B;AACF","sources":["packages/next/src/index.ts","packages/next/src/storage.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\n\nimport type { GetContextParameters, InteractionMode } from '@logto/node';\nimport NodeClient from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport type { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport type { LogtoNextConfig } from './types';\n\nexport { ReservedScope, UserScope } from '@logto/node';\n\nexport type { LogtoContext, InteractionMode } from '@logto/node';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (\n redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`,\n interactionMode?: InteractionMode\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri, interactionMode);\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleSignInCallback = (redirectTo = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n\n if (request.url) {\n await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);\n await this.storage?.save();\n response.redirect(redirectTo);\n }\n }, this.ironSessionConfigs);\n\n handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signOut(redirectUri);\n\n request.session.destroy();\n await this.storage?.save();\n\n if (this.navigateUrl) {\n response.redirect(this.navigateUrl);\n }\n }, this.ironSessionConfigs);\n\n handleUser = (configs?: GetContextParameters) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, configs);\n\n handleAuthRoutes =\n (configs?: GetContextParameters): NextApiHandler =>\n (request, response) => {\n const { action } = request.query;\n\n if (action === 'sign-in') {\n return this.handleSignIn()(request, response);\n }\n\n if (action === 'sign-up') {\n return this.handleSignIn(undefined, 'signUp')(request, response);\n }\n\n if (action === 'sign-in-callback') {\n return this.handleSignInCallback()(request, response);\n }\n\n if (action === 'sign-out') {\n return this.handleSignOut()(request, response);\n }\n\n if (action === 'user') {\n return this.handleUser(configs)(request, response);\n }\n\n response.status(404).end();\n };\n\n withLogtoApiRoute = (\n handler: NextApiHandler,\n config: GetContextParameters = {}\n ): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config);\n\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(request, 'user', { enumerable: true, get: () => user });\n\n return handler(request, response);\n }, this.ironSessionConfigs);\n\n withLogtoSsr = <P extends Record<string, unknown> = Record<string, unknown>>(\n handler: (\n context: GetServerSidePropsContext\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n configs: GetContextParameters = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, configs);\n // eslint-disable-next-line @silverhand/fp/no-mutating-methods\n Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });\n\n return handler(context);\n }, this.ironSessionConfigs);\n\n private createNodeClient(request: IncomingMessage) {\n this.storage = new NextStorage(request);\n\n return new NodeClient(this.config, {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\n });\n }\n\n private get ironSessionConfigs() {\n return {\n cookieName: `logto:${this.config.appId}`,\n password: this.config.cookieSecret,\n cookieOptions: {\n secure: this.config.cookieSecure,\n maxAge: 14 * 24 * 60 * 60,\n },\n };\n }\n\n private async getLogtoUserFromRequest(request: IncomingMessage, configs: GetContextParameters) {\n const nodeClient = this.createNodeClient(request);\n\n return nodeClient.getContext(configs);\n }\n}\n","import type { IncomingMessage } from 'http';\n\nimport type { Storage, StorageKey } from '@logto/node';\n\nexport default class NextStorage implements Storage {\n private sessionChanged = false;\n constructor(private readonly request: IncomingMessage) {}\n\n async setItem(key: StorageKey, value: string) {\n this.request.session[key] = value;\n this.sessionChanged = true;\n }\n\n async getItem(key: StorageKey) {\n const value = this.request.session[key];\n\n if (value === undefined) {\n return null;\n }\n\n return String(value);\n }\n\n async removeItem(key: StorageKey) {\n this.request.session[key] = undefined;\n this.sessionChanged = true;\n }\n\n async save() {\n if (!this.sessionChanged) {\n return;\n }\n\n await this.request.session.save();\n this.sessionChanged = false;\n }\n}\n"],"names":[],"version":3,"file":"module.mjs.map"}
|