Skip to content

Commit 0cc845a

Browse files
authored
fix(next): fix with logto ssr (#996)
resolved #990
1 parent dd42dad commit 0cc845a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

.changeset/odd-colts-scream.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@logto/next": patch
3+
---
4+
5+
fix function withLogtoSsr
6+
7+
It can be called in getServerSideProps now.

packages/next/src/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,21 @@ export default class LogtoClient extends LogtoNextBaseClient {
200200
onError?: (error: unknown) => unknown
201201
) =>
202202
async (context: GetServerSidePropsContext) => {
203-
return this.withLogtoApiRoute(
204-
async () => {
205-
return handler(context);
206-
},
207-
configs,
208-
onError
209-
);
203+
try {
204+
const nodeClient = await this.createNodeClientFromNextApi(context.req, context.res);
205+
const user = await nodeClient.getContext(configs);
206+
207+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
208+
Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
209+
210+
return await handler(context);
211+
} catch (error: unknown) {
212+
if (onError) {
213+
return onError(error);
214+
}
215+
216+
throw error;
217+
}
210218
};
211219

212220
async createNodeClientFromNextApi(

0 commit comments

Comments
 (0)