Skip to content

Commit ad0c1e4

Browse files
committed
Edits and fixes to resolve issues
1 parent ed3c648 commit ad0c1e4

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

src/content/docs/developer-spotlight/tutorials/fullstack-authentication-with-next-js-and-cloudflare-d1.mdx

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
updated: 2024-11-10
2+
updated: 2025-01-13
33
difficulty: Intermediate
44
content_type: 📝 Tutorial
55
pcx_content_type: tutorial
@@ -37,8 +37,8 @@ You can find the finished code for this project on [GitHub](https://github.com/m
3737

3838
<Render file="prereqs" product="workers" />
3939

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/).
4242

4343
## 1. Create a Next.js app using Workers
4444

@@ -59,19 +59,29 @@ From within the repository or directory where you want to create your project ru
5959
}}
6060
/>
6161

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+
```
6373

6474
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.
6575

6676
```sh title=".dev.vars"
6777
AUTH_SECRET = "<replace-me>"
6878
AUTH_RESEND_KEY = "<replace-me>"
69-
AUTH_EMAIL_FROM = "<replace-me>"
79+
AUTH_EMAIL_FROM = "[email protected]"
7080
AUTH_URL = "http://localhost:8787/"
7181
```
7282

7383
:::note[Manually set URL]
74-
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).
7585
:::
7686

7787
## 2. Install Auth.js
@@ -86,12 +96,20 @@ Now run the following to generate an `AUTH_SECRET`:
8696
npx auth secret
8797
```
8898

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:
90100

91101
```sh
92102
npx wrangler secret put AUTH_SECRET
93103
```
94104

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+
95113
Next, go into the newly generated `env.d.ts` file and add the following to the <Type text="CloudflareEnv" /> interface:
96114

97115
```ts title="env.d.ts"
@@ -132,21 +150,37 @@ interface CloudflareEnv {
132150

133151
## 4. Configure Credentials Provider
134152

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).
136154

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:
138156

139157
```sh title="Add Resend email to secrets"
140158
npx wrangler secret put AUTH_EMAIL_FROM
141159
```
142160

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:
162+
163+
```sh title=".dev.vars"
164+
# ...
165+
AUTH_EMAIL_FROM = "[email protected]"
166+
# ...
167+
```
168+
143169
Now [create a Resend API key](https://resend.com/docs/dashboard/api-keys/introduction) with `Sending access` and add it to your Worker's Secrets:
144170

145171
```sh title="Add Resend API key to secrets"
146172
npx wrangler secret put AUTH_RESEND_KEY
147173
```
148174

149-
After adding both of those Secrets, your `env.d.ts` should include the following:
175+
As with previous secrets, update your `.dev.vars` file with the new secret value for `AUTH_RESEND_KEY` to use in local development:
176+
177+
```sh title=".dev.vars"
178+
# ...
179+
AUTH_RESEND_KEY = "<replace-me>"
180+
# ...
181+
```
182+
183+
After adding both of those Secrets, your `env.d.ts` should now include the following:
150184

151185
```ts title="env.d.ts"
152186
interface CloudflareEnv {
@@ -185,7 +219,7 @@ export const { handlers, signIn, signOut, auth } = await authResult();
185219

186220
Now, lets add the route handler and middleware used to authenticate and persist sessions.
187221

188-
Create a new directory structure and route handler within `src/app/api/auth/[...nextauth]` as `route.ts`. The file should contain:
222+
Create a new directory structure and route handler within `src/app/api/auth/[...nextauth]` called `route.ts`. The file should contain:
189223

190224
<TypeScriptExample filename="src/app/api/auth/[...nextauth]/route.ts">
191225
```ts
@@ -228,7 +262,7 @@ export async function GET(request: NextRequest) {
228262
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.
229263

230264
:::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.
232266
:::
233267

234268
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:
249283
</FileTree>
250284

251285
## 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:
253-
<PackageManagers pkg="shadcn@latest" args={"init -d"} />
286+
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+
```
254290

255291
Next, run the following to add a few components:
256292
```sh title="Add components"
@@ -377,11 +413,15 @@ Now, it's time to preview our app. Run the following to preview your application
377413
args={"preview"}
378414
/>
379415

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+
380420
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.
381421

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.
383423

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:
385425

386426
<PackageManagers
387427
type="run"

0 commit comments

Comments
 (0)