-
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
In my app folder in the main
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
The problem I am facing is that the object 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 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I'd like to add something that might be too obvious for guys with more experience. My server auth is NOT using My point of all this is that I am using a different auth strategy on the backend (not next-auth). In this case, is it expected behaviour for Thank you guys! |
Beta Was this translation helpful? Give feedback.
Hi @Byron0000 and @KrishnaCodez,
So I solved my entire auth problem by doing it on the backend. Basically I used passportjs in my backend nestjs repository to issue a JWT. I then pass this JWT to the frontend as an
HTTPONLY
cookie which get stored in the browser. This JWT is then passed over to the backend from the frontend with every endpoint call and I created an auth guard (again with passportjs) which extracts the JWT verifies it and either allow the user through or kick them out and the frontend redirects them to login page.Now the only issue with this is that if you want to implement a feature such that your frontend does not make a call if a JWT does not exist or is expirted, you …