Redirect doesnt work
Answered
Pacific herring posted this in #help-forum
Pacific herringOP
Hi, if the user is created I attempt to redirect to the dashboard:
tho nothing happens? why is that? in middleware works fine.
export async function POST (request: NextRequest) {
// bla bla
if(user) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
}tho nothing happens? why is that? in middleware works fine.
15 Replies
European sprat
Do it client side after a success/fail
Answer
@European sprat Do it client side after a success/fail
Pacific herringOP
With useRouter's hook?
Yes, sending the
Hence, you should use client-side navigation like
POST request won't cause a redirect on the client side. Redirect only works when you directly open that url in browser.Hence, you should use client-side navigation like
router.push.Pacific herringOP
Why would you automatically think i solved my problem here @joulev
I was about to say that i cannot use useRouter hook there (in the component which is clientside) i've tried it already
Why? If you’re using Pages Router, make sure you are importing
useRouter from next/routerIn app router, it’s
next/navigation. Provide more information if you want me to help with that@Pacific herring Why would you automatically think i solved my problem here <@484037068239142956>
Because that is the only way to force navigate to another page (which is not vanilla JavaScript). You only have a few options, redirect() in server actions, router.push or vanilla window.location.href = “â€. Since you use a manual post request, it’s not server actions so the first choice is gone, last choice is also gone unless you really want to use vanilla JS
Of course feel free to remark to another answer when you find a better solution
Pacific herringOP
I'll send some details later tonight
Pacific herringOP
yh
solved...
for some reasons yesterday i was getting some random error in the console
about using useRouter hook in a bad state or sum shit like that
@joulev Because that is the only way to force navigate to another page (which is not vanilla JavaScript). You only have a few options, redirect() in server actions, router.push or vanilla window.location.href = “â€. Since you use a manual post request, it’s not server actions so the first choice is gone, last choice is also gone unless you really want to use vanilla JS
Pacific herringOP
sry lack of sleep made me act up ðŸ‘