What pattern should I use to access Session in client component

Bit unsure of how to go about this, any help massively appreciated I'm using app router and I've got a Header component that needs to access the Session (to display the user's profile picture) and also have some state (toggling between 2 menu displays, for example). When I use a server component, I get an error for using useState/useRef (I tried using a zustand store to get around useState but didn't work). When I use a client component I get complains that I'm accessing serverside variables (I assume from the session request). What's the best way to go about this? Potential thoughts: * useServer function call from a client component to get the session? * Use a server component and then manage the state using a top level context provider? I'll have a bunch of popovers and sidebars that should be able to control/close each other, hence why I prefer Zustand over just useState Thank you!
No description
No description
Solution:
1. get the session in a server component and pass it to any client components that need it
Jump to solution
17 Replies
hyhy
hyhy•8mo ago
I tried splitting each of the 3 sections into separate client components that can communicate via zustand, but this is throwing all sorts of issues i don't understand
No description
No description
hyhy
hyhy•8mo ago
Might try clerk instead
Max
Max•8mo ago
I like clerk way more, think ya should go that route. can use easily in mobile if ya go that route eventually.
hyhy
hyhy•8mo ago
Clerk is so much nicer for this, probably just gonna use that and rip out nextAuth
hausinho
hausinho•8mo ago
I understand that people are recommending Clerk, but has anyone solved this issue in nextauth?
Julgers
Julgers•8mo ago
@hausinho @hyhy If I understood the question correctly, you can use
const { data: session, status } = useSession();
const { data: session, status } = useSession();
inside your client component and then do something like
<Component image={session.user.image} />
<Component image={session.user.image} />
When I use a client component I get complains that I'm accessing serverside variables
That is probably because the server side getServerAuthSession() was used instead of useSession()
hyhy
hyhy•8mo ago
Ah interesting, I'll give that another try and report back
hausinho
hausinho•8mo ago
Did it work for you?
hyhy
hyhy•8mo ago
Not at my pc now but I'll give it a try this evening Gonna be this weekend instead I didn't get a chance to and I need to travel all day today
hausinho
hausinho•8mo ago
Thanks for the heads up, I'll be on the lookout 🙂
hyhy
hyhy•8mo ago
i wasn't able to get this to work, going to test further and see if i can get anywhere with it
cje
cje•8mo ago
is this with create t3 app using the app dir? we didn't include the providers for getting the session on the client, because the new server patterns are better you can either
Solution
cje
cje•8mo ago
1. get the session in a server component and pass it to any client components that need it
cje
cje•8mo ago
2. add the providers back in and use useSession in client components
Julgers
Julgers•8mo ago
My bad, yeah I forgot to mention that you do need the session provider for client components you can find that here for next auth v4 for next auth v5 (experimental) in case you are using that, it seems to not have changed much, according to the API reference This is assuming that you still want to do usestate or useeffect, which requires you to make it a client component. Otherwise you can go for the first option that @cje mentioned
cje
cje•8mo ago
you dont need the session provider for client components can pass the session as a prop from rsc
hyhy
hyhy•8mo ago
cheers for that, i ended up just going back to the page router to get some work done so i'll try migrating to app router again soon
Want results from more Discord servers?
Add your server
More Posts