-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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 |
Thanks, I will give it a try! |
@lpknv Did that work? I am having the same issue trying to deploy to a digital ocean server |
I didn't try it yet. If you are curious you can add |
@lpknv unfortunately it didn't work. I belive my issue stems from my SSL certificate which is described here. My SSL guy is sorting it out now. https://vercel.com/guides/resolve-err-too-many-redirects-when-using-cloudflare-proxy-with-vercel Edit: it was an SSL issue, as well as an API being redirected to the wrong route which caused a loop during production as the environment variable wasn't set properly |
This might be because your use case only requires server-side. However, this doesn’t actually solve the issue. https://www.dhiwise.com/post/nextjs-rewrites-everything-you-need-to-know |
for me the problem was that I was trying to make the middleware cleaner
this didn't work until I just squashed everything into middleware function directly |
I can't recall that in the nextjs docs that you can use Middleware everywhere you want. |
` export default async function middleware(req: NextRequest) { if (isPublicPage) {
} export const config = { ` Still stuck :((, the network tab show the post request to '/' (200 success) but redirect not perform , |
You're not returning your |
This comment has been minimized.
This comment has been minimized.
It probably isn't the same for you, but my issue was a combination of a mistyped environment variable name for the API route and my SSL certificate being invalid so it tried to route to a HTTP version |
"mistyped environment variable name" - I can relate to that haha... |
I've tried all the solutions mentioned here, but nothing is working. Has anyone come up with a new solution? I am using two redirects, 1st redirect from config and the second in middleware. 1st redirection is working fine, but following code is not working
|
Same problem here. Tried disabling cache, adding 303 status code, changing from |
@HCarrer did you manage to solve it in any way? |
Need to manually call router.refresh() in the client I think. To clear the router cache. That was my solution for fixing middleware after user's log in. Refreshing the page fixed it, but router.refresh() does also. I wish there was a way to delete the router cache without reloading the page though. |
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: