Supabase Auth with Next.js
This submodule provides convenience helpers for implementing user authentication in Next.js applications.
Install the Next.js helper library#
npm install @supabase/auth-helpers-nextjs
This library supports the following tooling versions:
- Node.js:
^10.13.0 || >=12.0.0
- Next.js:
>=10
Note: Next.js 13 is stable, however, the new
app
directory and Server Components are still in beta. Check out our experimental guide on using Auth Helpers with Next.js Server Components.
Additionally, install the React Auth Helpers for components and hooks that can be used across all React-based frameworks.
npm install @supabase/auth-helpers-react
Set up environment variables#
Retrieve your project URL and anon key in your project's API settings in the Dashboard to set up the following environment variables. For local development you can set them in a .env.local
file. See an example.
1NEXT_PUBLIC_SUPABASE_URL=YOUR_SUPABASE_URL 2NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
Basic Setup#
Wrap your pages/_app.js
component with the SessionContextProvider
component:
import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'
import { SessionContextProvider } from '@supabase/auth-helpers-react'
import { useState } from 'react'
function MyApp({ Component, pageProps }) {
// Create a new supabase browser client on every first render.
const [supabaseClient] = useState(() => createBrowserSupabaseClient())
return (
<SessionContextProvider
supabaseClient={supabaseClient}
initialSession={pageProps.initialSession}
>
<Component {...pageProps} />
</SessionContextProvider>
)
}
You can now determine if a user is authenticated by checking that the user
object returned by the useUser()
hook is defined.
Usage with TypeScript#
You can pass types that were generated with the Supabase CLI to the Supabase Client to get enhanced type safety and auto completion:
Browser client#
Creating a new supabase client object:
import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs' import { Database } from '../database.types' const supabaseClient = createBrowserSupabaseClient<Database>()
Retrieving a supabase client object from the SessionContext:
import { useSupabaseClient } from '@supabase/auth-helpers-react' import { Database } from '../database.types' const supabaseClient = useSupabaseClient<Database>()
Server client#
// Creating a new supabase server client object (e.g. in API route):
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
import type { NextApiRequest, NextApiResponse } from 'next'
import type { Database } from 'types_db'
export default async (req: NextApiRequest, res: NextApiResponse) => {
const supabaseServerClient = createServerSupabaseClient<Database>({
req,
res,
})
const {
data: { user },
} = await supabaseServerClient.auth.getUser()
res.status(200).json({ name: user?.name ?? '' })
}
Client-side data fetching with RLS#
For row level security to work properly when fetching data client-side, you need to make sure to use the supabaseClient
from the useSupabaseClient
hook and only run your query once the user is defined client-side in the useUser()
hook:
import { Auth } from '@supabase/auth-ui-react'
import { ThemeSupa } from '@supabase/auth-ui-shared'
import { useUser, useSupabaseClient } from '@supabase/auth-helpers-react'
import { useEffect, useState } from 'react'
const LoginPage = () => {
const supabaseClient = useSupabaseClient()
const user = useUser()
const [data, setData] = useState()
useEffect(() => {
async function loadData() {
const { data } = await supabaseClient.from('test').select('*')
setData(data)
}
// Only run query once user is logged in.
if (user) loadData()
}, [user])
if (!user)
return (
<Auth
redirectTo="http://localhost:3000/"
appearance={{ theme: ThemeSupa }}
supabaseClient={supabaseClient}
providers={['google', 'github']}
socialLayout="horizontal"
/>
)
return (
<>
<button onClick={() => supabaseClient.auth.signOut()}>Sign out</button>
<p>user:</p>
<pre>{JSON.stringify(user, null, 2)}</pre>
<p>client-side data fetching with RLS</p>
<pre>{JSON.stringify(data, null, 2)}</pre>
</>
)
}
export default LoginPage
Server-side rendering (SSR)#
Create a server supabase client to retrieve the logged in user's session:
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
export default function Profile({ user }) {
return <div>Hello {user.name}</div>
}
export const getServerSideProps = async (ctx) => {
// Create authenticated Supabase Client
const supabase = createServerSupabaseClient(ctx)
// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession()
if (!session)
return {
redirect: {
destination: '/',
permanent: false,
},
}
return {
props: {
initialSession: session,
user: session.user,
},
}
}
Server-side data fetching with RLS#
You can use the server supabase client to run row level security authenticated queries server-side:
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
export default function ProtectedPage({ user, data }) {
return (
<>
<div>Protected content for {user.email}</div>
<pre>{JSON.stringify(data, null, 2)}</pre>
<pre>{JSON.stringify(user, null, 2)}</pre>
</>
)
}
export const getServerSideProps = async (ctx) => {
// Create authenticated Supabase Client
const supabase = createServerSupabaseClient(ctx)
// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession()
if (!session)
return {
redirect: {
destination: '/',
permanent: false,
},
}
// Run queries with RLS on the server
const { data } = await supabase.from('users').select('*')
return {
props: {
initialSession: session,
user: session.user,
data: data ?? [],
},
}
}
Server-side data fetching to OAuth APIs using provider token
#oauth-provider-token#
When using third-party auth providers, sessions are initiated with an additional provider_token
field which is persisted in the auth cookie and can be accessed within the session object. The provider_token
can be used to make API requests to the OAuth provider's API endpoints on behalf of the logged-in user.
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
export default function ProtectedPage({ user, allRepos }) {
return (
<>
<div>Protected content for {user.email}</div>
<p>Data fetched with provider token:</p>
<pre>{JSON.stringify(allRepos, null, 2)}</pre>
<p>user:</p>
<pre>{JSON.stringify(user, null, 2)}</pre>
</>
)
}
export const getServerSideProps = async (ctx) => {
// Create authenticated Supabase Client
const supabase = createServerSupabaseClient(ctx)
// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession()
if (!session)
return {
redirect: {
destination: '/',
permanent: false,
},
}
// Retrieve provider_token & logged in user's third-party id from metadata
const { provider_token, user } = session
const userId = user.user_metadata.user_name
const allRepos = await (
await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
method: 'GET',
headers: {
Authorization: `token ${provider_token}`,
},
})
).json()
return { props: { user, allRepos } }
}
Protecting API routes#
Create a server supabase client to retrieve the logged in user's session:
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
const ProtectedRoute = async (req, res) => {
// Create authenticated Supabase Client
const supabase = createServerSupabaseClient({ req, res })
// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession()
if (!session)
return res.status(401).json({
error: 'not_authenticated',
description: 'The user does not have an active session or is not authenticated',
})
// Run queries with RLS on the server
const { data } = await supabase.from('test').select('*')
res.json(data)
}
export default ProtectedRoute
Auth with Next.js Middleware#
As an alternative to protecting individual pages you can use a Next.js Middleware to protect the entire directory or those that match the config object. In the following example, all requests to /middleware-protected/*
will check whether a user is signed in, if successful the request will be forwarded to the destination route, otherwise the user will be redirected:
import { createMiddlewareSupabaseClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(req: NextRequest) {
// We need to create a response and hand it to the supabase client to be able to modify the response headers.
const res = NextResponse.next()
// Create authenticated Supabase Client.
const supabase = createMiddlewareSupabaseClient({ req, res })
// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession()
// Check auth condition
if (session?.user.email?.endsWith('@gmail.com')) {
// Authentication successful, forward request to protected route.
return res
}
// Auth condition not met, redirect to home page.
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
export const config = {
matcher: '/middleware-protected/:path*',
}
Migration Guide#
Migrating to v0.5.X#
To make these helpers more flexible as well as more maintainable and easier to upgrade for new versions of Next.js, we're stripping them down to the most useful part which is managing the cookies and giving you an authenticated supabase-js client in any environment (client, server, middleware/edge).
Therefore we're marking the withApiAuth
, withPageAuth
, and withMiddlewareAuth
higher order functions as deprecated and they will be removed in the next minor release (v0.6.X).
Please follow the steps below to update your API routes, pages, and middleware handlers. Thanks!
withApiAuth
deprecated!
Use createServerSupabaseClient
within your NextApiHandler
:
import { withApiAuth } from '@supabase/auth-helpers-nextjs'
export default withApiAuth(async function ProtectedRoute(req, res, supabase) {
// Run queries with RLS on the server
const { data } = await supabase.from('test').select('*')
res.json(data)
})
withPageAuth
deprecated!
Use createServerSupabaseClient
within getServerSideProps
:
import { withPageAuth, User } from '@supabase/auth-helpers-nextjs'
export default function Profile({ user }: { user: User }) {
return <pre>{JSON.stringify(user, null, 2)}</pre>
}
export const getServerSideProps = withPageAuth({ redirectTo: '/' })
withMiddlewareAuth
deprecated!
import { withMiddlewareAuth } from '@supabase/auth-helpers-nextjs' export const middleware = withMiddlewareAuth({ redirectTo: '/', authGuard: { isPermitted: async (user) => { return user.email?.endsWith('@gmail.com') ?? false }, redirectTo: '/insufficient-permissions', }, }) export const config = { matcher: '/middleware-protected', }
Migrating to v0.4.X and supabase-js v2#
With the update to supabase-js
v2 the auth
API routes are no longer required, therefore you can go ahead and delete your auth
directory under the /pages/api/
directory. Please refer to the v2 migration guide for the full set of changes within supabase-js.
The /api/auth/logout
API route has been removed, please use the signout
method instead:
<button onClick={async () => { await supabaseClient.auth.signOut() router.push('/') }} > Logout </button>
The supabaseClient
and supabaseServerClient
have been removed in favor of the createBrowserSupabaseClient
and createServerSupabaseClient
methods. This allows you to provide the CLI-generated types to the client:
// client-side
import type { Database } from 'types_db'
const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>())
// server-side API route
import type { NextApiRequest, NextApiResponse } from 'next'
import type { Database } from 'types_db'
export default async (req: NextApiRequest, res: NextApiResponse) => {
const supabaseServerClient = createServerSupabaseClient<Database>({
req,
res,
})
const {
data: { user },
} = await supabaseServerClient.auth.getUser()
res.status(200).json({ name: user?.name ?? '' })
}
- The
UserProvider
has been replaced by theSessionContextProvider
. Make sure to wrap yourpages/_app.js
componenent with theSessionContextProvider
. Then, throughout your application you can use theuseSessionContext
hook to get thesession
and theuseSupabaseClient
hook to get an authenticatedsupabaseClient
. - The
useUser
hook now returns theuser
object ornull
. - Usage with TypeScript: You can pass types that were generated with the Supabase CLI to the Supabase Client to get enhanced type safety and auto completion:
Creating a new supabase client object:
import { Database } from '../database.types'
const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>())
Retrieving a supabase client object from the SessionContext:
import { useSupabaseClient } from '@supabase/auth-helpers-react' import { Database } from '../database.types' const supabaseClient = useSupabaseClient<Database>()