router.push doesn't work in production #51782
-
SummaryThis function is supposed to redirect the user to the async function handleSubmit(event) {
event.preventDefault();
// reset message and error
setMessage(null);
setError(null);
// get form data
const form = event.target;
const formData = new FormData(form);
const email = formData.get("email");
const password = formData.get("password");
// validate
if (!email || !password) {
setError("Please fill in all fields");
return;
}
// when everything is ok, change the content of the button to "Loading..."
setButtonContent("Loading...");
// send data to server
try {
const data = await signIn("credentials", {
redirect: false,
email,
password,
});
if (data.error !== null) {
setError(data.error);
setButtonContent("Login");
return;
}
} catch (error) {
console.log(error);
return;
}
console.log("Before redirect");
router.push("/dashboard");
console.log("After redirect");
} Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 19 replies
-
Experiencing exactly the same issue |
Beta Was this translation helpful? Give feedback.
-
Is this using App Router? or Page Dir? |
Beta Was this translation helpful? Give feedback.
-
@itzAymvn I don't know why exactly, but in my case it wouldn't redirect because at the time redirecting, router was not aware of user being successfully authenticated, so I had to refresh the state manually router.refresh() and then router.replace('route-to-redirect') So in your case it would be console.log("Before redirect");
router.refresh(); // dirty fix, but it works
router.push("/dashboard");
console.log("After redirect"); CC @icyJoseph |
Beta Was this translation helpful? Give feedback.
-
I fixed the issue by removing the I believe the problem occurred because I attempted to prefetch the dashboard page before a user logged in. This caused an issue because I have middleware in place that redirects users to the root |
Beta Was this translation helpful? Give feedback.
-
This issue is the most annoying ever..this is'nt my first time using next-auth..how tf will a router.push not work even after getting a console.log just before thee router.push...nothing seems to work..might just dump it for a custom auth..what a waste of time.damn |
Beta Was this translation helpful? Give feedback.
-
Dealt with this same issue, the solution was similar to this from @xiliumz, but there was no prefetch(). If an unauth'd user clicked on a button that calls router.push('/edit'), middleware redirected them to /login. Then they would successfully auth and a router.push('/edit') on /login would run, but it did nothing. The solution was to point the original button at /login instead of /edit (middleware redirects already-auth'd users from /login to /edit anyway) and, for good measure, put a router.refresh() in front of /login's router.push(). |
Beta Was this translation helpful? Give feedback.
@itzAymvn I don't know why exactly, but in my case it wouldn't redirect because at the time redirecting, router was not aware of user being successfully authenticated, so I had to refresh the state manually
and then
So in your case it would be
CC @icyJoseph