Next.js Discord

Discord Forum

router.push not working on build

Answered
ncls. posted this in #help-forum
Open in Discord
Hello,

I built a login which redirects you after successful login using router.push and it's working absolutely fine in dev mode but when I build it and run it on my server, nothing happens when it should redirect me.
Does someone have an idea what's wrong here?
The line in question (fairly simple):
router.push(response.url || '/');
Answered by Ray
if (response?.ok) {
  router.push(response.url || '/');
  router.refresh();
} 

try this maybe
View full answer

4 Replies

When I click the login button, I get a 307 in the network tab (details in image below). The middleware seems to think that I'm not authenticated yet even though I am. The login works fine if the callbackUrl is a not protected route.
Someone have an idea why the middleware works fine locally but doesn't seem to see that I'm authenticated on live server unless I reload?
Full login page logic (NextAuth):
const response = await signIn('credentials', {
  login: login,
  password: password,
  callbackUrl: callbackUrl || '/',
  redirect: false,
});

if (response?.ok) {
  router.push(response.url || '/');
} else {
  if (response?.status === 401) {
    setFieldError('login', 'Credentials and password do not match.');
  }
}
Answer
@Ray ts if (response?.ok) { router.push(response.url || '/'); router.refresh(); } try this maybe
Thank you! I read about the fix but I put it the other way around, so refresh before push. Now that I think about it, that didn't make any sense😅