Skip to content

Commit 1dcd90b

Browse files
authored
feat: next auth guide (#664)
1 parent 3a3dc80 commit 1dcd90b

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed
Loading
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
slug: /quick-starts/next-auth
3+
sidebar_label: Next Auth
4+
sidebar_custom_props:
5+
logoFilename: 'next-auth.svg'
6+
description: Authentication for Next.js.
7+
---
8+
9+
import FurtherReadings from '../../fragments/_further-readings.md';
10+
11+
import ConfigProvider from './_config-provider.mdx';
12+
import GuideTip from './_guide-tip.mdx';
13+
import ScopesAndClaims from './_scopes-and-claims.mdx';
14+
15+
# Next Auth guide
16+
17+
This guide will show you how to integrate Logto into your Next.js application with [Next Auth](https://next-auth.js.org/).
18+
19+
<GuideTip />
20+
21+
## Prerequisites
22+
23+
- A [Logto Cloud](https://cloud.logto.io) account or a self-hosted Logto (Check out the [⚡ Get started](../../../docs/tutorials/get-started/) guide to create one if you don't have).
24+
- A Logto traditional application created.
25+
- A Next.js project with Next Auth, check out the [Next Auth documentation](https://next-auth.js.org/getting-started/introduction).
26+
27+
## Integration
28+
29+
### Config Next Auth provider
30+
31+
<ConfigProvider />
32+
33+
### Checkpoint
34+
35+
Now, you can test your application to see if the authentication works as expected.
36+
37+
## Scopes and claims
38+
39+
<ScopesAndClaims />
40+
41+
## Further readings
42+
43+
<FurtherReadings />
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import redirectUriFigure from '../../assets/next-auth-redirect-uri.png';
2+
import ConfigureRedirectUri from '../../fragments/_configure-redirect-uri.mdx';
3+
import GetAppSecret from '../../fragments/_get-app-secret.mdx';
4+
import AssumingUrl from '../../fragments/_web-assuming-url.md';
5+
import SignInFlowSummary from '../../fragments/_web-sign-in-flow-summary.mdx';
6+
7+
<SignInFlowSummary />
8+
9+
<AssumingUrl />
10+
11+
#### Configure sign-in redirect URI
12+
13+
<ConfigureRedirectUri
14+
figureSrc={redirectUriFigure}
15+
redirectUri="http://localhost:3000/api/auth/callback/logto"
16+
/>
17+
18+
#### Config Next Auth provider
19+
20+
<GetAppSecret />
21+
22+
Modify your API route config of Next Auth, if you are using Pages Router, the file is in `pages/api/auth/[...nextauth].js`, if you are using App Router, the file is in `app/api/auth/[...nextauth]/router.ts`.
23+
24+
The following is an example of App Router:
25+
26+
```ts
27+
import NextAuth from 'next-auth';
28+
29+
const handler = NextAuth({
30+
providers: [
31+
{
32+
id: 'logto',
33+
name: 'Logto',
34+
type: 'oauth',
35+
// You can get the well-known URL from the Logto Application Details page,
36+
// in the field "OpenID Provider configuration endpoint"
37+
wellKnown: 'https://xxxx.logto.app/oidc/.well-known/openid-configuration',
38+
authorization: { params: { scope: 'openid offline_access profile email' } },
39+
clientId: '<logto-app-id>',
40+
clientSecret: '<logto-app-secret>',
41+
client: {
42+
id_token_signed_response_alg: 'ES384',
43+
},
44+
profile(profile) {
45+
// You can customize the user profile mapping here
46+
return {
47+
id: profile.sub,
48+
name: profile.name ?? profile.username,
49+
email: profile.email,
50+
image: profile.picture,
51+
};
52+
},
53+
},
54+
],
55+
});
56+
57+
export { handler as GET, handler as POST };
58+
```
59+
60+
1. Replace the `wellKnown` URL with your Logto application's "OpenID Provider configuration endpoint".
61+
2. Replace the `clientId` and `clientSecret` with your Logto application's ID and secret.
62+
3. Customize the `profile` function to map the user profile to the Next Auth user object, the default mapping is shown in the example.
63+
4. Remember to set the `id_token_signed_response_alg` to `ES384`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:::tip
2+
3+
- In this guide, we assume you have set up Next Auth in your Next.js project. If you haven't, check out the [Next Auth documentation](https://next-auth.js.org/getting-started/introduction) to get started.
4+
5+
:::
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
```ts
2+
const handler = NextAuth({
3+
providers: [
4+
{
5+
id: 'logto',
6+
name: 'Logto',
7+
// ... other options
8+
authorization: { params: { scope: 'openid offline_access profile email' } },
9+
// ... other options
10+
},
11+
],
12+
});
13+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ScopesAndClaims from '../../fragments/_scopes-and-claims.mdx';
2+
3+
import ScopesAndClaimsCode from './_scopes-and-claims-code.md';
4+
5+
<ScopesAndClaims configScopesCode={<ScopesAndClaimsCode />} />

static/img/logo/next-auth.svg

Lines changed: 25 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)