Skip to content

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

rewrite server-side auth section to use Route Handlers, rather than Server Actions

Jon Meyers43c49e8dd119
diff --git a/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx b/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx
index dba2976c0a1..0aaafb1f237 100644
--- a/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx
+++ b/apps/docs/pages/guides/auth/auth-helpers/nextjs.mdx
@@ -373,14 +373,97 @@ TypeScript types can be [generated with the Supabase CLI](/docs/reference/javasc
### Server-side
-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.
+The combination of [Server Components](https://nextjs.org/docs/getting-started/react-essentials#server-components) and [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) can be used to trigger the authentication process from form submissions.
+
+#### Sign Up Route
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+>
+<TabPanel id="js" label="JavaScript">
+
+```jsx app/auth/sign-up.js
+import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
+import { cookies } from 'next/headers'
+import { NextResponse } from 'next/server'
+
+export const dynamic = 'force-dynamic'
+
+export async function POST(request) {
+ const requestUrl = new URL(request.url)
+ const formData = await request.formData()
+ const email = formData.get('email')
+ const password = formData.get('password')
+ const supabase = createRouteHandlerClient({ cookies })
+
+ await supabase.auth.signUp({
+ email,
+ password,
+ options: {
+ emailRedirectTo: `${requestUrl.origin}/auth/callback`,
+ },
+ })
+
+ return NextResponse.redirect(requestUrl.origin, {
+ status: 301,
+ })
+}
+```
+
+</TabPanel>
+
+<TabPanel id="ts" label="TypeScript">
+
+```tsx app/auth/sign-up.ts
+import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
+import { cookies } from 'next/headers'
+import { NextResponse } from 'next/server'
+
+import type { Database } from '@/lib/database.types'
+
+export const dynamic = 'force-dynamic'
+
+export async function POST(request: Request) {
+ const requestUrl = new URL(request.url)
+ const formData = await request.formData()
+ const email = String(formData.get('email'))
+ const password = String(formData.get('password'))
+ const supabase = createRouteHandlerClient<Database>({ cookies })
+
+ await supabase.auth.signUp({
+ email,
+ password,
+ options: {
+ emailRedirectTo: `${requestUrl.origin}/auth/callback`,
+ },
+ })
+
+ return NextResponse.redirect(requestUrl.origin, {
+ status: 301,
+ })
+}
+```
<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.
+TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
</Admonition>
+</TabPanel>
+</Tabs>
+
+<Admonition type="note">
+
+Returning a `301` status redirects from a POST to a GET route
+
+</Admonition>
+
+#### Login Route
+
<Tabs
scrollable
size="small"
@@ -389,61 +472,103 @@ Next.js [Server Actions](https://nextjs.org/docs/app/building-your-application/d
>
<TabPanel id="js" label="JavaScript">
-```jsx app/login.js
-import { createServerActionClient } from '@supabase/auth-helpers-nextjs'
-import { revalidatePath } from 'next/cache'
+```jsx app/auth/login.js
+import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
+import { NextResponse } from 'next/server'
export const dynamic = 'force-dynamic'
-export default async function Login() {
- const handleSignUp = async (formData) => {
- 'use server'
- const email = formData.get('email')
- const password = formData.get('password')
+export async function POST(request) {
+ const requestUrl = new URL(request.url)
+ const formData = await request.formData()
+ const email = formData.get('email')
+ const password = formData.get('password')
+ const supabase = createRouteHandlerClient({ cookies })
- const supabase = createServerActionClient({ cookies })
- await supabase.auth.signUp({
- email,
- password,
- options: {
- emailRedirectTo: 'http://localhost:3000/auth/callback',
- },
- })
+ await supabase.auth.signInWithPassword({
+ email,
+ password,
+ })
- revalidatePath('/')
- }
+ return NextResponse.redirect(requestUrl.origin, {
+ status: 301,
+ })
+}
+```
- const handleSignIn = async (formData) => {
- 'use server'
- const email = formData.get('email')
- const password = formData.get('password')
+</TabPanel>
- const supabase = createServerActionClient({ cookies })
- await supabase.auth.signInWithPassword({
- email,
- password,
- })
+<TabPanel id="ts" label="TypeScript">
- revalidatePath('/')
- }
+```tsx app/auth/login.ts
+import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
+import { cookies } from 'next/headers'
+import { NextResponse } from 'next/server'
- const handleSignOut = async () => {
- 'use server'
- const supabase = createServerActionClient({ cookies })
- await supabase.auth.signOut()
- revalidatePath('/')
- }
+import type { Database } from '@/lib/database.types'
- return (
- <form action={handleSignUp}>
- <input name="email" />
- <input type="password" name="password" />
- <button>Sign up</button>
- <button formAction={handleSignIn}>Sign in</button>
- <button formAction={handleSignOut}>Sign out</button>
- </form>
- )
+export const dynamic = 'force-dynamic'
+
+export async function POST(request: Request) {
+ const requestUrl = new URL(request.url)
+ const formData = await request.formData()
+ const email = String(formData.get('email'))
+ const password = String(formData.get('password'))
+ const supabase = createRouteHandlerClient<Database>({ cookies })
+
+ await supabase.auth.signInWithPassword({
+ email,
+ password,
+ })
+
+ return NextResponse.redirect(requestUrl.origin, {
+ status: 301,
+ })
+}
+```
+
+<Admonition type="note">
+
+TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
+
+</Admonition>
+
+</TabPanel>
+</Tabs>
+
+<Admonition type="note">
+
+Returning a `301` status redirects from a POST to a GET route
+
+</Admonition>
+
+#### Logout Route
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+>
+<TabPanel id="js" label="JavaScript">
+
+```jsx app/auth/logout.js
+import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
+import { cookies } from 'next/headers'
+import { NextResponse } from 'next/server'
+
+export const dynamic = 'force-dynamic'
+
+export async function POST(request) {
+ const requestUrl = new URL(request.url)
+ const supabase = createRouteHandlerClient({ cookies })
+
+ await supabase.auth.signOut()
+
+ return NextResponse.redirect(`${requestUrl.origin}/login`, {
+ status: 301,
+ })
}
```
@@ -451,71 +576,80 @@ export default async function Login() {
<TabPanel id="ts" label="TypeScript">
-```tsx app/login.ts
-import { createServerActionClient } from '@supabase/auth-helpers-nextjs'
-import { revalidatePath } from 'next/cache'
+```tsx app/auth/logout.ts
+import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
+import { NextResponse } from 'next/server'
import type { Database } from '@/lib/database.types'
export const dynamic = 'force-dynamic'
-export default async function Login() {
- const handleSignUp = async (formData: FormData) => {
- 'use server'
- const email = String(formData.get('email'))
- const password = String(formData.get('password'))
+export async function POST(request: Request) {
+ const requestUrl = new URL(request.url)
+ const supabase = createRouteHandlerClient<Database>({ cookies })
- const supabase = createServerActionClient<Database>({ cookies })
- await supabase.auth.signUp({
- email,
- password,
- options: {
- emailRedirectTo: 'http://localhost:3000/auth/callback',
- },
- })
+ await supabase.auth.signOut()
- revalidatePath('/')
- }
+ return NextResponse.redirect(`${requestUrl.origin}/login`, {
+ status: 301,
+ })
+}
+```
- const handleSignIn = async (formData: FormData) => {
- 'use server'
- const email = String(formData.get('email'))
- const password = String(formData.get('password'))
+<Admonition type="note">
- const supabase = createServerActionClient<Database>({ cookies })
- await supabase.auth.signInWithPassword({
- email,
- password,
- })
+TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
- revalidatePath('/')
- }
+</Admonition>
- const handleSignOut = async () => {
- 'use server'
- const supabase = createServerActionClient<Database>({ cookies })
- await supabase.auth.signOut()
- revalidatePath('/')
- }
+</TabPanel>
+</Tabs>
+
+#### Login Page
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+>
+<TabPanel id="js" label="JavaScript">
+```jsx app/login.jsx
+export default function Login() {
return (
- <form action={handleSignUp}>
+ <form action="/auth/login" method="post">
+ <label htmlFor="email">Email</label>
<input name="email" />
+ <label htmlFor="password">Password</label>
<input type="password" name="password" />
- <button>Sign up</button>
- <button formAction={handleSignIn}>Sign in</button>
- <button formAction={handleSignOut}>Sign out</button>
+ <button>Sign In</button>
+ <button formAction="/auth/sign-up">Sign Up</button>
+ <button formAction="/auth/logout">Sign Out</button>
</form>
)
}
```
-<Admonition type="note">
+</TabPanel>
-TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createServerActionClient` to add type support to the Supabase client.
+<TabPanel id="ts" label="TypeScript">
-</Admonition>
+```tsx app/login.tsx
+export default function Login() {
+ return (
+ <form action="/auth/login" method="post">
+ <label htmlFor="email">Email</label>
+ <input name="email" />
+ <label htmlFor="password">Password</label>
+ <input type="password" name="password" />
+ <button>Sign In</button>
+ <button formAction="/auth/sign-up">Sign Up</button>
+ </form>
+ )
+}
+```
</TabPanel>
</Tabs>
@@ -817,7 +951,7 @@ TypeScript types can be [generated with the Supabase CLI](/docs/reference/javasc
>
<TabPanel id="js" label="JavaScript">
-```jsx app/api/todos/route.jsx
+```jsx app/api/todos/route.js
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import { cookies } from 'next/headers'
@@ -836,7 +970,7 @@ export async function POST(request) {
<TabPanel id="ts" label="TypeScript">
-```tsx app/api/todos/route.tsx
+```tsx app/api/todos/route.ts
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import { cookies } from 'next/headers'
@@ -914,10 +1048,10 @@ import type { Database } from '@/lib/database.types'
export const runtime = 'edge'
export const dynamic = 'force-dynamic'
-export default async function ServerComponent<Database>() {
+export default async function Page() {
const cookieStore = cookies()
- const supabase = createServerComponentClient({
+ const supabase = createServerComponentClient<Database>({
cookies: () => cookieStore,
})
@@ -945,7 +1079,7 @@ TypeScript types can be [generated with the Supabase CLI](/docs/reference/javasc
>
<TabPanel id="js" label="JavaScript">
-```jsx app/api/todos/route.jsx
+```jsx app/api/todos/route.js
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import { cookies } from 'next/headers'
@@ -970,7 +1104,7 @@ export async function POST(request) {
<TabPanel id="ts" label="TypeScript">
-```tsx app/api/todos/route.tsx
+```tsx app/api/todos/route.ts
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import { cookies } from 'next/headers'
@@ -1052,7 +1186,7 @@ export default async function Page() {
<Admonition type="note">
-TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createServerComponentClient` to add type support to the Supabase client.
+TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createClient` to add type support to the Supabase client.
</Admonition>
@@ -1069,7 +1203,7 @@ TypeScript types can be [generated with the Supabase CLI](/docs/reference/javasc
>
<TabPanel id="js" label="JavaScript">
-```jsx app/api/todos/route.jsx
+```jsx app/api/todos/route.js
import { createClient } from '@supabase/supabase-js'
import { NextResponse } from 'next/server'
@@ -1090,7 +1224,7 @@ export async function POST(request) {
<TabPanel id="ts" label="TypeScript">
-```tsx app/api/todos/route.tsx
+```tsx app/api/todos/route.ts
import { createClient } from '@supabase/supabase-js'
import { NextResponse } from 'next/server'
@@ -1111,7 +1245,7 @@ export async function POST(request: Request) {
<Admonition type="note">
-TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createRouteHandlerClient` to add type support to the Supabase client.
+TypeScript types can be [generated with the Supabase CLI](/docs/reference/javascript/typescript-support) and passed to `createClient` to add type support to the Supabase client.
</Admonition>