Next.js Discord

Discord Forum

useSession returns unauthenticated in production when accessing a non root route directly via url.

Answered
Atlantic menhaden posted this in #help-forum
Open in Discord
Atlantic menhadenOP
I already made a post about this but didn't describe it well enough.

Anyone familiar with Next Auth or AuthJS. Are you able to help me troubleshoot this issue? i've tried everything.

Behaviour. When accessing non root route directly via url, session already returns null.

- I have tried session using useSession().
- I have tried getServerSide props with getSession and passing the session in as a prop.

Attached is a demonstration of the behaviour, alongside the associated files:

/dashboard/index.js (client side page) ( this has been changed constantly to accomodate getserverside props, etc. it's true that status returns unauthenticated too)
https://pastebin.com/TZbNYPf5

_app.js (has my SessionProvider wrapped)
https://pastebin.com/AgutihMj

[..nextAuth].js
https://pastebin.com/4exYqkPa

additional info
- only happens in production
- production uses cloudflare to route dns (idk if important just worth a mention)
Answered by Ray
since you are not fetching the session in getServerSideProps so you don't need to pass the session to the provider
View full answer

16 Replies

Atlantic menhadenOP
Atlantic menhadenOP
noticed there's a lot of actives rn so bump
@Ray ts const { data: session, status } = useSession({ required: true }) try this in `/dashboard/index.js`
Atlantic menhadenOP
no luck :( not pog.
it redirect you to signin but you are not able to signin?
@Ray it redirect you to signin but you are not able to signin?
Atlantic menhadenOP
the sign in is successful, and there is a session stored, but it still displays as unauthenticated. unless my request originates from the root of my app.
thanks for the help
this has been boggling my brain for like 2 days
brother... you are the actual GOATTTT. can i paypal you $10 or something for helping?
genuinely saved my life
since you are not fetching the session in getServerSideProps so you don't need to pass the session to the provider
Answer
Atlantic menhadenOP
was then about to say, i would love to know WHY it wasn't working
you're the best