From 9f91da075b22d102ef1b56a6ef0699246fd86499 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Tue, 30 Jan 2024 11:49:06 +0800 Subject: [PATCH] refactor(console): update express guide (#5314) --- .../assets/docs/guides/web-express/README.mdx | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/packages/console/src/assets/docs/guides/web-express/README.mdx b/packages/console/src/assets/docs/guides/web-express/README.mdx index e145a67e0..e1a008406 100644 --- a/packages/console/src/assets/docs/guides/web-express/README.mdx +++ b/packages/console/src/assets/docs/guides/web-express/README.mdx @@ -9,8 +9,8 @@ import Step from '@/mdx-components/Step'; @@ -40,7 +40,6 @@ pnpm add @logto/express cookie-parser express-session @@ -84,8 +83,7 @@ app.use(session({ secret: '${generateStandardSecret()}', cookie: { maxAge: 14 * ### Configure Redirect URI @@ -126,34 +124,28 @@ app.get('/', (req, res) => { -In order to get user profile, we need to use the `withLogto` middleware: +Calling `/logto/sign-out` will clear all the Logto data in memory and cookies if they exist. -```ts +After signing out, it'll be great to redirect your user back to your website. Let's add `http://localhost:3000` as one of the Post Sign-out URIs in Admin Console (shows under Redirect URIs). + + + + + +In Logto SDK, you can use the `withLogto` middleware to get `req.user.isAuthenticated` to check the authentication status, if the user is signed in, the value will be `true`, otherwise, the value will be `false`. + +``ts import { withLogto } from '@logto/express'; app.use(withLogto(config)); ``` -Then the user profile will be attached to `req`, example usage: - -```ts -app.get('/user', (req, res) => { - res.json(req.user); -}); -``` - - - - - -After setting up `withLogto` in the previous step, we can protect routes by creating a simple middleware: +No, let's use this value to protect routes by creating a simple middleware: ```ts const requireAuth = async (req: Request, res: Response, next: NextFunction) => { @@ -165,24 +157,25 @@ const requireAuth = async (req: Request, res: Response, next: NextFunction) => { }; ``` -And then: +And then use it in the route handler: ```ts app.get('/protected', requireAuth, (req, res) => { res.end('protected resource'); }); ``` - -Calling `/logto/sign-out` will clear all the Logto data in memory and cookies if they exist. +Now, you can test your application: -After signing out, it'll be great to redirect your user back to your website. Let's add `http://localhost:3000` as one of the Post Sign-out URIs in Admin Console (shows under Redirect URIs). +1. Run your application, you will see the sign-in button. +2. Click the sign-in button, and you will be redirected to the sign in route, and the SDK will then init the sign-in process and redirect to the Logto sign-in page. +3. After you signed in, you will be redirect back to your application and see the sign-out button. +4. Calling `/logto/sign-out` to sign-out.