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
docs: Update user management docs for next JS (supabase#37664)
* Draft
* Update text for guide
* Update text for guide
* Update code for newer next js versions
* Final small change
* Update apps/docs/content/_partials/project_setup.mdx
Co-authored-by: Charis <[email protected]>
* Changes from review
* Use project name in tutorial text
* Fix up inclusion
* Fix inclusion
* Prettier
---------
Co-authored-by: Charis <[email protected]>
Copy file name to clipboardExpand all lines: apps/docs/content/_partials/project_setup.mdx
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,15 +22,13 @@ Now set up the database schema. You can use the "User Management Starter" quicks
22
22
<TabPanelid="dashboard"label="Dashboard">
23
23
24
24
1. Go to the [SQL Editor](https://supabase.com/dashboard/project/_/sql) page in the Dashboard.
25
-
2. Click **User Management Starter**.
25
+
2. Click **User Management Starter** under the **Community > Quickstarts** tab.
26
26
3. Click **Run**.
27
27
28
28
<Admonitiontype="note">
29
29
30
30
You can pull the database schema down to your local project by running the `db pull` command. Read the [local development docs](/docs/guides/cli/local-development#link-your-project) for detailed instructions.
31
31
32
-
{/* TODO: discuss */}
33
-
34
32
```bash
35
33
supabase link --project-ref <project-id>
36
34
# You can get <project-id> from your project's dashboard URL: https://supabase.com/dashboard/project/<project-id>
Copy file name to clipboardExpand all lines: apps/docs/content/_partials/quickstart_intro.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,4 +2,4 @@ This tutorial demonstrates how to build a basic user management app. The app aut
2
2
3
3
-[Supabase Database](/docs/guides/database) - a Postgres database for storing your user data and [Row Level Security](/docs/guides/auth#row-level-security) so data is protected and users can only access their own information.
4
4
-[Supabase Auth](/docs/guides/auth) - allow users to sign up and log in.
5
-
-[Supabase Storage](/docs/guides/storage) - users can upload a profile photo.
5
+
-[Supabase Storage](/docs/guides/storage) - allow users to upload a profile photo.
Copy file name to clipboardExpand all lines: apps/docs/content/guides/getting-started/tutorials/with-nextjs.mdx
+34-39Lines changed: 34 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,11 +17,11 @@ If you get stuck while working through this guide, refer to the [full example on
17
17
18
18
## Building the app
19
19
20
-
Let's start building the Next.js app from scratch.
20
+
Start building the Next.js app from scratch.
21
21
22
22
### Initialize a Next.js app
23
23
24
-
We can use[`create-next-app`](https://nextjs.org/docs/getting-started) to initialize an app called `supabase-nextjs`:
24
+
Use[`create-next-app`](https://nextjs.org/docs/getting-started) to initialize an app called `supabase-nextjs`:
25
25
26
26
<Tabs
27
27
scrollable
@@ -54,8 +54,7 @@ Then install the Supabase client library: [supabase-js](https://github.com/supab
54
54
npm install @supabase/supabase-js
55
55
```
56
56
57
-
And finally we want to save the environment variables in a `.env.local`.
58
-
Create a `.env.local` file at the root of the project, and paste the API URL and the `anon` key that you copied [earlier](#get-the-api-keys).
57
+
Save the environment variables in a `.env.local` file at the root of the project, and paste the API URL and the `anon` key that you copied [earlier](#get-the-api-keys).
An optional step is to update the CSS file `app/globals.css` to make the app look nice.
68
-
You can find the full contents of this file [here](https://raw.githubusercontent.com/supabase/supabase/master/examples/user-management/nextjs-user-management/app/globals.css).
67
+
You can find the full contents of this file [in the example repository](https://raw.githubusercontent.com/supabase/supabase/master/examples/user-management/nextjs-user-management/app/globals.css).
69
68
70
69
### Supabase Server-Side Auth
71
70
72
71
Next.js is a highly versatile framework offering pre-rendering at build time (SSG), server-side rendering at request time (SSR), API routes, and middleware edge-functions.
73
72
74
-
To better integrate with the framework, we've created the `@supabase/ssr` package for Server-Side Auth. It has all the functionalities to quickly configure your Supabase project to use cookies for storing user sessions. See the [Next.js Server-Side Auth guide](/docs/guides/auth/server-side/nextjs) for more information.
73
+
To better integrate with the framework, we've created the `@supabase/ssr` package for Server-Side Auth. It has all the functionalities to quickly configure your Supabase project to use cookies for storing user sessions. Read the [Next.js Server-Side Auth guide](/docs/guides/auth/server-side/nextjs) for more information.
75
74
76
75
Install the package for Next.js.
77
76
@@ -184,11 +183,11 @@ Since Server Components can't write cookies, you need middleware to refresh expi
184
183
- Passing the refreshed Auth token to Server Components through `request.cookies.set`, so they don't attempt to refresh the same token themselves.
185
184
- Passing the refreshed Auth token to the browser, so it replaces the old token. This is done with `response.cookies.set`.
186
185
187
-
You could also add a matcher, so that the middleware only runs on route that access Supabase. For more information, check out this [documentation](https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths).
186
+
You could also add a matcher, so that the middleware only runs on routes that access Supabase. For more information, read [the Next.js matcher documentation](https://nextjs.org/docs/app/api-reference/file-conventions/middleware#matcher).
188
187
189
188
<Admonitiontype="danger">
190
189
191
-
Be careful when protecting pages. The server gets the user session from the cookies, which can be spoofed by anyone.
190
+
Be careful when protecting pages. The server gets the user session from the cookies, which anyone can spoof.
192
191
193
192
Always use `supabase.auth.getUser()` to protect pages and user data.
Navigate to `http://localhost:3000/login`. You should see your login form, but it's not yet hooked up to the actual login function. Next, you need to create the login/signup actions. They will:
360
+
Next, you need to create the login/signup actions to hook up the form to the function. Which does the following:
362
361
363
362
- Retrieve the user's information.
364
-
- Send that information to Supabase as a signup request, which in turns will send a confirmation email.
363
+
- Send that information to Supabase as a signup request, which in turns sends a confirmation email.
365
364
- Handle any error that arises.
366
365
367
366
<Admonitiontype="caution">
368
367
369
-
Note that cookiesis called before any calls to Supabase, which opts fetch calls out of Next.js's caching. This is important for authenticated data fetches, to ensure that users get access only to their own data.
368
+
The `cookies` method is called before any calls to Supabase, which takes fetch calls out of Next.js's caching. This is important for authenticated data fetches, to ensure that users get access only to their own data.
370
369
371
-
See the Next.js docs to learn more about [opting out of data caching](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching).
370
+
Read the Next.js docs to learn more about [opting out of data caching](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching).
372
371
373
372
</Admonition>
374
373
@@ -382,6 +381,8 @@ See the Next.js docs to learn more about [opting out of data caching](https://ne
382
381
383
382
<TabPanelid="js"label="JavaScript">
384
383
384
+
Create the `action.js` file in the `app/login` folder, which contains the login and signup functions and the `error/page.jsx` file, and displays an error message if the login or signup fails.
385
+
385
386
<$CodeTabs>
386
387
387
388
```js name=app/login/actions.js
@@ -409,7 +410,6 @@ export async function login(formData) {
409
410
}
410
411
411
412
revalidatePath('/', 'layout')
412
-
redirect('/account')
413
413
}
414
414
415
415
exportasyncfunctionsignup(formData) {
@@ -427,7 +427,6 @@ export async function signup(formData) {
427
427
}
428
428
429
429
revalidatePath('/', 'layout')
430
-
redirect('/account')
431
430
}
432
431
```
433
432
@@ -443,6 +442,8 @@ export default function ErrorPage() {
443
442
444
443
<TabPanelid="ts"label="TypeScript">
445
444
445
+
Create the `action.ts` file in the `app/login` folder, which contains the login and signup functions and the `error/page.tsx` file, which displays an error message if the login or signup fails.
Did you know? You could also customize emails sent out to new users, including the email's looks, content, and query parameters. Check out the [settings of your project](/dashboard/project/_/auth/templates).
477
+
**Did you know?** You can also customize other emails sent out to new users, including the email's looks, content, and query parameters. Check out the [settings of your project](/dashboard/project/_/auth/templates).
481
478
482
479
</Admonition>
483
480
484
481
### Confirmation endpoint
485
482
486
-
As we are working in a server-side rendering (SSR) environment, it is necessary to create a server endpoint responsible for exchanging the `token_hash` for a session.
483
+
As you are working in a server-side rendering (SSR) environment, you need to create a server endpoint responsible for exchanging the `token_hash` for a session.
487
484
488
-
In the following code snippet, we perform the following steps:
485
+
The code performs the following steps:
489
486
490
-
-Retrieve the code sent back from the Supabase Auth server using the `token_hash` query parameter.
491
-
-Exchange this code for a session, which we store in our chosen storage mechanism (in this case, cookies).
492
-
- Finally, we redirect the user to the `account` page.
487
+
-Retrieves the code sent back from the Supabase Auth server using the `token_hash` query parameter.
488
+
-Exchanges this code for a session, which you store in your chosen storage mechanism (in this case, cookies).
489
+
- Finally, redirects the user to the `account` page.
493
490
494
491
<Tabs
495
492
scrollable
@@ -505,7 +502,6 @@ In the following code snippet, we perform the following steps:
0 commit comments