@logto/next 1.0.0-beta.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/LICENSE +21 -0
- package/lib/index.d.ts +45 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +147 -0
- package/lib/index.js.map +1 -0
- package/lib/module.js +135 -0
- package/lib/module.js.map +1 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Silverhand
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { IdTokenClaims, LogtoConfig } 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
|
+
declare module 'http' {
|
|
12
|
+
interface IncomingMessage {
|
|
13
|
+
user: LogtoUser;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
type LogtoNextConfig = LogtoConfig & {
|
|
17
|
+
cookieSecret: string;
|
|
18
|
+
cookieSecure: boolean;
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
};
|
|
21
|
+
export type LogtoUser = {
|
|
22
|
+
isAuthenticated: boolean;
|
|
23
|
+
claims?: IdTokenClaims;
|
|
24
|
+
accessToken?: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* @getAccessToken: if set to true, will try to get an access token and attach to req.user,
|
|
28
|
+
* if unable to grant an access token, will set req.user.isAuthenticated to false,
|
|
29
|
+
* this can make sure the refresh token is not revoked and still valid, so is considered more secure.
|
|
30
|
+
*/
|
|
31
|
+
type WithLogtoConfig = {
|
|
32
|
+
getAccessToken?: boolean;
|
|
33
|
+
};
|
|
34
|
+
export default class LogtoClient {
|
|
35
|
+
constructor(config: LogtoNextConfig);
|
|
36
|
+
handleSignIn: (redirectUri?: string) => NextApiHandler;
|
|
37
|
+
handleSignInCallback: (redirectTo?: string) => NextApiHandler;
|
|
38
|
+
handleSignOut: (redirectUri?: string) => NextApiHandler;
|
|
39
|
+
handleUser: (config?: WithLogtoConfig | undefined) => NextApiHandler<any>;
|
|
40
|
+
withLogtoApiRoute: (handler: NextApiHandler, config?: WithLogtoConfig) => NextApiHandler;
|
|
41
|
+
withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, config?: WithLogtoConfig) => (context: GetServerSidePropsContext<import("querystring").ParsedUrlQuery, import("next").PreviewData>) => Promise<GetServerSidePropsResult<P>>;
|
|
42
|
+
private get ironSessionConfigs();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;ACMA,eAAe,cAAc,CAAC;IAC5B,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,eAAe,MAAM,CAAC;IACpB,UAAU,eAAe;QACvB,IAAI,EAAE,SAAS,CAAC;KACjB;CACF;AAED,uBAA8B,WAAW,GAAG;IAC1C,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,wBAAwB;IACtB,eAAe,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AACH,uBAA8B;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AC7BF;gBAG+B,MAAM,EAAE,eAAe;IAEpD,YAAY,4BAAkE,cAAc,CAS9D;IAE9B,oBAAoB,2BAAuC,cAAc,CAS3C;IAE9B,aAAa,4BAAwC,cAAc,CAWrC;IAE9B,UAAU,gEAGG;IAEb,iBAAiB,YAAa,cAAc,WAAU,eAAe,KAAQ,cAAc,CAQ7D;IAE9B,YAAY,mFAEC,yBAAyB,kFAE5B,eAAe,oJAQK;IAmB9B,OAAO,KAAK,kBAAkB,GAS7B;CAwCF","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 { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig, LogtoUser, WithLogtoConfig } from './types';\n\nexport type { LogtoUser } from './types';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri);\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 = (config?: WithLogtoConfig) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, config);\n\n withLogtoApiRoute = (handler: NextApiHandler, config: WithLogtoConfig = {}): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);\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 config: WithLogtoConfig = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, config.getAccessToken);\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(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\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, getAccessToken?: boolean) {\n const nodeClient = this.createNodeClient(request);\n const { isAuthenticated } = nodeClient;\n\n if (!isAuthenticated) {\n const user: LogtoUser = {\n isAuthenticated,\n };\n\n return user;\n }\n\n if (!getAccessToken) {\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n };\n\n return user;\n }\n\n try {\n const accessToken = await nodeClient.getAccessToken();\n await this.storage?.save();\n\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n accessToken,\n };\n\n return user;\n } catch {\n return {\n isAuthenticated: false,\n };\n }\n }\n}\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
var $j3Dy6$logtonode = require("@logto/node");
|
|
2
|
+
var $j3Dy6$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", () => $3556748a90dddf6c$export$2e2bcd8739ae039);
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class $1abf01985e812f74$export$2e2bcd8739ae039 {
|
|
20
|
+
constructor(request){
|
|
21
|
+
this.request = request;
|
|
22
|
+
this.sessionChanged = false;
|
|
23
|
+
}
|
|
24
|
+
async setItem(key, value) {
|
|
25
|
+
this.request.session[key] = value;
|
|
26
|
+
this.sessionChanged = true;
|
|
27
|
+
}
|
|
28
|
+
getItem(key) {
|
|
29
|
+
const value = this.request.session[key];
|
|
30
|
+
if (value === undefined) return null;
|
|
31
|
+
return String(value);
|
|
32
|
+
}
|
|
33
|
+
removeItem(key) {
|
|
34
|
+
this.request.session[key] = undefined;
|
|
35
|
+
this.sessionChanged = true;
|
|
36
|
+
}
|
|
37
|
+
async save() {
|
|
38
|
+
if (!this.sessionChanged) return;
|
|
39
|
+
await this.request.session.save();
|
|
40
|
+
this.sessionChanged = false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class $3556748a90dddf6c$export$2e2bcd8739ae039 {
|
|
46
|
+
constructor(config1){
|
|
47
|
+
this.config = config1;
|
|
48
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`)=>(0, $j3Dy6$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
49
|
+
const nodeClient = this.createNodeClient(request);
|
|
50
|
+
await nodeClient.signIn(redirectUri);
|
|
51
|
+
await this.storage?.save();
|
|
52
|
+
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
53
|
+
}, this.ironSessionConfigs);
|
|
54
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>(0, $j3Dy6$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
55
|
+
const nodeClient = this.createNodeClient(request);
|
|
56
|
+
if (request.url) {
|
|
57
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
58
|
+
await this.storage?.save();
|
|
59
|
+
response.redirect(redirectTo);
|
|
60
|
+
}
|
|
61
|
+
}, this.ironSessionConfigs);
|
|
62
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl)=>(0, $j3Dy6$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
63
|
+
const nodeClient = this.createNodeClient(request);
|
|
64
|
+
await nodeClient.signOut(redirectUri);
|
|
65
|
+
request.session.destroy();
|
|
66
|
+
await this.storage?.save();
|
|
67
|
+
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
68
|
+
}, this.ironSessionConfigs);
|
|
69
|
+
this.handleUser = (config)=>this.withLogtoApiRoute((request, response)=>{
|
|
70
|
+
response.json(request.user);
|
|
71
|
+
}, config);
|
|
72
|
+
this.withLogtoApiRoute = (handler, config = {})=>(0, $j3Dy6$ironsessionnext.withIronSessionApiRoute)(async (request, response)=>{
|
|
73
|
+
const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);
|
|
74
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
75
|
+
Object.defineProperty(request, "user", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: ()=>user
|
|
78
|
+
});
|
|
79
|
+
return handler(request, response);
|
|
80
|
+
}, this.ironSessionConfigs);
|
|
81
|
+
this.withLogtoSsr = (handler, config = {})=>(0, $j3Dy6$ironsessionnext.withIronSessionSsr)(async (context)=>{
|
|
82
|
+
const user = await this.getLogtoUserFromRequest(context.req, config.getAccessToken);
|
|
83
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
84
|
+
Object.defineProperty(context.req, "user", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
get: ()=>user
|
|
87
|
+
});
|
|
88
|
+
return handler(context);
|
|
89
|
+
}, this.ironSessionConfigs);
|
|
90
|
+
}
|
|
91
|
+
createNodeClient(request) {
|
|
92
|
+
this.storage = new (0, $1abf01985e812f74$export$2e2bcd8739ae039)(request);
|
|
93
|
+
return new (0, ($parcel$interopDefault($j3Dy6$logtonode)))({
|
|
94
|
+
...this.config,
|
|
95
|
+
persistAccessToken: this.config.persistAccessToken ?? true
|
|
96
|
+
}, {
|
|
97
|
+
storage: this.storage,
|
|
98
|
+
navigate: (url)=>{
|
|
99
|
+
this.navigateUrl = url;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
get ironSessionConfigs() {
|
|
104
|
+
return {
|
|
105
|
+
cookieName: `logto:${this.config.appId}`,
|
|
106
|
+
password: this.config.cookieSecret,
|
|
107
|
+
cookieOptions: {
|
|
108
|
+
secure: this.config.cookieSecure,
|
|
109
|
+
maxAge: 1209600
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
async getLogtoUserFromRequest(request, getAccessToken) {
|
|
114
|
+
const nodeClient = this.createNodeClient(request);
|
|
115
|
+
const { isAuthenticated: isAuthenticated } = nodeClient;
|
|
116
|
+
if (!isAuthenticated) {
|
|
117
|
+
const user = {
|
|
118
|
+
isAuthenticated: isAuthenticated
|
|
119
|
+
};
|
|
120
|
+
return user;
|
|
121
|
+
}
|
|
122
|
+
if (!getAccessToken) {
|
|
123
|
+
const user = {
|
|
124
|
+
isAuthenticated: isAuthenticated,
|
|
125
|
+
claims: nodeClient.getIdTokenClaims()
|
|
126
|
+
};
|
|
127
|
+
return user;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const accessToken = await nodeClient.getAccessToken();
|
|
131
|
+
await this.storage?.save();
|
|
132
|
+
const user = {
|
|
133
|
+
isAuthenticated: isAuthenticated,
|
|
134
|
+
claims: nodeClient.getIdTokenClaims(),
|
|
135
|
+
accessToken: accessToken
|
|
136
|
+
};
|
|
137
|
+
return user;
|
|
138
|
+
} catch {
|
|
139
|
+
return {
|
|
140
|
+
isAuthenticated: false
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;AAAA;;ACIe;IAEb,YAA6B,OAAwB,CAAE;aAA1B,OAAwB,GAAxB,OAAwB;aAD7C,cAAc,GAAG,KAAK;KAC2B;IAEzD,MAAM,OAAO,CAAC,GAAe,EAAE,KAAa,EAAE;QAC5C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QAClC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;KAC5B;IAED,OAAO,CAAC,GAAe,EAAE;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,AAAC;QAExC,IAAI,KAAK,KAAK,SAAS,EACrB,OAAO,IAAI,CAAC;QAGd,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;KACtB;IAED,UAAU,CAAC,GAAe,EAAE;QAC1B,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;QACtC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;KAC5B;IAED,MAAM,IAAI,GAAG;QACX,IAAI,CAAC,IAAI,CAAC,cAAc,EACtB,OAAO;QAGT,MAAM,IAAI,CAAC,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,AAAC;QAEvC,IAAI,CAAC,eAAe,EAAE;YACpB,MAAM,IAAI,GAAc;iCACtB,eAAe;aAChB,AAAC;YAEF,OAAO,IAAI,CAAC;SACb;QAED,IAAI,CAAC,cAAc,EAAE;YACnB,MAAM,IAAI,GAAc;iCACtB,eAAe;gBACf,MAAM,EAAE,UAAU,CAAC,gBAAgB,EAAE;aACtC,AAAC;YAEF,OAAO,IAAI,CAAC;SACb;QAED,IAAI;YACF,MAAM,WAAW,GAAG,MAAM,UAAU,CAAC,cAAc,EAAE,AAAC;YACtD,MAAM,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC;YAE3B,MAAM,IAAI,GAAc;iCACtB,eAAe;gBACf,MAAM,EAAE,UAAU,CAAC,gBAAgB,EAAE;6BACrC,WAAW;aACZ,AAAC;YAEF,OAAO,IAAI,CAAC;SACb,CAAC,OAAM;YACN,OAAO;gBACL,eAAe,EAAE,KAAK;aACvB,CAAC;SACH;KACF;CACF","sources":["packages/next/src/index.ts","packages/next/src/storage.ts"],"sourcesContent":["import { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig, LogtoUser, WithLogtoConfig } from './types';\n\nexport type { LogtoUser } from './types';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri);\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 = (config?: WithLogtoConfig) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, config);\n\n withLogtoApiRoute = (handler: NextApiHandler, config: WithLogtoConfig = {}): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);\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 config: WithLogtoConfig = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, config.getAccessToken);\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(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\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, getAccessToken?: boolean) {\n const nodeClient = this.createNodeClient(request);\n const { isAuthenticated } = nodeClient;\n\n if (!isAuthenticated) {\n const user: LogtoUser = {\n isAuthenticated,\n };\n\n return user;\n }\n\n if (!getAccessToken) {\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n };\n\n return user;\n }\n\n try {\n const accessToken = await nodeClient.getAccessToken();\n await this.storage?.save();\n\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n accessToken,\n };\n\n return user;\n } catch {\n return {\n isAuthenticated: false,\n };\n }\n }\n}\n","import { IncomingMessage } from 'http';\n\nimport { 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 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 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.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import $a8eQ2$logtonode 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
|
+
getItem(key) {
|
|
16
|
+
const value = this.request.session[key];
|
|
17
|
+
if (value === undefined) return null;
|
|
18
|
+
return String(value);
|
|
19
|
+
}
|
|
20
|
+
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
|
+
class $7d6712a39300ba99$export$2e2bcd8739ae039 {
|
|
33
|
+
constructor(config1){
|
|
34
|
+
this.config = config1;
|
|
35
|
+
this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
36
|
+
const nodeClient = this.createNodeClient(request);
|
|
37
|
+
await nodeClient.signIn(redirectUri);
|
|
38
|
+
await this.storage?.save();
|
|
39
|
+
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
40
|
+
}, this.ironSessionConfigs);
|
|
41
|
+
this.handleSignInCallback = (redirectTo = this.config.baseUrl)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
42
|
+
const nodeClient = this.createNodeClient(request);
|
|
43
|
+
if (request.url) {
|
|
44
|
+
await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
|
|
45
|
+
await this.storage?.save();
|
|
46
|
+
response.redirect(redirectTo);
|
|
47
|
+
}
|
|
48
|
+
}, this.ironSessionConfigs);
|
|
49
|
+
this.handleSignOut = (redirectUri = this.config.baseUrl)=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
50
|
+
const nodeClient = this.createNodeClient(request);
|
|
51
|
+
await nodeClient.signOut(redirectUri);
|
|
52
|
+
request.session.destroy();
|
|
53
|
+
await this.storage?.save();
|
|
54
|
+
if (this.navigateUrl) response.redirect(this.navigateUrl);
|
|
55
|
+
}, this.ironSessionConfigs);
|
|
56
|
+
this.handleUser = (config)=>this.withLogtoApiRoute((request, response)=>{
|
|
57
|
+
response.json(request.user);
|
|
58
|
+
}, config);
|
|
59
|
+
this.withLogtoApiRoute = (handler, config = {})=>(0, $a8eQ2$withIronSessionApiRoute)(async (request, response)=>{
|
|
60
|
+
const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);
|
|
61
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
62
|
+
Object.defineProperty(request, "user", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
get: ()=>user
|
|
65
|
+
});
|
|
66
|
+
return handler(request, response);
|
|
67
|
+
}, this.ironSessionConfigs);
|
|
68
|
+
this.withLogtoSsr = (handler, config = {})=>(0, $a8eQ2$withIronSessionSsr)(async (context)=>{
|
|
69
|
+
const user = await this.getLogtoUserFromRequest(context.req, config.getAccessToken);
|
|
70
|
+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
|
|
71
|
+
Object.defineProperty(context.req, "user", {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
get: ()=>user
|
|
74
|
+
});
|
|
75
|
+
return handler(context);
|
|
76
|
+
}, this.ironSessionConfigs);
|
|
77
|
+
}
|
|
78
|
+
createNodeClient(request) {
|
|
79
|
+
this.storage = new (0, $8fa7922f9b3d24ed$export$2e2bcd8739ae039)(request);
|
|
80
|
+
return new (0, $a8eQ2$logtonode)({
|
|
81
|
+
...this.config,
|
|
82
|
+
persistAccessToken: this.config.persistAccessToken ?? true
|
|
83
|
+
}, {
|
|
84
|
+
storage: this.storage,
|
|
85
|
+
navigate: (url)=>{
|
|
86
|
+
this.navigateUrl = url;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
get ironSessionConfigs() {
|
|
91
|
+
return {
|
|
92
|
+
cookieName: `logto:${this.config.appId}`,
|
|
93
|
+
password: this.config.cookieSecret,
|
|
94
|
+
cookieOptions: {
|
|
95
|
+
secure: this.config.cookieSecure,
|
|
96
|
+
maxAge: 1209600
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async getLogtoUserFromRequest(request, getAccessToken) {
|
|
101
|
+
const nodeClient = this.createNodeClient(request);
|
|
102
|
+
const { isAuthenticated: isAuthenticated } = nodeClient;
|
|
103
|
+
if (!isAuthenticated) {
|
|
104
|
+
const user = {
|
|
105
|
+
isAuthenticated: isAuthenticated
|
|
106
|
+
};
|
|
107
|
+
return user;
|
|
108
|
+
}
|
|
109
|
+
if (!getAccessToken) {
|
|
110
|
+
const user = {
|
|
111
|
+
isAuthenticated: isAuthenticated,
|
|
112
|
+
claims: nodeClient.getIdTokenClaims()
|
|
113
|
+
};
|
|
114
|
+
return user;
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
const accessToken = await nodeClient.getAccessToken();
|
|
118
|
+
await this.storage?.save();
|
|
119
|
+
const user = {
|
|
120
|
+
isAuthenticated: isAuthenticated,
|
|
121
|
+
claims: nodeClient.getIdTokenClaims(),
|
|
122
|
+
accessToken: accessToken
|
|
123
|
+
};
|
|
124
|
+
return user;
|
|
125
|
+
} catch {
|
|
126
|
+
return {
|
|
127
|
+
isAuthenticated: false
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
export {$7d6712a39300ba99$export$2e2bcd8739ae039 as default};
|
|
135
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;AAAA;;ACIe;IAEb,YAA6B,OAAwB,CAAE;aAA1B,OAAwB,GAAxB,OAAwB;aAD7C,cAAc,GAAG,KAAK;KAC2B;IAEzD,MAAM,OAAO,CAAC,GAAe,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"],"sourcesContent":["import { IncomingMessage } from 'http';\n\nimport NodeClient from '@logto/node';\nimport { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';\nimport { GetServerSidePropsContext, GetServerSidePropsResult, NextApiHandler } from 'next';\n\nimport NextStorage from './storage';\nimport { LogtoNextConfig, LogtoUser, WithLogtoConfig } from './types';\n\nexport type { LogtoUser } from './types';\n\nexport default class LogtoClient {\n private navigateUrl?: string;\n private storage?: NextStorage;\n constructor(private readonly config: LogtoNextConfig) {}\n\n handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const nodeClient = this.createNodeClient(request);\n await nodeClient.signIn(redirectUri);\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 = (config?: WithLogtoConfig) =>\n this.withLogtoApiRoute((request, response) => {\n response.json(request.user);\n }, config);\n\n withLogtoApiRoute = (handler: NextApiHandler, config: WithLogtoConfig = {}): NextApiHandler =>\n withIronSessionApiRoute(async (request, response) => {\n const user = await this.getLogtoUserFromRequest(request, config.getAccessToken);\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 config: WithLogtoConfig = {}\n ) =>\n withIronSessionSsr(async (context) => {\n const user = await this.getLogtoUserFromRequest(context.req, config.getAccessToken);\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(\n {\n ...this.config,\n persistAccessToken: this.config.persistAccessToken ?? true,\n },\n {\n storage: this.storage,\n navigate: (url) => {\n this.navigateUrl = url;\n },\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, getAccessToken?: boolean) {\n const nodeClient = this.createNodeClient(request);\n const { isAuthenticated } = nodeClient;\n\n if (!isAuthenticated) {\n const user: LogtoUser = {\n isAuthenticated,\n };\n\n return user;\n }\n\n if (!getAccessToken) {\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n };\n\n return user;\n }\n\n try {\n const accessToken = await nodeClient.getAccessToken();\n await this.storage?.save();\n\n const user: LogtoUser = {\n isAuthenticated,\n claims: nodeClient.getIdTokenClaims(),\n accessToken,\n };\n\n return user;\n } catch {\n return {\n isAuthenticated: false,\n };\n }\n }\n}\n","import { IncomingMessage } from 'http';\n\nimport { 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 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 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.js.map"}
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@logto/next",
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
|
+
"source": "./src/index.ts",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"require": "./lib/index.js",
|
|
8
|
+
"import": "./lib/module.js"
|
|
9
|
+
},
|
|
10
|
+
"module": "./lib/module.js",
|
|
11
|
+
"types": "./lib/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"lib"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/logto-io/js.git",
|
|
19
|
+
"directory": "packages/next"
|
|
20
|
+
},
|
|
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",
|
|
26
|
+
"lint": "eslint --ext .ts src",
|
|
27
|
+
"test": "jest",
|
|
28
|
+
"test:coverage": "jest --silent --coverage",
|
|
29
|
+
"prepack": "pnpm test"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@logto/node": "^1.0.0-beta.0",
|
|
33
|
+
"iron-session": "^6.1.3"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@jest/types": "^27.5.1",
|
|
37
|
+
"@parcel/core": "^2.6.2",
|
|
38
|
+
"@parcel/packager-ts": "^2.6.2",
|
|
39
|
+
"@parcel/transformer-typescript-types": "^2.6.2",
|
|
40
|
+
"@silverhand/eslint-config": "^0.17.0",
|
|
41
|
+
"@silverhand/ts-config": "^0.14.0",
|
|
42
|
+
"@silverhand/ts-config-react": "^0.14.0",
|
|
43
|
+
"@types/jest": "^27.4.0",
|
|
44
|
+
"eslint": "^8.9.0",
|
|
45
|
+
"jest": "^27.5.1",
|
|
46
|
+
"jest-location-mock": "^1.0.9",
|
|
47
|
+
"jest-matcher-specific-error": "^1.0.0",
|
|
48
|
+
"lint-staged": "^13.0.0",
|
|
49
|
+
"next": "^12.2.2",
|
|
50
|
+
"next-test-api-route-handler": "^3.1.6",
|
|
51
|
+
"parcel": "^2.6.2",
|
|
52
|
+
"prettier": "^2.3.2",
|
|
53
|
+
"react": "^17.0.2",
|
|
54
|
+
"react-dom": "^17.0.2",
|
|
55
|
+
"ts-jest": "^27.0.4",
|
|
56
|
+
"typescript": "^4.5.5"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"next": ">=12"
|
|
60
|
+
},
|
|
61
|
+
"eslintConfig": {
|
|
62
|
+
"extends": "@silverhand"
|
|
63
|
+
},
|
|
64
|
+
"prettier": "@silverhand/eslint-config/.prettierrc",
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public"
|
|
67
|
+
},
|
|
68
|
+
"targets": {
|
|
69
|
+
"main": {
|
|
70
|
+
"context": "node",
|
|
71
|
+
"engines": {
|
|
72
|
+
"node": "^16"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"gitHead": "f0f78e6f0b97174de98588b35d1d12c8396206ba"
|
|
77
|
+
}
|