Next.js Discord

Discord Forum

infinite loop why??

Answered
Siamese Crocodile posted this in #help-forum
Open in Discord
Avatar
Siamese CrocodileOP
Image
Answered by ncls.
Becuase your middleware is protecting your login as well
View full answer

126 Replies

Avatar
Becuase your middleware is protecting your login as well
Answer
Avatar
Siamese CrocodileOP
so i put it like this?
Image
this fixes it but what about this
Image
the dot excludes the middleware file for somereason
Avatar
Why would you put a dot there?
Avatar
Siamese CrocodileOP
i just tested it and it worked it stopped my issue but i do not know why it works
Avatar
Because the matcher defines what routes your middleware is being ran on. If the user is not authenticated, they will be sent to /login. Since you didn't exclude that route in your middleware, your middleware will again check if the user is authenticated and if not, they will send them to /login again over and over again
Avatar
Siamese CrocodileOP
yes i get it but i mean the dot solution
.
that also stops the infinite loop but why o.o
also i would like not to exclude the login page
Avatar
Because in RegEx, the dot matches everything so it will exclude every single route and therefore not run the middleware on anything (since you are using ?! aka a "negative lookahead")
Avatar
Siamese CrocodileOP
if the user if autheticated should be redirected to the homepage
okei i see
Avatar
Then check what path the user is on in your middleware
if (request.nextUrl.pathname.startsWith('/login') && response.status !== 401) return NextResponse.redirect('/homepage');
Avatar
Siamese CrocodileOP
return NextResponse.redirect('/homepage'); tells me it has to be absolute path
is that normal
Avatar
Oh, yeah
Forgot that
Add your env variable
The one you also use to redirect to login
Avatar
Siamese CrocodileOP
why nexturl
does that mean its the redirect url
could it not just be .url
Avatar
Next.js
Avatar
Siamese CrocodileOP
hh
Avatar
.url is just the full URL as a string I think. Since the request is of type NextRequest, which expands the normal Request type, it basically just added the nextUrl property
Avatar
Siamese CrocodileOP
honestly the adding the login thing in the matcher feels like not the solution i try to explain why
regardless of which page i am i want this middleware to run including the login page
reason why is because i would like also protect the login page from being accessed if the user already is authenticated
Avatar
.
Avatar
Siamese CrocodileOP
i think i tried it but ill retry
wait
Avatar
You maybe put it in the wrong place
And don't forget to add your env variable for the full URL
If you want, you can also just post the code here so that I or others can check
Avatar
Siamese CrocodileOP
Image
it works but
there is 2 console.logs
when i login
Avatar
That's not where you put it XD
What you are doing rn is redirecting the user to the login, when they are logged in and trying to access the login
Avatar
Siamese CrocodileOP
yea i just realised
Avatar
You basically keep that
And add what I sent you above your check it the user is not authorized
Oh and to the other check you also add a check if they are on the login page because that was your original issue
Avatar
Siamese CrocodileOP
Image
Avatar
So if (response.status === 401 && !request.nextUrl.pathname.startsWith('/login'))
Avatar
Siamese CrocodileOP
Image
anyways it crashes ill take a break to have a clear mind
Avatar
This is still missing in your check if your user is unauthorized
Or you sent the wrong screenshot
Avatar
Siamese CrocodileOP
yea wrong
Image
wrong screenshot haha
Avatar
No
Avatar
Siamese CrocodileOP
i did add what you said only thing i changed was the 200
Avatar
Delete the top part
On the bottom you have the check for 401
You just add the check fi the user is on the login route there
Avatar
Siamese CrocodileOP
so delete first if statement
and keep second and third
i did that
and it did not work
Avatar
Yeah
Because the last one is missing the login route check
It just needs a && !request.nextUrl.pathname.startsWith('/login')
Avatar
Siamese CrocodileOP
Image
Avatar
Brown bear
not trying to be rude, but you can copy and paste this into chatGTP and get the answer 🤩
Avatar
Yes, this looks fine
Avatar
Brown bear
its sent by Robot God
Avatar
Siamese CrocodileOP
tried chatgpt did not solve it
Avatar
Brown bear
wow really, ive done the same thing and it was great
Avatar
Siamese CrocodileOP
paste the solution here lets see
its the end of the day ill get back to it later smt still not working anyways atleast i am in the good path
Avatar
If you tell us how it's not working, we can maybe help you
Avatar
Siamese CrocodileOP
ill update this post later
Avatar
Siamese CrocodileOP
okei after breathing a bit
Image
this fixes it only question i wonder is why does it run twice the middleware
if i got to the /dashboard it only consoles logs once
same with login
Avatar
Once for login and then once for dashboard after redirecting
Avatar
Siamese CrocodileOP
but if i
Image
i am already in the login page
so that the first console log
when i click that i get 2 more console logs
Avatar
Ok, got it. What does the login button do?
Avatar
Siamese CrocodileOP
good question xD
wait
Image
this so thats maybe why xD
the refresh thing i forgot about it
why is this client side tho
could i not make it server?
Avatar
Kind of
Avatar
Siamese CrocodileOP
o there is another console.log
omg.........
but still it logs 2 times
the button for login makes somehow that behaviour
Avatar
You can put a console.log(request.url) into your middleware to see exactly what routes are triggering it
Btw, have you ever looked into [NextAuth](https://www.npmjs.com/package/next-auth)?
Avatar
Siamese CrocodileOP
Image
looks like i get feed 2 docs when i refresh so thats maybe why
Avatar
Nope
Avatar
Siamese CrocodileOP
yea nextauth does not save sessions in database if i use credentials method
Avatar
router.refresh does not really refresh the whole page which is why you keep your previous data
Avatar
Siamese CrocodileOP
Image
it was commented after i saw it
but still the 2 logs
maybe it has smt to do with npm run dev
Avatar
.
It's probably your POST request to login and then your dashboard
Why do you need session strategy?
Avatar
Siamese CrocodileOP
cuz i would like to invalidate sessions
if i ban a user or i want to log out a user from a device
jwt u cant
Image
Avatar
Makes sense. You can however implement your own logic to use database sessions or invalidate JWTs (as in blacklisting them)
Avatar
Siamese CrocodileOP
Image
now this is interesting
i think its because of this
Image
the router thing pushes to the dashboard and because of that the middleware check again finds out that is auth an retriggers a redirect
so maybe i should check for this condition as well if it comes from a redirect or smt like that right?