Next.js Discord

Discord Forum

Why would it be a good idea to have a Session Provider wrapper be a client component?

Answered
King Shepherd posted this in #help-forum
Open in Discord
King ShepherdOP
My understanding of client components is that the static portion or a client component is pre-rendered serverside, and then hydrated after the initial pageload. Why would this need to be the case for a next auth session provider wrapper?
Answered by joulev
if you use the useSession hook or similar next-auth client-side hook in any client components, then for that hook to work, the client component must be a child of the SessionProvider

if you don't use any hooks then you don't need this SessionProvider. For example if you only use server components for retrieving auth state, you can use getServerSession – then you don't need to wrap the component under a SessionProvider
View full answer

18 Replies

@joulev providers are for client-side states so they *must* be client components – it's not even a question of should or shouldn't
King ShepherdOP
Thanks. In some articles I'm seeing people wrapping their RootLayout in that provider wrapper. If the whole application isn't wrapped in the provider, will session data not be available on the pages i leave out?
or how does that work
@King Shepherd Thanks. In some articles I'm seeing people wrapping their RootLayout in that provider wrapper. If the whole application isn't wrapped in the provider, will session data not be available on the pages i leave out?
if you use the useSession hook or similar next-auth client-side hook in any client components, then for that hook to work, the client component must be a child of the SessionProvider

if you don't use any hooks then you don't need this SessionProvider. For example if you only use server components for retrieving auth state, you can use getServerSession – then you don't need to wrap the component under a SessionProvider
Answer
@joulev providers are for client-side states so they *must* be client components – it's not even a question of should or shouldn't
King ShepherdOP
also, is the reason the SessionProvider wrapper needs to be a client component because being a client component allows for javascript code to run on the client that makes the request to the auth0 tenant? Where as if it were a server component, then javascript wouldn't be able to run on the client to make those requests?
I'm new to nextauth and nextjs broadly so let me know if that question made zero sense haha
@joulev this one i don't know since i don't use next-auth
King ShepherdOP
what is your preferred method of auth out of curiosity
I'm trying to be a full stack person and exploring a lot of routes for getting services up and running
@King Shepherd what is your preferred method of auth out of curiosity
i would say i have no preferred methods since my current job doesn't require me to build one :derp:
but there are many auth systems for you
supabase, next-auth, clerk
King ShepherdOP
I've heard of clerk
I know supabase for the db service but i'll look into what they do for auth
thanks for your help