A
A
Alexander2022-04-05 15:40:36
User identification
Alexander, 2022-04-05 15:40:36

How to implement OAuth 2.0 authentication on the server and bind to the client?

How to correctly implement OAuth 2.0 authentication (passportjs) on the server and connect with the client so that after the callback is executed, the user returns and cookies with tokens are set in setHeader?

Right now my code looks something like this:

@Get('google')
  @UseGuards(GoogleAuthGuard)
  async googleAuth2(@Res() res: Response) {
    console.log('res', res);
  }

  @Get('google/callback')
  @UseGuards(GoogleAuthGuard)
  async googleCallback(@Req() req: any, @Res() res: Response) {
    console.log('req', req.user);
    const { accessTokenCookie, refreshTokenCookie, user } = req.user;
    res.setHeader('Set-Cookie', [accessTokenCookie, refreshTokenCookie]);
    res.req.user;
    return res.redirect('http://localhost:3000/');
  }

The problem is that on the client, the localhost:5000/api/auth/google route is executed in a new window and it cannot be connected with the client logic, since no action is launched on the client and the saga is not executed, which dispatches the received user to the state, and passes it to private routes on the client.

const googleAuth = () => {
    window.open('http://localhost:5000/api/auth/google', '_self')
  }

Tell me which fullstack flow you are using for this task so that the application works correctly.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question