Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_label: 'Migrating to SSR from Auth Helpers'

The new `ssr` package takes the core concepts of the Auth Helpers and makes them available to any server language or framework. This page will guide you through migrating from the Auth Helpers package to `ssr`.

### Replacing Supabase packages
## Replacing Supabase packages

<Tabs scrollable size="small" type="underlined" defaultActiveId="nextjs" queryGroup="framework">

Expand Down Expand Up @@ -37,11 +37,11 @@ npm uninstall @supabase/auth-helpers-remix
npm install @supabase/ssr
```

### Creating a client
## Creating a client

The new `ssr` package exports two functions for creating a Supabase client. The `createBrowserClient` function is used in the client, and the `createServerClient` function is used in the server.

Check out the [Creating a client](/docs/guides/auth/server-side/creating-a-client) page for examples of creating a client in your framework.
Read the [Creating a client](/docs/guides/auth/server-side/creating-a-client) page for examples of creating a client in your framework [and our migration guide](/docs/guides/troubleshooting/how-to-migrate-from-supabase-auth-helpers-to-ssr-package-5NRunM).

## Next steps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ keywords = [ "migration", "auth", "ssr", "package" ]
database_id = "273b79ad-b8ff-4669-8544-b99920e2d3c8"
---

The `auth-helpers` packages are being deprecated and replaced with the `@supabase/ssr` package. We recommend migrating to the `@supabase/ssr` package as future bug fixes and feature releases are focused on the `@supabase/ssr` package.
The `auth-helpers` packages are deprecated and replaced with the `@supabase/ssr` package. We recommend migrating to the `@supabase/ssr` package as future bug fixes and feature releases are focused on the `@supabase/ssr` package.

Here are the steps for you to migrate your application from the `auth-helpers` package to `@supabase/ssr` package.

Depending on your implementation, you may ignore some parts of this documentation and use your own implementation (i.e. using API routes vs. Server Actions). Whats important is you replace the clients provided by `auth-helpers` with the utility functions created using clients provided by `@supabase/ssr`.
Depending on your implementation, you may ignore some parts of this documentation and use your own implementation (i.e. using API routes vs. Server Actions). What's important is you replace the clients provided by `auth-helpers` with the utility functions created using clients provided by `@supabase/ssr`.

### 1. Uninstall Supabase Auth helpers and install the Supabase SSR package

Its important that you dont use both `auth-helpers-nextjs` and `@supabase/ssr` packages in the same application to avoid running into authentication issues.
It's important that you don't use both `auth-helpers-nextjs` and `@supabase/ssr` packages in the same application to avoid running into authentication issues.

```
npm uninstall @supabase/auth-helpers-nextjs @supabase/supabase-js
npm install @supabase/ssr @supabase/supabase-js
```

### 2. Create the utility functions to create Supabase clients
### 2. Create the library functions to create Supabase clients

```
// utils/supabase/client.ts
// lib/supabase/client.ts

import { createBrowserClient } from '@supabase/ssr';

Expand All @@ -36,12 +36,12 @@ export function createClient() {
);
}

// utils/supabase/server.ts
import { createServerClient, type CookieOptions } from '@supabase/ssr';
import { cookies } from 'next/headers';
// lib/supabase/server.ts
import { createServerClient } from '@supabase/ssr'
import { cookies } from 'next/headers'

export function createClient() {
const cookieStore = cookies();
export async function createClient() {
const cookieStore = await cookies()

return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
Expand All @@ -53,29 +53,29 @@ export function createClient() {
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options)
)
cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options))
} catch {
// The `setAll` method was called from a Server Component.
// This can be ignored if you have proxy refreshing
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
},
}
);
)
}

// utils/supabase/proxy.ts
import { createServerClient } from '@supabase/ssr';
import { NextResponse, type NextRequest } from 'next/server';
// lib/supabase/proxy.ts
import { createServerClient } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server'

export async function updateSession(request: NextRequest) {
let supabaseResponse = NextResponse.next({
request,
});
})

// With Fluid compute, don't put this client in a global environment
// variable. Always create a new one on each request.
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
Expand All @@ -85,35 +85,35 @@ export async function updateSession(request: NextRequest) {
return request.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => request.cookies.set(name, value))
cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value))
supabaseResponse = NextResponse.next({
request,
})
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options)
)
cookiesToSet.forEach(({ name, value, options }) => supabaseResponse.cookies.set(name, value, options))
},
},
}
);
)

// IMPORTANT: Avoid writing any logic between createServerClient and
// supabase.auth.getUser(). A simple mistake could make it very hard to debug
// Do not run code between createServerClient and
// supabase.auth.getClaims(). A simple mistake could make it very hard to debug
// issues with users being randomly logged out.

const {
data: { user },
} = await supabase.auth.getUser();
// IMPORTANT: If you remove getClaims() and you use server-side rendering
// with the Supabase client, your users may be randomly logged out.
const { data } = await supabase.auth.getClaims()

const user = data?.claims

if (
!user &&
!request.nextUrl.pathname.startsWith('/login') &&
!request.nextUrl.pathname.startsWith('/auth')
) {
// no user, potentially respond by redirecting the user to the login page
const url = request.nextUrl.clone();
url.pathname = '/login';
return NextResponse.redirect(url);
const url = request.nextUrl.clone()
url.pathname = '/login'
return NextResponse.redirect(url)
}

// IMPORTANT: You *must* return the supabaseResponse object as it is. If you're
Expand All @@ -129,7 +129,7 @@ export async function updateSession(request: NextRequest) {
// If this is not done, you may be causing the browser and server to go out
// of sync and terminate the user's session prematurely!

return supabaseResponse;
return supabaseResponse
}
```

Expand All @@ -138,11 +138,11 @@ export async function updateSession(request: NextRequest) {
```
// proxy.ts

import { type NextRequest } from 'next/server';
import { updateSession } from '@/utils/supabase/proxy';
import { type NextRequest } from "next/server"
import { updateSession } from "@/lib/supabase/proxy"

export async function proxy(request: NextRequest) {
return await updateSession(request);
return await updateSession(request)
}

export const config = {
Expand All @@ -154,9 +154,9 @@ export const config = {
* - favicon.ico (favicon file)
* Feel free to modify this pattern to include more paths.
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};
}
```

### 4. Create your server actions to handle login and sign up
Expand Down Expand Up @@ -309,7 +309,7 @@ Likewise, you can replace the clients created with `@supabase/auth-helpers-nextj
`createServerComponentClient` → `createServerClient`
`createRouteHandlerClient` → `createServerClient`

You can find more clear and concise examples of creating clients in our documentation [here](/docs/guides/auth/server-side/creating-a-client?queryGroups=framework&framework=nextjs&queryGroups=environment&environment=route-handler#creating-a-client).
You can find more clear and concise examples of creating clients [in our SSR documentation](/docs/guides/auth/server-side/creating-a-client?queryGroups=framework&framework=nextjs&queryGroups=environment&environment=route-handler#creating-a-client).

If you have any feedback about this guide, provide them as a comment below. If you find any issues or have feedback for the `@supabase/ssr` client, post them as an issue in `@supabase/ssr` repo.

Expand Down
Loading
Loading