Skip to content

Commit 3011e60

Browse files
committed
docs: add guide for NextAuth
1 parent f831f2a commit 3011e60

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
id: auth-js
3+
title: Integrate authentication into Auth.js and NextAuth
4+
sidebar_label: Auth.js and NextAuth
5+
---
6+
7+
This guide covers integrating Ory with Auth.js, a popular authentication library for Next.js applications. Auth.js is a flexible
8+
authentication library that supports multiple authentication providers, including Ory Network out of the box.
9+
10+
Auth.js is the successor to NextAuth.js, which is a popular authentication library for Next.js applications. The Ory provider
11+
works with both Auth.js and NextAuth.js, so you can use either library to integrate Ory with your Next.js application.
12+
13+
To connect your Next.js application with Ory we:
14+
15+
1. Clone an example Next.js application with Auth.js
16+
2. Create an OAuth2 client in Ory and configure Auth.js to use it
17+
3. Perform a demo flow to see the integration in action
18+
19+
Let's get started!
20+
21+
## Clone the example Next.js application
22+
23+
First, clone the example Next.js application with Auth.js:
24+
25+
```shell-session
26+
git clone https://github.com/nextauthjs/next-auth-example.git
27+
cd next-auth-example
28+
npm i
29+
cp .env.local.example .env.local
30+
npx auth secret
31+
```
32+
33+
In the `auth.ts` file, replace the `profiders` array with just Ory:
34+
35+
```ts title="auth.ts"
36+
import { OryNetworkCta } from './ory-network-cta'
37+
export const config = {
38+
theme: { logo: "https://authjs.dev/img/logo-sm.png" },
39+
providers: [
40+
// Apple,
41+
// AzureB2C,
42+
Ory
43+
]
44+
// ...
45+
}
46+
```
47+
48+
## Create OAuth2 Client in Ory Network
49+
50+
To create the OAuth2 client, you need to know the redirect URL of your Next.js application. The redirect URL is the URL where Ory
51+
will send the user after they log in. When running NextJS locally, the redirect URL for Auth.js is usually
52+
`http://localhost:3000/auth/callback/ory`.
53+
54+
1. Log into your Ory Network account.
55+
2. create a new project, or select an existing one.
56+
3. Go to ["OAuth 2" -> "Clients"](https://console.ory.sh/projects/current/oauth) and click
57+
["Create OAuth 2.0 Client"](https://console.ory.sh/projects/current/oauth/create).
58+
4. Select "Server App".
59+
5. Choose a client name, e.g. "NextAuth Example".
60+
6. Scopes `openid` and `offline_access` are preselected. Add `email` and `profile` to the list.
61+
7. Add the redirect URL, for example `http://localhost:3000/api/auth/callback/ory`.
62+
8. Scroll to the bottom and click "Create Client".
63+
9. Copy the Client Secret and click "Done" and save it in your `.env.local` file.
64+
10. Copy the Client ID from the client list and save it in your `.env.local` file.
65+
66+
## Configure Auth.js to use Ory
67+
68+
Your `.env.local` file should now look like this:
69+
70+
```env
71+
# Needed by Auth.js
72+
AUTH_SECRET=...
73+
74+
# Needed for Ory
75+
AUTH_ORY_ID=...
76+
AUTH_ORY_SECRET=...
77+
```
78+
79+
Next, add the Ory Issuer URL to your `.env.local` file. The Issuer URL is the "Ory Network Project API Endpoint" you can find
80+
[here](https://console.ory.sh/projects/current/developers/guides). Unless you have a custom domain set up for your Ory Network
81+
project, it will be in the form of:
82+
83+
```
84+
https://{project.slug}.projects.oryapis.com
85+
```
86+
87+
Your final `.env.local` file should look like this:
88+
89+
```env
90+
# Needed by Auth.js
91+
AUTH_SECRET=...
92+
93+
# Needed for Ory
94+
AUTH_ORY_ID=...
95+
AUTH_ORY_SECRET=...
96+
AUTH_ORY_ISSUER=...
97+
```
98+
99+
## Test the flow
100+
101+
Now everything is set up and you can run `npm run dev` to start the app and click on the top left "Sign in" button to start the
102+
login flow.
103+
104+
## Going to production
105+
106+
When you are ready to go to production, you need to update the redirect URL in the Ory client settings to the production URL of
107+
your Next.js application.
108+
109+
## Trouble Shooting
110+
111+
### Incorrect `redirect_uri`
112+
113+
If the server responds with
114+
115+
```
116+
The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.
117+
The 'redirect_uri' parameter does not match any of the OAuth 2.0 Client's pre-registered redirect urls.
118+
```
119+
120+
please ensure that the redirect URL is correct. You can find the redirect URL you are sending to Ory by checking the network tab
121+
of your browser and look for calls to `/oauth2/auth`.

0 commit comments

Comments
 (0)