New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add SessionProvider
so i can useSession
#1607
Comments
this is actually a reasonable point - we (maintainers) tend to pass the session from the server nowadays, but |
My specific use case I was using the session to check if the user was logged in and rendering either a login or submit button dynamically as well as displaying some user data. Using the pages router this was fine but now using the app router I can't easily do this the way I did before. I was thinking of making a TRPC endpoint to supply the session and user data to the client component but can't decide if that's jank/hacky. How would you suggest going about using the user session and user info in a client component? Pass as prop or TRPC endpoint? I'm asking from a JR dev point of view. |
in general the best way to do it now is to use |
SessionProvider
so i can useSession
I've been building with next-auth and appdir for a while now and never found myself missing If more report it as missing we might reconsider but this is a conscious decision for now |
If you get the serverSession in the root layout like this
You can then pass it into the "use client" Providers, which again passes it to SessionProvider. Now you can access session in client components with useSession without prop drilling and without loading state.
What do you guys think? |
I'm highly interested in this as well, please consider adding the feature :) My particular use case is updating the session on the client side when a user switches orgs, read from the url pathname |
With NextAuth v5 (which is in beta and will be out in the next couple of weeks) you'll do this using server actions |
@juliusmarminge You mentioned using server actions. How would you recommend doing so? For now, I'm just passing props from Use case is to conditionally render a sign in / sign out button and the content inside of it. |
if you're using app router you won't need |
Considering how much easier it makes migrating from pages router, I'm also with adding the provider. Although making the migration easy might not be a priority for the maintainers. |
At first I thought this was an oversight but reading through this thread, I've changed my mind. Specifically this from Julius:
|
Provide environment information
"initVersion": "7.22.0"
Describe the bug
Session provider from Next auth not wrapping children in
src/app/layout.tsx
and must be added manually. Calling useSession from Next auth not available till this is done.Reproduction repo
https://stackblitz.com/github/L-Steinmacher/t3-app-dir-next-auth?file=src%2Fapp%2Flayout.tsx
To reproduce
npm create t3-app@latest
select options for Next Auth and app dir. Calling hookuseSession
not available till wrapping children in the provider.Additional information
Not sure if it's a bug per se but thought I'd report this.
The text was updated successfully, but these errors were encountered: