redirection url does not change until I reload
Answered
Spotted Rail posted this in #help-forum
Spotted RailOP
I have implemented login and logout for my app using next-auth and middleware, but when the user successfully logs into the dashboard, the URL remains "/signin" instead of "/dashboard" until I reload the page. Similarly when I log out from the app the url shows "/dashboard" instead of "/signin".
Answered by Spotted Rail
Okay so I just downgraded to next14.0.2 from 14.2.3 and the redirection is working fine
Edit: 14.1.0 also works
Edit: 14.1.0 also works
16 Replies
Spotted RailOP
Transvaal lion
do you have a window.location.replace anywhere after sign in? i didn't see one
or some sort of routing after login?
Spotted RailOP
nope, i dont think it is needed
Spotted RailOP
here's my login form:
"use client"
import { useFormState, useFormStatus } from 'react-dom';
import { authenticate } from '@/app/lib/actions';
import { useForm } from "react-hook-form";
export default function SignInForm() {
const { register, handleSubmit, formState: { errors } } = useForm();
const [errorMessage, dispatch] = useFormState(authenticate, undefined);
const action = handleSubmit(async (data) => {
const response = await dispatch(data);
console.log("UserData", data);
});
return (
<form action={action}>
<h2>Sign In</h2>
<input {...register("email", { required: true })} type="email" placeholder="Email" />
{errors.email && <span className="error">Email is required</span>}
<input {...register("password", { required: true })} type="password" placeholder="Password" />
{errors.password && <span className="error">Password is required</span>}
<button type="submit">Sign In</button>
<button onClick={() => signIn("discord")}>Login with Discord</button>
<div
className="flex h-8 items-end space-x-1"
aria-live="polite"
aria-atomic="true"
>
{errorMessage && (
<>
<p className="text-sm text-red-500">{errorMessage}</p>
</>
)}
</div>
</form>
)
}
Spotted RailOP
Okay so I just downgraded to next14.0.2 from 14.2.3 and the redirection is working fine
Edit: 14.1.0 also works
Edit: 14.1.0 also works
Answer
Spotted RailOP
So my next question would be why it doesnt work in latest version of nextjs ?
probably it's this bug https://github.com/vercel/next.js/issues/61874
Spotted RailOP
how can this issue be solved? there's not much info on this page
for now you can try adding the hostname as well
so instead of
you do
for example.
Or just downgrade, which is the simplest solution and what i would do, while waiting for the team to patch this. (Or try to patch it yourself)
so instead of
NextResponse.redirect(new URL("/foo", req.url))
you do
const urlHost = process.env.NODE_ENV === "development"
? "http://localhost:3000"
: "https://yourdomain.com";
NextResponse.redirect(`${urlHost}/foo`);
for example.
Or just downgrade, which is the simplest solution and what i would do, while waiting for the team to patch this. (Or try to patch it yourself)
Spotted RailOP
yeah, thanks i'll try this OR maybe not use middleware at all?
yeah. plenty of options
if it's a framework bug, we have to use workaround, in this case there are plenty of them
Spotted RailOP
right