Hikmet
Hikmet•2mo ago

Session token received but it doesn't work

Hello friends, I tried to authenticate by following a video with next.js. At first I couldn't even log in, but I made a mistake and I fixed it. My problem now is that when I log in to the site, the session token is created and I can see it from devtools. However, logically, the logged in user should not be able to come back, but I can come back to the login page. https://github.com/HikmetMelikk/authnmore You can review the codes here and we can discuss them. Thanks in advance.
GitHub
GitHub - HikmetMelikk/authnmore: trying something from scratch
trying something from scratch. Contribute to HikmetMelikk/authnmore development by creating an account on GitHub.
Solution:
Use the file middleware.ts (or .js) in the root of your project to define Middleware. For example, at the same level as pages or app, or inside src if applicable.
Jump to solution
31 Replies
Korven
Korven•2mo ago
I took a very very cursory glance, so I may have missed it, but I didn't see you checking if the user is already checked in anywhere
Hikmet
HikmetOP•2mo ago
my friend said use state for check user is logged in or not but in video instructor not checking it i am learning auth so don't know how the auth flow works but logically it should be work
Korven
Korven•2mo ago
are you finished watching the entire video you're following? it's highly likely they'll come to that part
Hikmet
HikmetOP•2mo ago
yeah i already finished the video also i have the source code maybe you know cosden solutions i love his works and want to use his solutions but for now its doesnt work on my project https://github.com/cosdensolutions/code/tree/master/videos/long/auth-flow-next-js/src/app here is the source code of the content
Korven
Korven•2mo ago
GitHub
code/videos/long/auth-flow-next-js/src/middleware.ts at master · co...
Source code for all Cosden Solutions videos. Contribute to cosdensolutions/code development by creating an account on GitHub.
Korven
Korven•2mo ago
they have this middleware defined, which checks for the session and if the route is protected or not, and routes the user accordingly
Hikmet
HikmetOP•2mo ago
yessir i can already log in and show error messages
Korven
Korven•2mo ago
maybe it's not being run correctly in your case?
Hikmet
HikmetOP•2mo ago
but major problem is
Korven
Korven•2mo ago
you can go back to the login page after logging in
Hikmet
HikmetOP•2mo ago
not store session token
Korven
Korven•2mo ago
right?
Hikmet
HikmetOP•2mo ago
yeah
Korven
Korven•2mo ago
the middleware is supposed to prevent that so there's probably a bug in there somewhere
Hikmet
HikmetOP•2mo ago
actually i can store the token but why isn't working idk ahahahj
Korven
Korven•2mo ago
I'd recommend using a debugger and stepping through it line by line to see where it's not behaving as expected
Hikmet
HikmetOP•2mo ago
could you teach that how can i debug it bcs i tried a lot or can you try my code on your local?
Korven
Korven•2mo ago
I'm on my phone so that ain't an option 😬
Hikmet
HikmetOP•2mo ago
oh okey np i checked my code for million time and dont understand what is wrong
Korven
Korven•2mo ago
you have a different folder structure I'm seeing than the original video code maybe that's causing the issue?
Hikmet
HikmetOP•2mo ago
i think that also but in next.js using src folder is optional so i dont think so but i want to try it
Korven
Korven•2mo ago
try it out anyways, you can revert if not
Hikmet
HikmetOP•2mo ago
yeap u r right i cheched up package.json also ahahah kekw
Korven
Korven•2mo ago
okay so i found your problem
Solution
Korven
Korven•2mo ago
Use the file middleware.ts (or .js) in the root of your project to define Middleware. For example, at the same level as pages or app, or inside src if applicable.
Korven
Korven•2mo ago
so if you're using the app dir, it needs to be besides app, not inside it if I'm reading this right so yeah there you go 😄
Hikmet
HikmetOP•2mo ago
holy shoot wait it worked oh dude you saved my mind because I was going crazy thank you so much how and where did you get that information
Korven
Korven•2mo ago
i just Google Nexjs middleware reading the docs is a good habit
Hikmet
HikmetOP•2mo ago
i didn't think that middleware function directory would be a problem
Korven
Korven•2mo ago
well now you know :p you can mark this as closed and stuff
Hikmet
HikmetOP•2mo ago
okey