@logto/next 1.1.1 → 2.0.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 +26 -0
- package/lib/edge/index.d.ts +7 -0
- package/lib/edge/index.js +14 -0
- package/lib/edge/iron-session-edge/get-property-descriptor-for-request-session.cjs +36 -0
- package/lib/edge/iron-session-edge/get-property-descriptor-for-request-session.d.ts +2 -0
- package/lib/edge/iron-session-edge/get-property-descriptor-for-request-session.js +32 -0
- package/lib/edge/iron-session-edge/index.cjs +30 -0
- package/lib/edge/iron-session-edge/index.d.ts +9 -0
- package/lib/edge/iron-session-edge/index.js +27 -0
- package/lib/src/client.cjs +101 -0
- package/lib/src/client.d.ts +20 -0
- package/lib/src/client.js +93 -0
- package/lib/src/index.cjs +26 -0
- package/lib/src/index.d.ts +7 -0
- package/lib/src/index.js +14 -0
- package/lib/src/index.test.d.ts +1 -0
- package/lib/src/storage.cjs +34 -0
- package/lib/src/storage.d.ts +11 -0
- package/lib/src/storage.js +30 -0
- package/lib/src/storage.test.d.ts +1 -0
- package/lib/src/types.d.ts +24 -0
- package/package.json +41 -44
- package/lib/index.d.ts +0 -30
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -129
- 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
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var client = require('../src/client.cjs');
|
|
6
|
+
var index = require('./iron-session-edge/index.cjs');
|
|
7
|
+
var NodeClient = require('@logto/node');
|
|
8
|
+
|
|
9
|
+
class LogtoClient extends client.default {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
super(config, {
|
|
12
|
+
withIronSessionApiRoute: index.withIronSessionApiRoute,
|
|
13
|
+
withIronSessionSsr: index.withIronSessionSsr,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
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
|
+
exports.default = LogtoClient;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import BaseClient from '../src/client';
|
|
2
|
+
import type { LogtoNextConfig } from '../src/types.js';
|
|
3
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
|
+
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
5
|
+
export default class LogtoClient extends BaseClient {
|
|
6
|
+
constructor(config: LogtoNextConfig);
|
|
7
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import LogtoNextBaseClient from '../src/client.js';
|
|
2
|
+
import { withIronSessionApiRoute, withIronSessionSsr } from './iron-session-edge/index.js';
|
|
3
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
|
+
|
|
5
|
+
class LogtoClient extends LogtoNextBaseClient {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
super(config, {
|
|
8
|
+
withIronSessionApiRoute,
|
|
9
|
+
withIronSessionSsr,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { LogtoClient as default };
|
|
@@ -0,0 +1,36 @@
|
|
|
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;
|
|
@@ -0,0 +1,32 @@
|
|
|
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 };
|
|
@@ -0,0 +1,30 @@
|
|
|
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;
|
|
@@ -0,0 +1,9 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,27 @@
|
|
|
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 };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var NodeClient = require('@logto/node');
|
|
6
|
+
var storage = require('./storage.cjs');
|
|
7
|
+
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
var NodeClient__default = /*#__PURE__*/_interopDefault(NodeClient);
|
|
11
|
+
|
|
12
|
+
class LogtoNextBaseClient {
|
|
13
|
+
constructor(config, adapters) {
|
|
14
|
+
this.config = config;
|
|
15
|
+
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
|
+
}
|
|
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
|
+
}
|
|
100
|
+
|
|
101
|
+
exports.default = LogtoNextBaseClient;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { GetContextParameters, InteractionMode } from '@logto/node';
|
|
2
|
+
import type { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';
|
|
3
|
+
import type { Adapters, LogtoNextConfig } from './types';
|
|
4
|
+
export default class LogtoNextBaseClient {
|
|
5
|
+
private readonly config;
|
|
6
|
+
private readonly adapters;
|
|
7
|
+
private navigateUrl?;
|
|
8
|
+
private storage?;
|
|
9
|
+
constructor(config: LogtoNextConfig, adapters: Adapters);
|
|
10
|
+
handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => NextApiHandler;
|
|
11
|
+
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
12
|
+
handleSignOut: (redirectUri?: string) => NextApiHandler;
|
|
13
|
+
handleUser: (configs?: GetContextParameters) => NextApiHandler;
|
|
14
|
+
handleAuthRoutes: (configs?: GetContextParameters) => NextApiHandler;
|
|
15
|
+
withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters) => NextApiHandler;
|
|
16
|
+
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters) => (context: GetServerSidePropsContext) => Promise<GetServerSidePropsResult<P>>;
|
|
17
|
+
private createNodeClient;
|
|
18
|
+
private get ironSessionConfigs();
|
|
19
|
+
private getLogtoUserFromRequest;
|
|
20
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import NodeClient from '@logto/node';
|
|
2
|
+
import NextStorage from './storage.js';
|
|
3
|
+
|
|
4
|
+
class LogtoNextBaseClient {
|
|
5
|
+
constructor(config, adapters) {
|
|
6
|
+
this.config = config;
|
|
7
|
+
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
|
+
}
|
|
68
|
+
createNodeClient(request) {
|
|
69
|
+
this.storage = new NextStorage(request);
|
|
70
|
+
return new NodeClient(this.config, {
|
|
71
|
+
storage: this.storage,
|
|
72
|
+
navigate: (url) => {
|
|
73
|
+
this.navigateUrl = url;
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
get ironSessionConfigs() {
|
|
78
|
+
return {
|
|
79
|
+
cookieName: `logto:${this.config.appId}`,
|
|
80
|
+
password: this.config.cookieSecret,
|
|
81
|
+
cookieOptions: {
|
|
82
|
+
secure: this.config.cookieSecure,
|
|
83
|
+
maxAge: 14 * 24 * 60 * 60,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
async getLogtoUserFromRequest(request, configs) {
|
|
88
|
+
const nodeClient = this.createNodeClient(request);
|
|
89
|
+
return nodeClient.getContext(configs);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { LogtoNextBaseClient as default };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var next = require('iron-session/next');
|
|
6
|
+
var client = require('./client.cjs');
|
|
7
|
+
var NodeClient = require('@logto/node');
|
|
8
|
+
|
|
9
|
+
class LogtoClient extends client.default {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
super(config, {
|
|
12
|
+
withIronSessionApiRoute: next.withIronSessionApiRoute,
|
|
13
|
+
withIronSessionSsr: next.withIronSessionSsr,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
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
|
+
exports.default = LogtoClient;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import LogtoNextBaseClient from './client.js';
|
|
2
|
+
import type { LogtoNextConfig } from './types.js';
|
|
3
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
|
+
export type { LogtoContext, InteractionMode } from '@logto/node';
|
|
5
|
+
export default class LogtoClient extends LogtoNextBaseClient {
|
|
6
|
+
constructor(config: LogtoNextConfig);
|
|
7
|
+
}
|
package/lib/src/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';
|
|
2
|
+
import LogtoNextBaseClient from './client.js';
|
|
3
|
+
export { ReservedScope, UserScope } from '@logto/node';
|
|
4
|
+
|
|
5
|
+
class LogtoClient extends LogtoNextBaseClient {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
super(config, {
|
|
8
|
+
withIronSessionApiRoute,
|
|
9
|
+
withIronSessionSsr,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { LogtoClient as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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;
|
|
@@ -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
|
+
}
|
|
@@ -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 {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { LogtoConfig } from '@logto/node';
|
|
2
|
+
import type { IronSession } from 'iron-session';
|
|
3
|
+
import type { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';
|
|
4
|
+
import type { NextApiRequest } from 'next';
|
|
5
|
+
export type NextRequestWithIronSession = NextApiRequest & {
|
|
6
|
+
session: IronSession;
|
|
7
|
+
};
|
|
8
|
+
declare module 'iron-session' {
|
|
9
|
+
interface IronSessionData {
|
|
10
|
+
accessToken?: string;
|
|
11
|
+
idToken?: string;
|
|
12
|
+
signInSession?: string;
|
|
13
|
+
refreshToken?: string;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export type LogtoNextConfig = LogtoConfig & {
|
|
17
|
+
cookieSecret: string;
|
|
18
|
+
cookieSecure: boolean;
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
};
|
|
21
|
+
export type Adapters = {
|
|
22
|
+
withIronSessionApiRoute: typeof withIronSessionApiRoute;
|
|
23
|
+
withIronSessionSsr: typeof withIronSessionSsr;
|
|
24
|
+
};
|
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.0.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/module.mjs",
|
|
11
|
-
"types": "./lib/index.d.ts",
|
|
12
20
|
"files": [
|
|
13
21
|
"lib"
|
|
14
22
|
],
|
|
@@ -18,60 +26,49 @@
|
|
|
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/ && pnpm check && parcel build && cp lib/index.d.ts lib/module.d.mts",
|
|
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.0.0",
|
|
31
|
+
"iron-session": "^6.3.1"
|
|
34
32
|
},
|
|
35
33
|
"devDependencies": {
|
|
36
|
-
"@
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@
|
|
41
|
-
"@
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"eslint": "^8.23.0",
|
|
45
|
-
"jest": "^27.5.1",
|
|
34
|
+
"@silverhand/eslint-config": "^3.0.1",
|
|
35
|
+
"@silverhand/ts-config": "^3.0.0",
|
|
36
|
+
"@silverhand/ts-config-react": "^3.0.0",
|
|
37
|
+
"@swc/core": "^1.3.50",
|
|
38
|
+
"@swc/jest": "^0.2.24",
|
|
39
|
+
"@types/jest": "^29.5.0",
|
|
40
|
+
"eslint": "^8.38.0",
|
|
41
|
+
"jest": "^29.5.0",
|
|
46
42
|
"jest-location-mock": "^1.0.9",
|
|
47
43
|
"jest-matcher-specific-error": "^1.0.0",
|
|
48
44
|
"lint-staged": "^13.0.0",
|
|
49
45
|
"next": "^12.2.2",
|
|
50
46
|
"next-test-api-route-handler": "^3.1.6",
|
|
51
|
-
"
|
|
52
|
-
"prettier": "^2.7.1",
|
|
47
|
+
"prettier": "^2.8.7",
|
|
53
48
|
"react": "^17.0.2",
|
|
54
49
|
"react-dom": "^17.0.2",
|
|
55
|
-
"
|
|
56
|
-
"typescript": "4.9.5"
|
|
50
|
+
"typescript": "^5.0.0"
|
|
57
51
|
},
|
|
58
52
|
"peerDependencies": {
|
|
59
53
|
"next": ">=12"
|
|
60
54
|
},
|
|
61
55
|
"eslintConfig": {
|
|
62
|
-
"extends": "@silverhand"
|
|
56
|
+
"extends": "@silverhand",
|
|
57
|
+
"rules": {
|
|
58
|
+
"unicorn/prefer-node-protocol": "off"
|
|
59
|
+
}
|
|
63
60
|
},
|
|
64
61
|
"prettier": "@silverhand/eslint-config/.prettierrc",
|
|
65
62
|
"publishConfig": {
|
|
66
63
|
"access": "public"
|
|
67
64
|
},
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
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": "node test.cjs && jest",
|
|
72
|
+
"test:coverage": "node test.cjs && jest --silent --coverage"
|
|
73
|
+
}
|
|
74
|
+
}
|
package/lib/index.d.ts
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/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
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
var $gpzKJ$logtonode = require("@logto/node");
|
|
2
|
-
var $gpzKJ$ironsessionnext = require("iron-session/next");
|
|
3
|
-
|
|
4
|
-
function $parcel$interopDefault(a) {
|
|
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
|
-
}
|
|
13
|
-
|
|
14
|
-
$parcel$defineInteropFlag(module.exports);
|
|
15
|
-
|
|
16
|
-
$parcel$export(module.exports, "default", () => $dee2a34a598960d9$export$2e2bcd8739ae039);
|
|
17
|
-
$parcel$export(module.exports, "ReservedScope", () => $dee2a34a598960d9$re_export$ReservedScope);
|
|
18
|
-
$parcel$export(module.exports, "UserScope", () => $dee2a34a598960d9$re_export$UserScope);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class $f80b46e338398246$export$2e2bcd8739ae039 {
|
|
22
|
-
constructor(request){
|
|
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){
|
|
50
|
-
this.config = config;
|
|
51
|
-
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode)=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
52
|
-
const nodeClient = this.createNodeClient(request);
|
|
53
|
-
await nodeClient.signIn(redirectUri, interactionMode);
|
|
54
|
-
await this.storage?.save();
|
|
55
|
-
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
56
|
-
}, this.ironSessionConfigs);
|
|
57
|
-
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
58
|
-
const nodeClient = this.createNodeClient(request);
|
|
59
|
-
if (request.url) {
|
|
60
|
-
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
61
|
-
await this.storage?.save();
|
|
62
|
-
response.redirect(redirectTo);
|
|
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();
|
|
69
|
-
await this.storage?.save();
|
|
70
|
-
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
71
|
-
}, this.ironSessionConfigs);
|
|
72
|
-
this.handleUser = (configs)=>this.withLogtoApiRoute((request, response)=>{
|
|
73
|
-
response.json(request.user);
|
|
74
|
-
}, configs);
|
|
75
|
-
this.handleAuthRoutes = (configs)=>(request, response)=>{
|
|
76
|
-
const { action: action } = request.query;
|
|
77
|
-
if (action === "sign-in") return this.handleSignIn()(request, response);
|
|
78
|
-
if (action === "sign-up") return this.handleSignIn(undefined, "signUp")(request, response);
|
|
79
|
-
if (action === "sign-in-callback") return this.handleSignInCallback()(request, response);
|
|
80
|
-
if (action === "sign-out") return this.handleSignOut()(request, response);
|
|
81
|
-
if (action === "user") return this.handleUser(configs)(request, response);
|
|
82
|
-
response.status(404).end();
|
|
83
|
-
};
|
|
84
|
-
this.withLogtoApiRoute = (handler, config = {})=>(0, $gpzKJ$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
85
|
-
const user = await this.getLogtoUserFromRequest(request, config);
|
|
86
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
87
|
-
Object.defineProperty(request, "user", {
|
|
88
|
-
enumerable: true,
|
|
89
|
-
get: ()=>user
|
|
90
|
-
});
|
|
91
|
-
return handler(request, response);
|
|
92
|
-
}, this.ironSessionConfigs);
|
|
93
|
-
this.withLogtoSsr = (handler, configs = {})=>(0, $gpzKJ$ironsessionnext.withIronSessionSsr)(async (context)=>{
|
|
94
|
-
const user = await this.getLogtoUserFromRequest(context.req, configs);
|
|
95
|
-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
96
|
-
Object.defineProperty(context.req, "user", {
|
|
97
|
-
enumerable: true,
|
|
98
|
-
get: ()=>user
|
|
99
|
-
});
|
|
100
|
-
return handler(context);
|
|
101
|
-
}, this.ironSessionConfigs);
|
|
102
|
-
}
|
|
103
|
-
createNodeClient(request) {
|
|
104
|
-
this.storage = new (0, $f80b46e338398246$export$2e2bcd8739ae039)(request);
|
|
105
|
-
return new (0, ($parcel$interopDefault($gpzKJ$logtonode)))(this.config, {
|
|
106
|
-
storage: this.storage,
|
|
107
|
-
navigate: (url)=>{
|
|
108
|
-
this.navigateUrl = url;
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
get ironSessionConfigs() {
|
|
113
|
-
return {
|
|
114
|
-
cookieName: `logto:${this.config.appId}`,
|
|
115
|
-
password: this.config.cookieSecret,
|
|
116
|
-
cookieOptions: {
|
|
117
|
-
secure: this.config.cookieSecure,
|
|
118
|
-
maxAge: 1209600
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
async getLogtoUserFromRequest(request, configs) {
|
|
123
|
-
const nodeClient = this.createNodeClient(request);
|
|
124
|
-
return nodeClient.getContext(configs);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
//# sourceMappingURL=index.js.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,CAAC,cAAc,GAAG,IAAI;IAC5B;IAEA,MAAM,OAAO;QACX,IAAI,CAAC,IAAI,CAAC,cAAc,EACtB;QAGF,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI;QAC/B,IA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G,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;uBAA1B;aADrB,iBAAiB,KAAK;IAC0B;IAExD,MAAM,QAAQ,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG;QAC5B,IAAI,CAAC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,CAAC,SAAS,QAAQ;oBAAE,YAAY,IAAI;oBAAE,KAAK,IAAM;gBAAK;gBAE3E,OAAO,QAAQ,SAAS;YAC1B,GAAG,IAAI,CAAC,kBAAkB;aAE5B,eAAe,CACb,SAGA,UAAgC,CAAC,CAAC,GAElC,CAAA,GAAA,yBAAiB,EAAE,OAAO,UAAY;gBACpC,MAAM,OAAO,MAAM,IAAI,CAAC,uBAAuB,CAAC,QAAQ,GAAG,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,gBAAS,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":"module.mjs.map"}
|