Skip to content

Commit ae727a4

Browse files
docs: update supabase docs for nextjs 16 proxyts change (supabase#40555)
* Quickstart next 16 update * Fix paths and env vars * docs: refactor nextjs server-side auth to use Proxy instead of middleware * docs: refactor nextjs server-side auth to match proxy * docs: refactor nextjs example to match Proxy * docs: refactor nextjs auth AI prompt to match Proxy * docs: refactor nextjs sentry telemetry integration to match Proxy * examples: update nextjs realtime example to match middleware * docs: refactoring guides to use nextjs proxy * examples: update nextjs-full example to match Next16 template * example: update nextjs-user-management to match nextjs 16 * docs: refactoring nextjs user-management tutorial to use typescript only * docs: refactoring nextjs quickstart, removing step 4 since this step is already included on `with-supabase` template, we can just remove this redundant step * docs: auth-helpers nextjs pages, Nextjs16 proxy disclaimer * stamp: lint * stamp: revert 'NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY' * stamp: nextjs examples, revert to use cookie options * fix(docs): typo * docs: updating nextjs-auth troubleshoot guide to match proxy * Update apps/docs/content/guides/getting-started/quickstarts/nextjs.mdx * Revert auth-helpers changes * Revert auth-helpers content * Apply suggestions from code review * Update apps/docs/content/troubleshooting/how-do-you-troubleshoot-nextjs---supabase-auth-issues-riMCZV.mdx * Update apps/docs/content/troubleshooting/how-do-you-troubleshoot-nextjs---supabase-auth-issues-riMCZV.mdx * Update apps/docs/content/troubleshooting/how-do-you-troubleshoot-nextjs---supabase-auth-issues-riMCZV.mdx * Update apps/docs/content/troubleshooting/how-do-you-troubleshoot-nextjs---supabase-auth-issues-riMCZV.mdx * Apply suggestions from code review * Prettier --------- Co-authored-by: kallebysantos <[email protected]>
1 parent 2f3e3b8 commit ae727a4

File tree

41 files changed

+1965
-2594
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1965
-2594
lines changed

apps/docs/content/guides/auth/quickstarts/nextjs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ hideToc: true
7474
7575
Start the development server, go to http://localhost:3000 in a browser, and you should see the contents of `app/page.tsx`.
7676
77-
To sign up a new user, navigate to http://localhost:3000/sign-up, and click `Sign up`. *NOTE: .env.example must be renamed to .env.local before this route becomes available*
77+
To sign up a new user, navigate to http://localhost:3000/auth/sign-up, and click `Sign up`.
7878
7979
</StepHikeCompact.Details>
8080

apps/docs/content/guides/auth/server-side/creating-a-client.mdx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ To access Supabase from a Next.js app, you need 2 types of Supabase clients:
164164
1. **Client Component client** - To access Supabase from Client Components, which run in the browser.
165165
2. **Server Component client** - To access Supabase from Server Components, Server Actions, and Route Handlers, which run only on the server.
166166

167-
Since Next.js Server Components can't write cookies, you need middleware to refresh expired Auth tokens and store them.
167+
Since Next.js Server Components can't write cookies, you need a [Proxy](https://nextjs.org/docs/app/getting-started/proxy) to refresh expired Auth tokens and store them.
168168

169-
The middleware is responsible for:
169+
The Proxy is responsible for:
170170

171171
1. Refreshing the Auth token by calling `supabase.auth.getClaims()`.
172172
2. Passing the refreshed Auth token to Server Components, so they don't attempt to refresh the same token themselves. This is accomplished with `request.cookies.set`.
@@ -181,7 +181,7 @@ The middleware is responsible for:
181181

182182
The cookies object lets the Supabase client know how to access the cookies, so it can read and write the user session data. To make `@supabase/ssr` framework-agnostic, the cookies methods aren't hard-coded. These utility functions adapt `@supabase/ssr`'s cookie handling for Next.js.
183183

184-
The `set` and `remove` methods for the server client need error handlers, because Next.js throws an error if cookies are set from Server Components. You can safely ignore this error because you'll set up middleware in the next step to write refreshed cookies to storage.
184+
The `set` and `remove` methods for the server client need error handlers, because Next.js throws an error if cookies are set from Server Components. You can safely ignore this error because you'll set up Proxy in the next step to write refreshed cookies to storage.
185185

186186
The cookie is named `sb-<project_ref>-auth-token` by default.
187187

@@ -201,49 +201,45 @@ The middleware is responsible for:
201201

202202
</Accordion>
203203

204-
Create a `utils/supabase` folder at the root of your project, or inside the `./src` folder if you are using one, with a file for each type of client. Then copy the utility functions for each client type.
204+
Create a `lib/supabase` folder at the root of your project, or inside the `./src` folder if you are using one, with a file for each type of client. Then copy the lib utility functions for each client type.
205205

206206
<div className="mt-12">
207207
<$CodeTabs>
208208
<$CodeSample
209-
path="/auth/nextjs/utils/supabase/client.ts"
210-
meta="name=utils/supabase/client.ts"
209+
path="/auth/nextjs/lib/supabase/client.ts"
210+
meta="name=lib/supabase/client.ts"
211211
language="typescript"
212212
/>
213213
<$CodeSample
214-
path="/auth/nextjs/utils/supabase/server.ts"
215-
meta="name=utils/supabase/server.ts"
214+
path="/auth/nextjs/lib/supabase/server.ts"
215+
meta="name=lib/supabase/server.ts"
216216
language="typescript"
217217
/>
218218
</$CodeTabs>
219219
</div>
220220

221-
### Hook up middleware
221+
### Hook up proxy
222222

223-
The code adds a [matcher](https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths) so the middleware doesn't run on routes that don't access Supabase.
223+
The code adds a [matcher](https://nextjs.org/docs/app/api-reference/file-conventions/proxy#matcher) so the Proxy doesn't run on routes that don't access Supabase.
224224

225225
<Admonition type="danger">
226226

227227
Be careful when protecting pages. The server gets the user session from the cookies, which can be spoofed by anyone.
228228

229229
Always use `supabase.auth.getClaims()` to protect pages and user data.
230230

231-
_Never_ trust `supabase.auth.getSession()` inside server code such as middleware. It isn't guaranteed to revalidate the Auth token.
231+
_Never_ trust `supabase.auth.getSession()` inside server code such as Proxy. It isn't guaranteed to revalidate the Auth token.
232232

233233
It's safe to trust `getClaims()` because it validates the JWT signature against the project's published public keys every time.
234234

235235
</Admonition>
236236

237237
<div className="mt-12">
238238
<$CodeTabs>
239+
<$CodeSample path="/auth/nextjs/proxy.ts" meta="name=proxy.ts" language="typescript" />
239240
<$CodeSample
240-
path="/auth/nextjs/middleware.ts"
241-
meta="name=middleware.ts"
242-
language="typescript"
243-
/>
244-
<$CodeSample
245-
path="/auth/nextjs/utils/supabase/middleware.ts"
246-
meta="name=utils/supabase/middleware.ts"
241+
path="/auth/nextjs/lib/supabase/proxy.ts"
242+
meta="name=lib/supabase/proxy.ts"
247243
language="typescript"
248244
/>
249245
</$CodeTabs>
@@ -256,7 +252,7 @@ You're done! To recap, you've successfully:
256252
- Called Supabase from a Server Action.
257253
- Called Supabase from a Server Component.
258254
- Set up a Supabase client utility to call Supabase from a Client Component. You can use this if you need to call Supabase from a Client Component, for example to set up a realtime subscription.
259-
- Set up middleware to automatically refresh the Supabase Auth session.
255+
- Set up Proxy to automatically refresh the Supabase Auth session.
260256

261257
You can now use any Supabase features from your client or server code!
262258

apps/docs/content/guides/getting-started/quickstarts/nextjs.mdx

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -62,58 +62,7 @@ hideToc: true
6262

6363
</StepHikeCompact.Step>
6464

65-
<StepHikeCompact.Step step={4}>
66-
<StepHikeCompact.Details title="Create Supabase client">
67-
68-
Create a new file at `utils/supabase/server.ts` and populate with the following.
69-
70-
This creates a Supabase client, using the credentials from the `env.local` file.
71-
72-
</StepHikeCompact.Details>
73-
74-
<StepHikeCompact.Code>
75-
76-
<$CodeTabs>
77-
78-
```ts name=utils/supabase/server.ts
79-
import { createServerClient } from '@supabase/ssr'
80-
import { cookies } from 'next/headers'
81-
82-
export async function createClient() {
83-
const cookieStore = await cookies()
84-
85-
return createServerClient(
86-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
87-
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
88-
{
89-
cookies: {
90-
getAll() {
91-
return cookieStore.getAll()
92-
},
93-
setAll(cookiesToSet) {
94-
try {
95-
cookiesToSet.forEach(({ name, value, options }) =>
96-
cookieStore.set(name, value, options)
97-
)
98-
} catch {
99-
// The `setAll` method was called from a Server Component.
100-
// This can be ignored if you have middleware refreshing
101-
// user sessions.
102-
}
103-
},
104-
},
105-
}
106-
)
107-
}
108-
```
109-
110-
</$CodeTabs>
111-
112-
</StepHikeCompact.Code>
113-
114-
</StepHikeCompact.Step>
115-
116-
<StepHikeCompact.Step step={5}>
65+
<StepHikeCompact.Step step={4}>
11766
<StepHikeCompact.Details title="Query Supabase data from Next.js">
11867

11968
Create a new file at `app/instruments/page.tsx` and populate with the following.
@@ -127,13 +76,22 @@ hideToc: true
12776
<$CodeTabs>
12877

12978
```ts name=app/instruments/page.tsx
130-
import { createClient } from '@/utils/supabase/server';
79+
import { createClient } from "@/lib/supabase/server";
80+
import { Suspense } from "react";
13181

132-
export default async function Instruments() {
82+
async function InstrumentsData() {
13383
const supabase = await createClient();
13484
const { data: instruments } = await supabase.from("instruments").select();
13585

136-
return <pre>{JSON.stringify(instruments, null, 2)}</pre>
86+
return <pre>{JSON.stringify(instruments, null, 2)}</pre>;
87+
}
88+
89+
export default function Instruments() {
90+
return (
91+
<Suspense fallback={<div>Loading instruments...</div>}>
92+
<InstrumentsData />
93+
</Suspense>
94+
);
13795
}
13896
```
13997

@@ -143,7 +101,7 @@ hideToc: true
143101

144102
</StepHikeCompact.Step>
145103

146-
<StepHikeCompact.Step step={6}>
104+
<StepHikeCompact.Step step={5}>
147105
<StepHikeCompact.Details title="Start the app">
148106

149107
Run the development server, go to http://localhost:3000/instruments in a browser and you should see the list of instruments.

0 commit comments

Comments
 (0)