-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat: next auth guide #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
slug: /quick-starts/next-auth | ||
sidebar_label: Next Auth | ||
sidebar_custom_props: | ||
logoFilename: 'next-auth.svg' | ||
description: Authentication for Next.js. | ||
--- | ||
|
||
import FurtherReadings from '../../fragments/_further-readings.md'; | ||
|
||
import ConfigProvider from './_config-provider.mdx'; | ||
import GuideTip from './_guide-tip.mdx'; | ||
import ScopesAndClaims from './_scopes-and-claims.mdx'; | ||
|
||
# Next Auth guide | ||
|
||
This guide will show you how to integrate Logto into your Next.js application with [Next Auth](https://next-auth.js.org/). | ||
|
||
<GuideTip /> | ||
|
||
## Prerequisites | ||
|
||
- 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). | ||
- A Logto traditional application created. | ||
- A Next.js project with Next Auth, check out the [Next Auth documentation](https://next-auth.js.org/getting-started/introduction). | ||
|
||
## Integration | ||
|
||
### Config Next Auth provider | ||
|
||
<ConfigProvider /> | ||
|
||
### Checkpoint | ||
|
||
Now, you can test your application to see if the authentication works as expected. | ||
|
||
## Scopes and claims | ||
|
||
<ScopesAndClaims /> | ||
|
||
## Further readings | ||
|
||
<FurtherReadings /> |
63 changes: 63 additions & 0 deletions
63
docs/quick-starts/framework/next-auth/_config-provider.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import redirectUriFigure from '../../assets/next-auth-redirect-uri.png'; | ||
import ConfigureRedirectUri from '../../fragments/_configure-redirect-uri.mdx'; | ||
import GetAppSecret from '../../fragments/_get-app-secret.mdx'; | ||
import AssumingUrl from '../../fragments/_web-assuming-url.md'; | ||
import SignInFlowSummary from '../../fragments/_web-sign-in-flow-summary.mdx'; | ||
|
||
<SignInFlowSummary /> | ||
|
||
<AssumingUrl /> | ||
|
||
#### Configure sign-in redirect URI | ||
|
||
<ConfigureRedirectUri | ||
figureSrc={redirectUriFigure} | ||
redirectUri="http://localhost:3000/api/auth/callback/logto" | ||
/> | ||
|
||
#### Config Next Auth provider | ||
|
||
<GetAppSecret /> | ||
|
||
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`. | ||
|
||
The following is an example of App Router: | ||
|
||
```ts | ||
import NextAuth from 'next-auth'; | ||
|
||
const handler = NextAuth({ | ||
providers: [ | ||
{ | ||
id: 'logto', | ||
name: 'Logto', | ||
type: 'oauth', | ||
// You can get the well-known URL from the Logto Application Details page, | ||
// in the field "OpenID Provider configuration endpoint" | ||
wellKnown: 'https://xxxx.logto.app/oidc/.well-known/openid-configuration', | ||
authorization: { params: { scope: 'openid offline_access profile email' } }, | ||
clientId: '<logto-app-id>', | ||
clientSecret: '<logto-app-secret>', | ||
client: { | ||
id_token_signed_response_alg: 'ES384', | ||
}, | ||
profile(profile) { | ||
// You can customize the user profile mapping here | ||
return { | ||
id: profile.sub, | ||
name: profile.name ?? profile.username, | ||
email: profile.email, | ||
image: profile.picture, | ||
}; | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
export { handler as GET, handler as POST }; | ||
``` | ||
|
||
1. Replace the `wellKnown` URL with your Logto application's "OpenID Provider configuration endpoint". | ||
2. Replace the `clientId` and `clientSecret` with your Logto application's ID and secret. | ||
3. Customize the `profile` function to map the user profile to the Next Auth user object, the default mapping is shown in the example. | ||
4. Remember to set the `id_token_signed_response_alg` to `ES384`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:::tip | ||
|
||
- 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. | ||
|
||
::: |
13 changes: 13 additions & 0 deletions
13
docs/quick-starts/framework/next-auth/_scopes-and-claims-code.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```ts | ||
const handler = NextAuth({ | ||
providers: [ | ||
{ | ||
id: 'logto', | ||
name: 'Logto', | ||
// ... other options | ||
authorization: { params: { scope: 'openid offline_access profile email' } }, | ||
// ... other options | ||
}, | ||
], | ||
}); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import ScopesAndClaims from '../../fragments/_scopes-and-claims.mdx'; | ||
|
||
import ScopesAndClaimsCode from './_scopes-and-claims-code.md'; | ||
|
||
<ScopesAndClaims configScopesCode={<ScopesAndClaimsCode />} /> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.