You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/developer-spotlight/tutorials/fullstack-authentication-with-next-js-and-cloudflare-d1.mdx
+56-16Lines changed: 56 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
updated: 2024-11-10
2
+
updated: 2025-01-13
3
3
difficulty: Intermediate
4
4
content_type: 📝 Tutorial
5
5
pcx_content_type: tutorial
@@ -37,8 +37,8 @@ You can find the finished code for this project on [GitHub](https://github.com/m
37
37
38
38
<Renderfile="prereqs"product="workers" />
39
39
40
-
- Create a [Resend account](https://resend.com/signup)with a [verified domain](https://resend.com/docs/dashboard/domains/introduction)and get an [API key](https://resend.com/docs/api-reference/api-keys/create-api-key).
41
-
-[Install and authenticate Wrangler](/workers/wrangler/install-and-update/).
40
+
3. Create or login to a [Resend account](https://resend.com/signup) and get an [API key](https://resend.com/docs/dashboard/api-keys/introduction#add-api-key).
41
+
4.[Install and authenticate Wrangler](/workers/wrangler/install-and-update/).
42
42
43
43
## 1. Create a Next.js app using Workers
44
44
@@ -59,19 +59,29 @@ From within the repository or directory where you want to create your project ru
59
59
}}
60
60
/>
61
61
62
-
This will create a new Next.js project using [OpenNext](https://opennext.js.org/cloudflare) that will run in a Worker using [Workers Assets](/workers/frameworks/framework-guides/nextjs/).
62
+
This will create a new Next.js project using [OpenNext](https://opennext.js.org/cloudflare) that will run in a Worker using [Workers Static Assets](/workers/frameworks/framework-guides/nextjs/#static-assets).
63
+
64
+
Before we get started, open your project's `tsconfig.json` file and add the following to the `compilerOptions` object to allow for top level await needed to let our application get the Cloudflare context:
65
+
66
+
```json title="tsconfig.json"
67
+
{
68
+
"compilerOptions": {
69
+
"target": "ES2022",
70
+
}
71
+
}
72
+
```
63
73
64
74
Throughout this tutorial, we'll add several values to Cloudflare Secrets. For [local development](/workers/configuration/secrets/#local-development-with-secrets), add those same values to a file in the top level of your project called `.dev.vars` and make sure it is not committed into version control. This will let you work with Secret values locally. Go ahead and copy and paste the following into `.dev.vars` for now and replace the values as we go.
Within the Workers environment, the `AUTH_URL` doesn't get picked up automatically by Auth.js, hence why we're specifying it manually here (we'll need to do the same for prod later).
84
+
Within the Workers environment, the `AUTH_URL` doesn't always get picked up automatically by Auth.js, hence why we're specifying it manually here (we'll need to do the same for prod later).
75
85
:::
76
86
77
87
## 2. Install Auth.js
@@ -86,12 +96,20 @@ Now run the following to generate an `AUTH_SECRET`:
86
96
npx auth secret
87
97
```
88
98
89
-
Now, deviating from the standard Auth.js setup, locate your generated secret (likely in a file named `.env.local`) and [add the secret to your Workers application](/workers/configuration/secrets/#adding-secrets-to-your-project) by running the following and completing the steps to add a secret's value:
99
+
Now, deviating from the standard Auth.js setup, locate your generated secret (likely in a file named `.env.local`) and [add the secret to your Workers application](/workers/configuration/secrets/#adding-secrets-to-your-project) by running the following and completing the steps to add a secret's value that we just generated:
90
100
91
101
```sh
92
102
npx wrangler secret put AUTH_SECRET
93
103
```
94
104
105
+
After adding the secret, update your `.dev.vars` file to include an `AUTH_SECRET` value (this secret should be different from the one you generated earlier for security purposes):
106
+
107
+
```sh title=".dev.vars"
108
+
# ...
109
+
AUTH_SECRET = "<replace-me>"
110
+
# ...
111
+
```
112
+
95
113
Next, go into the newly generated `env.d.ts` file and add the following to the <Typetext="CloudflareEnv" /> interface:
96
114
97
115
```ts title="env.d.ts"
@@ -132,21 +150,37 @@ interface CloudflareEnv {
132
150
133
151
## 4. Configure Credentials Provider
134
152
135
-
Auth.js provides integrations for many different [credential providers](https://authjs.dev/getting-started/authentication) such as Google, GitHub, etc. For this tutorial we're going to use [Resend for magic links](https://authjs.dev/getting-started/authentication/email). You should have already created a Resend account and have an API key.
153
+
Auth.js provides integrations for many different [credential providers](https://authjs.dev/getting-started/authentication) such as Google, GitHub, etc. For this tutorial we're going to use [Resend for magic links](https://authjs.dev/getting-started/authentication/email). You should have already created a Resend account and have an [API key](https://resend.com/docs/dashboard/api-keys/introduction#add-api-key).
136
154
137
-
With a domain [created and verified](https://resend.com/docs/dashboard/domains/introduction)within Resend, add a new Secret to your Worker containing an email from a verified domain such as `[email protected]`.
155
+
Using either a [Resend verified domain email address](https://resend.com/docs/dashboard/domains/introduction)or `[email protected]`, add a new Secret to your Worker containing the email your magic links will come from:
138
156
139
157
```sh title="Add Resend email to secrets"
140
158
npx wrangler secret put AUTH_EMAIL_FROM
141
159
```
142
160
161
+
Next, ensure the `AUTH_EMAIL_FROM` environment variable is updated in your `.dev.vars` file with the email you just added as a secret:
@@ -228,7 +262,7 @@ export async function GET(request: NextRequest) {
228
262
You'll need to run this once on your production database to create the necessary tables. If you're following along with this tutorial, we'll run it together in a few steps.
229
263
230
264
:::note[Clean up]
231
-
Running this multiple times won't hurt anything since the tables are only created if they do not already exist, but it's a good idea to remove this route from your production code once you've run it.
265
+
Running this multiple times won't hurt anything since the tables are only created if they do not already exist, but it's a good idea to remove this route from your production code once you've run it since you won't need it anymore.
232
266
:::
233
267
234
268
Before we go further, make sure you've created all of the necessary files:
@@ -249,8 +283,10 @@ Before we go further, make sure you've created all of the necessary files:
249
283
</FileTree>
250
284
251
285
## 6. Build Sign-in Interface
252
-
We've completed the backend steps for our application. Now, we need a way to sign in. First, let's install shadcn:
We've completed the backend steps for our application. Now, we need a way to sign in. First, let's install [shadcn](https://ui.shadcn.com/):
287
+
```sh title="Install shadcn"
288
+
npx shadcn@latest init -d
289
+
```
254
290
255
291
Next, run the following to add a few components:
256
292
```sh title="Add components"
@@ -377,11 +413,15 @@ Now, it's time to preview our app. Run the following to preview your application
377
413
args={"preview"}
378
414
/>
379
415
416
+
:::caution[Windows support]
417
+
OpenNext has [limited Windows support](https://opennext.js.org/cloudflare#windows-support) and recommends using WSL2 if developing on Windows.
418
+
:::
419
+
380
420
You should see our login form. But wait, we're not done yet. Remember to create your database tables by visiting `/api/setup`. You should see `Migration completed`. This means your database is ready to go.
381
421
382
-
Navigate back to your application's homepage. Enter your email and sign in. You should receive an email in your inbox (check spam). Follow the link to sign in. If everything is configured correctly, you should now see a basic user profile letting your update your name and sign out.
422
+
Navigate back to your application's homepage. Enter your email and sign in (use the same email as your Resend account if you used the `[email protected]` address). You should receive an email in your inbox (check spam). Follow the link to sign in. If everything is configured correctly, you should now see a basic user profile letting your update your name and sign out.
383
423
384
-
Now let's take our application to production. From within the project's directory run:
424
+
Now let's deploy our application to production. From within the project's directory run:
0 commit comments