This guide will help you set up the integration between Clerk and Supabase using the modern third-party auth approach. This setup uses Clerk for user management and Supabase for your application data.
- A Clerk account and application
- A Supabase project
- Next.js application with both
@clerk/nextjsand@supabase/supabase-jsinstalled
- Go to your Supabase Dashboard
- Navigate to Authentication > Integrations
- Add a new Third-Party Auth integration
- Select Clerk from the list
- Follow the configuration steps provided by Supabase
- Visit Clerk's Connect with Supabase page in your Clerk dashboard
- Follow the setup instructions to configure your Clerk instance for Supabase compatibility
- This will set up the necessary JWT claims and configuration
Copy your .env.example to .env.local and fill in the required values:
# Clerk Authentication
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_...
CLERK_SECRET_KEY=sk_test_...
# Supabase
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJ...- Start your Next.js application
- Sign up/in through Clerk
- Try making authenticated requests to Supabase
import { createSupabaseServerClient } from '@/lib/supabase'
export async function getData() {
const supabase = await createSupabaseServerClient()
const { data, error } = await supabase
.from('your_table')
.select('*')
return data
}import { supabase } from '@/lib/supabase'
import { useAuth } from '@clerk/nextjs'
function MyComponent() {
const { getToken } = useAuth()
const fetchData = async () => {
const token = await getToken()
const { data, error } = await supabase
.from('your_table')
.select('*')
.auth(token)
}
}import { getCurrentUser } from '@/lib/user'
export async function ProfilePage() {
const user = await getCurrentUser()
if (!user) {
redirect('/sign-in')
}
return <div>Welcome {user.firstName}!</div>
}Create RLS policies in your Supabase tables that use Clerk user IDs. Example:
-- Enable RLS on your table
ALTER TABLE your_table ENABLE ROW LEVEL SECURITY;
-- Allow users to read their own data
CREATE POLICY "Users can read own data" ON your_table
FOR SELECT USING (auth.jwt() ->> 'sub' = user_id);
-- Allow users to insert their own data
CREATE POLICY "Users can insert own data" ON your_table
FOR INSERT WITH CHECK (auth.jwt() ->> 'sub' = user_id);- Ensure third-party auth is properly configured in Supabase
- Verify your Supabase URL and keys are correct
- Check that RLS policies allow the operations you're trying to perform
- Make sure you're using
createSupabaseServerClient()for server-side operations - For client-side, ensure you're passing the Clerk token to Supabase