Next.js Discord

Discord Forum

Redirecting the client to another page

Answered
Dovekie posted this in #help-forum
Open in Discord
Avatar
DovekieOP
1. Client makes a request to /api/auth/signup with an email and password
2. Server reads the data, creates the user and then sends an email confirmation
3. I then want the client to be redirected to /auth/confirm-signup

My problem is that if I redirect the client, the request is a "POST" and not a "GET" that will show the client the confirm signup page.

Any ideas on how I could solve this ?
Answered by fuma πŸ’™ joulev
Correct, you can't redirect the user with a fetch on the client side. Use router.push instead
View full answer

10 Replies

Avatar
DovekieOP
Ideally I'd want the server redirecting the user to the confirm-signup page. But the only thing that comes to my mind is to have the client navigate to the page with a router.push or something
Avatar
fuma πŸ’™ joulev
Correct, you can't redirect the user with a fetch on the client side. Use router.push instead
Answer
Avatar
DovekieOP
I see, just to be 100% certain.

There's no way for me to redirect the user from the server to a "confirm-signup" page, right ?

I hope my question is clear
Here's how I want the flow to take place from the user's perspective:

1. I'm at /auth/signup
2. I type in my password, and email
3. I submit and make a request to /api/auth/signup
4. If my credentials are good, I want to see the confirm-signup screen. So I need to get redirected to it

So, the redirect can only be performed by the client by using router.push() or can the server somehow redirect me too ?
Avatar
fuma πŸ’™ joulev
True, unless the user directly open /api/auth/signup (if it is a GET API endpoint), they won't get redirected
because that will be awful if opening a request can cause redirection on the client side
Avatar
DovekieOP
Copy copy If there's any piece of documentation you suggest I read so I can get a better understanding of these mechanisms please let me know

Otherwise, thank you very much for your help
Avatar
fuma πŸ’™ joulev
Actually it's not related to Next.js, read this docs:
https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect
You can use follow in order to redirect the user, but that ignores client-side cache. With router.push, you can reuse the cache, resulting a better performance during redirection.
Avatar
DovekieOP
I see, thank you again