
Password Based Auth flow for React Router and Supabase
import { type EmailOtpType } from '@supabase/supabase-js'
import { redirect, type LoaderFunctionArgs } from 'react-router'
import { createClient } from '@/registry/default/clients/react-router/lib/supabase/server'
export async function loader({ request }: LoaderFunctionArgs) {
const requestUrl = new URL(request.url)
const token_hash = requestUrl.searchParams.get('token_hash')
const type = requestUrl.searchParams.get('type') as EmailOtpType | null
const _next = requestUrl.searchParams.get('next')
const next = _next?.startsWith('/') ? _next : '/'
if (token_hash && type) {
const { supabase, headers } = createClient(request)
const { error } = await supabase.auth.verifyOtp({
type,
token_hash,
})
if (!error) {
return redirect(next, { headers })
} else {
return redirect(`/auth/error?error=${error?.message}`)
}
}
// redirect the user to an error page with some instructions
return redirect(`/auth/error?error=No token hash or type`)
}
You'll need to set the following environment variables in your project: VITE_SUPABASE_URL and VITE_SUPABASE_PUBLISHABLE_KEY.