Next.js Discord

Discord Forum

Help Needed: Next.js + Supabase Authentication Error

Unanswered
Bigheaded ant posted this in #help-forum
Open in Discord
Bigheaded antOP
Hey everyone! I'm struggling with a nasty error in my Next.js app using Supabase authentication. I've tried multiple solutions but can't seem to fix it.

The error:

Error: Route "/api/conversations/[id]" used cookies().get('sb-tnmjazfqpgzwxxxvwjcn-auth-token'). cookies() should be awaited before using its value.

My code:
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'; import { cookies } from 'next/headers';

// This doesn't work
const cookieStore = cookies(); const supabase = createRouteHandlerClient({ cookies: () => cookieStore });
I've tried:

Using await cookies()
Passing cookies directly
Using Promise.resolve(cookies())
Creating a custom cookie handler
Using the @supabase/ssr package

Nothing seems to fix it! The app works when I manually test it, but I keep getting build errors and runtime warnings.
Anyone else run into this issue with Next.js + Supabase authentication? Any working solutions?

Environment:

Next.js (App Router)
@supabase/auth-helpers-nextjs
TypeScript

Thanks in advance for any help!

3 Replies

Original message was deleted
Dutch
can you show the file of this code
// This doesn't work
const cookieStore = cookies();
const supabase = createRouteHandlerClient({ cookies: () => cookieStore });
Bigheaded antOP
added as any fixed the error: const supabase = createRouteHandlerClient({ cookies: () => cookieStore as any });
Dutch
also you can type it as typeof cookieStore or its type