This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
useSession() hook returning undefined in NextJS v13.4.3 #7839
Labels
question
Ask how to do something or how something works
Question 💬
I am new to NextJS and I am using v13.4.3. My backend is performing auth and upon login, it is returning a JWT as HTTPONLY cookie which is attached to the browser. My requirement is to build components on the frontend that have conditional buttons/text based on whether the user is logged in or not.
My understanding is that I can use the useSession hook in my components to grab the session from the cookie when the page is rendering and this should tell me whether the user is logged in or not. The problem is that the useSession hook is always returning undefined.
How to reproduce ☕️
Here is my code and structure. I am using the app structure since I am on the newer version of Next as June 2023.
In my app folder, I have created a folder called auth which has a file called
auth-context.tsx
-src/app/auth/auth-context.tsx
In my app folder in the main
layout
file -src/app/layout.tsx
I have done the above in order to wrap the useSession in a sessionProvider by creating a separate client as mentioned by NextJS documentation. I followed this thread to learn about this - #5647
Now the component that needs access to the session is sitting in the
(dashboard)
folder in app folder -src/app/(dashboard)/home/page.tsx
The problem I am facing is that the object
data
always comes back as undefined when I navigate to the home page. When I check the browser, I am seeing the cookie being passed correctly. I decoded the cookie on jwt.io and it is the right cookie that my backend is passing. So it seems like my login functionality is working fine but I am unable to grab the session.The console in the browser logs this - LINE7 null The console in vs code logs this - LINE7 undefined
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
The text was updated successfully, but these errors were encountered: