Auth

Setting up Server-Side Auth for Next.js


Next.js comes in two flavors: the App Router and the Pages Router. You can set up Server-Side Auth with either strategy. You can even use both in the same application.

1

Install Supabase packages

Install the @supabase/supabase-js package and the helper @supabase/ssr package.


_10
npm install @supabase/supabase-js @supabase/ssr

2

Set up environment variables

Create a .env.local file in your project root directory.

Fill in your NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY:

Project URL
Anon key
.env.local

_10
NEXT_PUBLIC_SUPABASE_URL=<your_supabase_project_url>
_10
NEXT_PUBLIC_SUPABASE_ANON_KEY=<your_supabase_anon_key>

3

Write utility functions to create Supabase clients

To access Supabase from your Next.js app, you need 2 types of Supabase clients:

  1. Client Component client - To access Supabase from Client Components, which run in the browser.
  2. Server Component client - To access Supabase from Server Components, Server Actions, and Route Handlers, which run only on the server.

Create a utils/supabase folder with a file for each type of client. Then copy the utility functions for each client type.

utils/supabase/client.ts
utils/supabase/server.ts

_10
import { createBrowserClient } from '@supabase/ssr'
_10
_10
export function createClient() {
_10
return createBrowserClient(
_10
process.env.NEXT_PUBLIC_SUPABASE_URL!,
_10
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
_10
)
_10
}

4

Hook up middleware

Create a middleware.ts file at the root of your project.

Since Server Components can't write cookies, you need middleware to refresh expired Auth tokens and store them.

The middleware is responsible for:

  1. Refreshing the Auth token (by calling supabase.auth.getUser).
  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.
  3. Passing the refreshed Auth token to the browser, so it replaces the old token. This is accomplished with response.cookies.set.

Copy the middleware code for your app.

Add a matcher so the middleware doesn't run on routes that don't access Supabase.

middleware.ts
utils/supabase/middleware.ts

_19
import { type NextRequest } from 'next/server'
_19
import { updateSession } from '@/utils/supabase/middleware'
_19
_19
export async function middleware(request: NextRequest) {
_19
return await updateSession(request)
_19
}
_19
_19
export const config = {
_19
matcher: [
_19
/*
_19
* Match all request paths except for the ones starting with:
_19
* - _next/static (static files)
_19
* - _next/image (image optimization files)
_19
* - favicon.ico (favicon file)
_19
* Feel free to modify this pattern to include more paths.
_19
*/
_19
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
_19
],
_19
}

5

Create a login page

Create a login page for your app. Use a Server Action to call the Supabase signup function.

Since Supabase is being called from an Action, use the client defined in @/utils/supabase/server.ts.

app/login/page.tsx
app/login/actions.ts
app/error/page.tsx

_14
import { login, signup } from './actions'
_14
_14
export default function LoginPage() {
_14
return (
_14
<form>
_14
<label htmlFor="email">Email:</label>
_14
<input id="email" name="email" type="email" required />
_14
<label htmlFor="password">Password:</label>
_14
<input id="password" name="password" type="password" required />
_14
<button formAction={login}>Log in</button>
_14
<button formAction={signup}>Sign up</button>
_14
</form>
_14
)
_14
}

6

Change the Auth confirmation path

If you have email confirmation turned on (the default), a new user will receive an email confirmation after signing up.

Change the email template to support a server-side authentication flow.

Go to the Auth templates page in your dashboard. In the Confirm signup template, change {{ .ConfirmationURL }} to {{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=signup.

7

Create a route handler for Auth confirmation

Create a Route Handler for auth/confirm. When a user clicks their confirmation email link, exchange their secure code for an Auth token.

Since this is a Router Handler, use the Supabase client from @/utils/supabase/server.ts.

app/auth/confirm/route.ts

_28
import { type EmailOtpType } from '@supabase/supabase-js'
_28
import { type NextRequest } from 'next/server'
_28
_28
import { createClient } from '@/utils/supabase/server'
_28
import { redirect } from 'next/navigation'
_28
_28
export async function GET(request: NextRequest) {
_28
const { searchParams } = new URL(request.url)
_28
const token_hash = searchParams.get('token_hash')
_28
const type = searchParams.get('type') as EmailOtpType | null
_28
const next = searchParams.get('next') ?? '/'
_28
_28
if (token_hash && type) {
_28
const supabase = await createClient()
_28
_28
const { error } = await supabase.auth.verifyOtp({
_28
type,
_28
token_hash,
_28
})
_28
if (!error) {
_28
// redirect user to specified redirect URL or root of app
_28
redirect(next)
_28
}
_28
}
_28
_28
// redirect the user to an error page with some instructions
_28
redirect('/error')
_28
}

8

Access user info from Server Component

Server Components can read cookies, so you can get the Auth status and user info.

Since you're calling Supabase from a Server Component, use the client created in @/utils/supabase/server.ts.

Create a private page that users can only access if they're logged in. The page displays their email.

app/private/page.tsx

_14
import { redirect } from 'next/navigation'
_14
_14
import { createClient } from '@/utils/supabase/server'
_14
_14
export default async function PrivatePage() {
_14
const supabase = await createClient()
_14
_14
const { data, error } = await supabase.auth.getUser()
_14
if (error || !data?.user) {
_14
redirect('/login')
_14
}
_14
_14
return <p>Hello {data.user.email}</p>
_14
}

Congratulations

You're done! To recap, you've successfully:

  • Called Supabase from a Server Action.
  • Called Supabase from a Server Component.
  • 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.
  • Set up middleware to automatically refresh the Supabase Auth session.

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