mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
refactor(console): update express guide (#5314)
This commit is contained in:
parent
a70397b125
commit
9f91da075b
1 changed files with 24 additions and 31 deletions
|
@ -9,8 +9,8 @@ import Step from '@/mdx-components/Step';
|
|||
<Steps>
|
||||
|
||||
<Step
|
||||
title="Install Logto SDK"
|
||||
subtitle="Please select your favorite package manager"
|
||||
title="Installation"
|
||||
subtitle="Install Logto SDK for your project"
|
||||
>
|
||||
|
||||
<Tabs>
|
||||
|
@ -40,7 +40,6 @@ pnpm add @logto/express cookie-parser express-session
|
|||
|
||||
<Step
|
||||
title="Init LogtoClient"
|
||||
subtitle="1 step"
|
||||
>
|
||||
|
||||
<InlineNotification>
|
||||
|
@ -84,8 +83,7 @@ app.use(session({ secret: '${generateStandardSecret()}', cookie: { maxAge: 14 *
|
|||
</Step>
|
||||
|
||||
<Step
|
||||
title="Sign in"
|
||||
subtitle="3 steps"
|
||||
title="Implement sign in"
|
||||
>
|
||||
|
||||
### Configure Redirect URI
|
||||
|
@ -126,34 +124,28 @@ app.get('/', (req, res) => {
|
|||
</Step>
|
||||
|
||||
<Step
|
||||
title="Get user profile"
|
||||
subtitle="2 steps"
|
||||
title="Implement sign out"
|
||||
>
|
||||
|
||||
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).
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Handle authentication status"
|
||||
>
|
||||
|
||||
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);
|
||||
});
|
||||
```
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Protect routes"
|
||||
subtitle="2 steps"
|
||||
>
|
||||
|
||||
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');
|
||||
});
|
||||
```
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Sign out"
|
||||
subtitle="1 step"
|
||||
title="Checkpoint: Test your application"
|
||||
>
|
||||
|
||||
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.
|
||||
|
||||
</Step>
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue