Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs(nextjs-auth): various improvements

Greg Richardson8918471091b1
diff --git a/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx b/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx
index bcb2a36db31..7a01bf1ac1e 100644
--- a/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx
+++ b/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx
@@ -54,18 +54,18 @@ yarn add @supabase/auth-helpers-nextjs
### Declare Environment Variables
-Retrieve your project's URL and anon key from your [API settings](https://app.supabase.com/project/_/settings/api), and create a `.env.local` file with the following environment variables.
-
-Make sure you get the names exactly right as these are picked up by the auth helpers library — you no longer have to explicitly pass them to a `createClient()` function as before:
+Retrieve your project's URL and anon key from your [API settings](https://app.supabase.com/project/_/settings/api), and create a `.env.local` file with the following environment variables:
```bash .env.local
NEXT_PUBLIC_SUPABASE_URL=your-supabase-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-supabase-anon-key
```
+Make sure you name these variables exactly as seen here, since these are picked up by the auth helpers library — you no longer have to explicitly pass them to a `createClient()` function as before.
+
### Refresh session with Middleware
-[Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) runs immediately before each route in rendered. Next.js only provides read access to cookies in Server Components, therefore, Middleware is used to refresh the user's session before loading Server Component routes.
+[Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) runs immediately before each route is rendered. Next.js only provides read access to cookies in Server Components, therefore, Middleware is used to refresh the user's session before loading Server Component routes.
<Tabs
scrollable
@@ -77,7 +77,7 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=your-supabase-anon-key
Create a new `middleware.js` file in the root of your project and populate with the following:
-```jsx middleware.js
+```js middleware.js
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
@@ -95,7 +95,7 @@ export async function middleware(req) {
Create a new `middleware.ts` file in the root of your project and populate with the following:
-```tsx middleware.ts
+```ts middleware.ts
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
@@ -110,12 +110,20 @@ export async function middleware(req: NextRequest) {
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createMiddlewareClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createMiddlewareClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>
-> The `getSession` function must be called for any Server Component routes that use a Supabase client.
+<Admonition type="note">
+
+The `getSession` function must be called for any Server Component routes that use a Supabase client.
+
+</Admonition>
### Code Exchange Route
@@ -131,7 +139,7 @@ The `Code Exchange` route is required for the [server-side auth flow](https://su
Create a new file at `app/auth/callback/route.js` and populate with the following:
-```jsx app/auth/callback/route.js
+```js app/auth/callback/route.js
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'
@@ -156,7 +164,7 @@ export async function GET(request) {
Create a new file at `app/auth/callback/route.ts` and populate with the following:
-```tsx app/auth/callback/route.ts
+```ts app/auth/callback/route.ts
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'
@@ -178,7 +186,11 @@ export async function GET(request: NextRequest) {
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>
@@ -196,7 +208,11 @@ export async function GET(request: NextRequest) {
Authentication can be initiated [client](/docs/guides/auth/auth-helpers/nextjs#client-side) or [server-side](/docs/guides/auth/auth-helpers/nextjs#server-side). All of the [supabase-js authentication strategies](/docs/reference/javascript/auth-api) are supported with the Auth Helpers client.
-> Note: The authentication flow requires the [Code Exchange Route](/docs/guides/auth/auth-helpers/nextjs#code-exchange-route) to exchange a `code` for the user's `session`.
+<Admonition type="note">
+
+The authentication flow requires the [Code Exchange Route](/docs/guides/auth/auth-helpers/nextjs#code-exchange-route) to exchange a `code` for the user's `session`.
+
+</Admonition>
### Client-side
@@ -210,7 +226,7 @@ Client Components can be used to trigger the authentication process from event h
>
<TabPanel id="js" label="JavaScript">
-```jsx app/login.js
+```jsx app/login.jsx
'use client'
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
@@ -268,7 +284,7 @@ export default function Login() {
<TabPanel id="ts" label="TypeScript">
-```tsx app/login.ts
+```tsx app/login.tsx
'use client'
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
@@ -324,7 +340,11 @@ export default function Login() {
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createClientComponentClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createClientComponentClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>
@@ -333,7 +353,11 @@ export default function Login() {
The combination of [Server Components](https://nextjs.org/docs/getting-started/react-essentials#server-components) and [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions) can be used to trigger the authentication process from form submissions.
-> Note: [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions) are currently in Alpha and likely to change. We recommend [triggering the authentication flow client-side](/docs/guides/auth/auth-helpers/nextjs#client-side) for production applications.
+<Admonition type="note">
+
+Next.js [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions) are currently in Alpha and may change. For now we recommend [triggering the authentication flow client-side](/docs/guides/auth/auth-helpers/nextjs#client-side) for production applications.
+
+</Admonition>
<Tabs
scrollable
@@ -461,7 +485,11 @@ export default async function Login() {
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerActionClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerActionClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>
@@ -526,47 +554,51 @@ export default function Home() {
<TabPanel id="ts" label="TypeScript">
-```jsx app/new-post.tsx
-"use client";
+```tsx app/client/page.tsx
+'use client'
-import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
-import { useEffect, useState } from "react";
+import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
+import { useEffect, useState } from 'react'
import type { Database } from '@/lib/database.types'
-type Todo = Database["public"]["Tables"]["todos"]["Row"];
+type Todo = Database['public']['Tables']['todos']['Row']
export default function Home() {
- const [todos, setTodos] = useState<Todo[] | null>(null);
- const supabase = createClientComponentClient<Database>();
+ const [todos, setTodos] = useState<Todo[] | null>(null)
+ const supabase = createClientComponentClient<Database>()
useEffect(() => {
const getData = async () => {
- const { data } = await supabase.from("todos").select();
- setTodos(data);
- };
-
- getData();
- }, []);
-
- return todos ? (
- <pre>{JSON.stringify(todos, null, 2)}</pre>
- ) : (
- <p>Loading todos...</p>
- );
+ const { data } = await supabase.from('todos').select()
+ setTodos(data)
+ }
+
+ getData()
+ }, [])
+
+ return todos ? <pre>{JSON.stringify(todos, null, 2)}</pre> : <p>Loading todos...</p>
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createClientComponentClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createClientComponentClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>
-> check out [this repo](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs) for more examples, including [realtime subscriptions](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs/app/realtime-posts.tsx).
+<Admonition type="info">
+
+Check out the [Next.js auth example repo](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs) for more examples, including [realtime subscriptions](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs/app/realtime-posts.tsx).
+
+</Admonition>
#### Singleton
-The `createClientComponentClient` function implements a [Singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern) to simplify instantiating Supabase clients. If you need multiple Supabase instances across Client Components - for example, when using multiple schemas - you can pass an additional configuration option for `{ isSingleton: false }` to get a new client every time this function is called.
+The `createClientComponentClient` function implements a [Singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern) by default, meaning that all invocations will return the same Supabase client instance. If you need multiple Supabase instances across Client Components, you can pass an additional configuration option `{ isSingleton: false }` to get a new client every time this function is called.
```jsx
const supabase = createClientComponentClient({ isSingleton: false })
@@ -585,7 +617,11 @@ const supabase = createClientComponentClient({ isSingleton: false })
[Server Components](https://nextjs.org/docs/getting-started/react-essentials#server-components) allow for asynchronous data to be fetched server-side.
-> Note: In order to use Supabase in Server Components, you need to have implemented the [Middleware](/docs/guides/auth/auth-helpers/nextjs#refresh-session-with-middleware) steps above.
+<Admonition type="note">
+
+In order to use Supabase in Server Components, you need to have implemented the [Middleware](/docs/guides/auth/auth-helpers/nextjs#refresh-session-with-middleware) steps above.
+
+</Admonition>
<Tabs
scrollable
@@ -623,12 +659,20 @@ export default async function ServerComponent() {
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerComponentClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerComponentClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>
-> check out [this repo](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs) for more examples, including redirecting unauthenticated users - [protected pages](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs/app/[id]/page.tsx).
+<Admonition type="info">
+
+Check out the [Next.js auth example repo](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs) for more examples, including redirecting unauthenticated users - [protected pages](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs/app/[id]/page.tsx).
+
+</Admonition>
### Server Actions
@@ -643,7 +687,11 @@ export default async function ServerComponent() {
[Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions) allow mutations to be performed server-side.
-> Note: Server Actions are currently in `alpha` so may change without notice.
+<Admonition type="note">
+
+Next.js Server Actions are currently in `alpha` so may change without notice.
+
+</Admonition>
<Tabs
scrollable
@@ -705,7 +753,11 @@ export default async function NewTodo() {
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerActionClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerActionClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>
@@ -763,7 +815,11 @@ export async function POST(request: Request) {
}
```
-> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
+
+</Admonition>
</TabPanel>
</Tabs>