-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NextJs 14 middleware redirect issue: User Keeps getting redirected to /login after successful Sign-In #59218
Comments
@anni1236012 try setting the status code to 303. One of the reasons it might fail is because of the browser cache. return NextResponse.redirect(new URL("/login", request.url), {
status: 303,
}); |
@SuhelMakkad it did not change the behavior. |
Interesting 🤔 I am using this exact setup for my app and it works fine. I tried your repo on my local machine, and after the changes it worked. Maybe you need to clear your browser cache. |
I have exactly the same problem, it does not give the expected behaviour for a while |
I fixed it with rewrite instead of redirect. return NextResponse.rewrite(new URL("/login", request.url), {
status: 303,
}); |
@anni1236012 no, the problem is not solved. a short time still does not give the expected result |
@yasermazlum Please share your github link to recreate the issue. |
try to avoid loading cached page |
I also stucked at login page, when I debug see that The user object found and returns true response. But the status code is 303 |
At NextJS 15 (canary version) it was fixed, the server middleware is correct changing the browser URL after But I guess I found another error when passing a hash parameter The problem was not completely fixed. |
I am experiencing an issue with the middleware. I need to route the user based on their I have two roles in my app, When the user changes the
However, the issue I'm facing is that the app is not getting routed to the URL as expected; it remains unchanged. |
might need to use |
I'm facing a similar issue, where in this middleware :
When redirect should happen it is not working, and nothing happens. Strange thing .... |
Any solution to this? I have the same issue. NextResponse.redirect doesn't redirect but stays on the same site. |
I did one workaround though it is not a perfect solution but it worked fine in my case. I have one drop-down through which I am changing the
So here what happens is, the app sends the user to I hope this workaround helps you. ✌️ |
Workaround for all, Type
|
I do have a similar issue following the official docs / guide https://nextjs.org/docs/app/building-your-application/authentication#optimistic-checks-with-middleware-optional. The redirect simply does not work as expected. |
@lpknv Try disable middleware cache before you return response using |
Link to the code that reproduces this issue
https://github.com/anni1236012/nextjsMiddleware
To Reproduce
nextjs_middleware_issue.mp4
Current vs. Expected behavior
Delayed navigation, looks like all requests are not reaching middleware and served from cache.
Verify canary release
Provide environment information
Operating System: Platform: linux Arch: x64 Version: #38~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 2 18:01:13 UTC 2 Binaries: Node: 20.5.1 npm: 9.8.0 Yarn: 1.22.19 pnpm: N/A Relevant Packages: next: 14.0.4-canary.37 eslint-config-next: 14.0.3 react: 18.2.0 react-dom: 18.2.0 typescript: 5.3.2 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Middleware / Edge (API routes, runtime)
Additional context
No response
The text was updated successfully, but these errors were encountered: