then-purpleT
Refineโ€ข3y agoโ€ข
7 replies
then-purple

Supabase Cookie-based Auth and Next.js 13 App Router

There are some new videos showing how to best integrate Supabase and NextJs App Router:

https://www.youtube.com/playlist?list=PL5S4mPUpp4OtMhpnp93EFSo42iQ40XjbF

I'd like to implement this into refine providers.

There is also a guide related to the videos.
The first piece is a middleware that manage sessions with createMiddlewareClient

https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware

next there is a server-side auth flow Code Exchange example:

https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-sign-in-with-code-exchange

It creates an auth/callback route to get a code from the url to pass to auth.exchangeCodeForSession(code)

method of createRouteHandlerClient({ cookies }) which usecookies from next/headers

Back to refine, there is this AuthProvider on the nextjs appdir example:

https://github.com/refinedev/refine/blob/master/examples/with-nextjs-appdir/src/authProvider.ts

it also uses cookies to store auth data.

I implemented the createClientComponentClient method from @supabase/auth-helpers-nextjs to get the client in the methods of the Supabase AuthProvider.

In the register method I'm passing to Supabase signUp() method also emailRedirectTo option that points to the auth/callback route.

Is this the same as the check method on the refine AuthProvider of the appdir example? How can I integrate the two?

How should I handle the cookies calls in the refine AuthProvider methods? Can I just remove the cookie parts?

This is the Client Component, is this the one to provide as dataProvider to refine?

const supabase = createClientComponentClient()


To handle mutations there are also createServerComponentClient createServerActionClient that uses cookies from next/headers.

What do you think? Can I just remove the cookie part of the AuthProvider and feed the createClientComponentClient as dataProvider?
YouTube
The Next.js 13 App Router makes it really easy to write client code - for things like realtime subscriptions - and server code - for things like data fetchin...
Cookie-based Auth and the Next.js 13 App Router
GitHub
Build your React-based CRUD applications, without constraints. ๐ŸŒŸ Star to support our work! - refine/authProvider.ts at master ยท refinedev/refine
refine/authProvider.ts at master ยท refinedev/refine
Authentication and Authorization helpers for creating an authenticated Supabase client with the Next.js 13 App Router.
Supabase Auth with the Next.js App Router | Supabase Docs
Was this page helpful?