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:Jump to 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.
31 Replies
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
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
are you finished watching the entire video you're following?
it's highly likely they'll come to that part
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
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.
they have this middleware defined, which checks for the session and if the route is protected or not, and routes the user accordingly
yessir
i can already log in
and show error messages
maybe it's not being run correctly in your case?
but major problem is
you can go back to the login page after logging in
not store session token
right?
yeah
the middleware is supposed to prevent that
so there's probably a bug in there somewhere
actually i can store the token but why isn't working idk ahahahj
I'd recommend using a debugger and stepping through it line by line
to see where it's not behaving as expected
could you teach that how can i debug it
bcs i tried a lot
or can you try my code on your local?
I'm on my phone so that ain't an option 😬
oh
okey np
i checked my code for million time and dont understand what is wrong
you have a different folder structure I'm seeing
than the original video code
maybe that's causing the issue?
i think that also but in next.js using src folder is optional
so i dont think so but i want to try it
try it out anyways, you can revert if not
yeap u r right
i cheched up package.json also ahahah
kekw
okay so
i found your problem
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.
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 😄
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
i just Google Nexjs middleware
reading the docs is a good habit
i didn't think that middleware function directory would be a problem
well now you know :p you can mark this as closed and stuff
okey