'How to manage the Strategy user info in NestJS and pass it to the client side in Angular?

I'm doing a little project in this quarantine time to learn about the backend part of a project. In this case I'm using Angular as client-side and NestJS as back-end.

In this project I'm learning how to login through Steam OpenID. I have used a third party package called "Passport-steam" to get the Strategy to authenticate.

In my client side when I click in the log button I change the window location to the controller where I have the method that automatically calls the Strategy.

window.location.href = `${environment.API}api/auth/steam`;    

This Strategy needs of a return URL which is ".../api/auth/steam/callback", another method in the controller.

@Controller('auth')
export class AuthController {
  private readonly logger = new Logger(AuthController.name);
  constructor(private readonly authService: AuthService) {}


  @Get('steam')
  @UseGuards(SteamAuthGuard)
  steamLogin(){ //this method calls the strategy }

  @Get('steam/callback')
  @UseGuards(SteamAuthGuard)
  async steamLoginCallback(@Request() req, @Res() res)
  {
    //This method is called once the user has logged in through steam
    console.log(req.user);
    this.authService.setUserInfo(req.user);
    res.redirect(`${environment.FRONTEND_URL}`);
  }

  @Get('profile')
  getProfile() {
    return this.authService.login();
  }
}

I didn't have a clue about how to receive the user profile in the client side because in the callback method, if I redirect to my page I didn't have any info. I created a profile method to get the user info stored in the callback and then subscribe to it in my client side to get this info. The only problem is that I can't handle more than one person connected which is not my intention.

How can I pass the user info from the server side to the client side?



Sources

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

Source: Stack Overflow

Solution Source