diff --git a/packages/console/src/assets/docs/tutorial/integrate-sdk/next.mdx b/packages/console/src/assets/docs/tutorial/integrate-sdk/next.mdx
index 9e057d559..705942720 100644
--- a/packages/console/src/assets/docs/tutorial/integrate-sdk/next.mdx
+++ b/packages/console/src/assets/docs/tutorial/integrate-sdk/next.mdx
@@ -54,7 +54,7 @@ Import and initialize LogtoClient:
{`// libraries/logto.js
-import { LogtoProvider, LogtoConfig } from '@logto/next';
+import LogtoClient from '@logto/next';
export const logtoClient = new LogtoClient({
endpoint: '${props.endpoint}',
@@ -88,14 +88,12 @@ Prepare [API routes](https://nextjs.org/docs/api-routes/introduction) to connect
Go back to your IDE/editor, use the following code to implement the API routes first:
-
+export default logtoClient.handleAuthRoutes();
+```
This will create 4 routes automatically:
@@ -179,35 +177,84 @@ Check [Next.js documentation](https://nextjs.org/docs/basic-features/data-fetchi
props.onNext(5)}
>
-Calling `.signOut()` will clear all the Logto data in memory and cookies if they exist.
+### Protect API routes
-After signing out, it'll be great to redirect user back to your website. Let's add `http://localhost:3000` as the Post Sign-out URI below before calling `api/logto/sign-out`.
+Wrap your handler with `logtoClient.withLogtoApiRoute`.
+
+```ts
+// pages/api/protected-resource.ts
+import { logtoClient } from '../../libraries/logto';
+
+export default logtoClient.withLogtoApiRoute((request, response) => {
+ if (!request.user.isAuthenticated) {
+ response.status(401).json({ message: 'Unauthorized' });
+
+ return;
+ }
+
+ response.json({
+ data: 'this_is_protected_resource',
+ });
+});
+```
+
+### Protect pages
+
+If you don't want anonymous users to access a page, use `logtoClient.withLogtoSsr` to get auth state, and redirect to sign-in route if not authenticated.
+
+```ts
+export const getServerSideProps = logtoClient.withLogtoSsr(async function ({ req, res }) {
+ const { user } = req;
+
+ if (!user.isAuthenticated) {
+ res.setHeader('location', '/api/logto/sign-in');
+ res.statusCode = 302;
+ res.end();
+ }
+
+ return {
+ props: { user },
+ };
+});
+```
+
+
+
+ props.onNext(6)}
+>
+
+Calling `/api/logto/sign-out` will clear all the Logto data in memory and cookies if they exist.
+
+After signing out, it'll be great to redirect user back to your website. Let's add `http://localhost:3000` as the Post Sign-out URI below before calling `/api/logto/sign-out`.
### Implement a sign-out button
-
-
-{`
-
+
+```
{`// libraries/logto.js
-import { LogtoProvider, LogtoConfig } from '@logto/next';
+import LogtoClient from '@logto/next';
export const logtoClient = new LogtoClient({
endpoint: '${props.endpoint}',
@@ -88,21 +88,19 @@ export const logtoClient = new LogtoClient({
返回你的 IDE 或编辑器,首先让我们使用如下代码来实现一组 API 路由:
-