Server side redirects after login
Answered
Dovekie posted this in #help-forum
DovekieOP
I have a NextJS 13 app on which I've implemented auth.
I have an API route
Then, if the response is successful, the client app redirects itself to the
I'm wondering if the server could also redirect the client, taking away any responsibility during auth.
I have an API route
/login that's called by the client with a POST request (with a username and password). It implements the necessary auth logic and then sends a response by setting the client's headers to the auth tokens.Then, if the response is successful, the client app redirects itself to the
/ route.I'm wondering if the server could also redirect the client, taking away any responsibility during auth.
Answered by joulev
you can do this but only with server actions - then you can use
redirect to redirect the user away. regular route handlers cannot achieve this.2 Replies
@Dovekie I have a NextJS 13 app on which I've implemented auth.
I have an API route /login that's called by the client with a POST request (with a username and password). It implements the necessary auth logic and then sends a response by setting the client's headers to the auth tokens.
Then, if the response is successful, the client app redirects itself to the / route.
I'm wondering if the server could also redirect the client, taking away any responsibility during auth.
you can do this but only with server actions - then you can use
redirect to redirect the user away. regular route handlers cannot achieve this.Answer
DovekieOP
Interesting, I'll check it out. Thank you joulev!