'How to Redirect From Backend To FrontEnd after Authentication

I Have My Backend (nodejs) running on another Port And My Frontend (React) Running On Another Port...So After After Sending Credentials To Backend And Authentication...How Can I Redirect To Front End ?



Solution 1:[1]

res.writeHead(302, {
    Location: 'http://front-end.com:8888/some/path'
});
res.end();

If you specify the full url you can redirect to another port using NodeJS.

Solution 2:[2]

You may use a web API (Express JS) in nodejs to build web api and frontend any plainJS or modren libaries that will send HTTP request to backend. It will help more.

Solution 3:[3]

In your case, you'd better auth request through Ajax, that way you can return some kind of jwt token if you're using it, or trigger any other session related action on frontend on successful login.

Solution 4:[4]

in my case, I was handling the verifyEmail on the backend of a nest.js server.

How I was able to redirect to the frontend after handling the request, was to use the res.redirect from express.


  @Get('verify/:token')
  async verifyUser(@Param('token') token, @Res() response: Response): Promise<any> {
    const redirectUrl = await  this.authService.verifyUserEmail(token);
    response.redirect(redirectUrl);
    return {redirectUrl};
  }

And, it worked like a charm.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sromeu
Solution 2 Hidayat Arghandabi
Solution 3 millenion
Solution 4 godofjs