首页 > 解决方案 > 如何在 NestJS 中管理 Strategy 用户信息并在 Angular 中将其传递给客户端?

问题描述

在这段隔离期间,我正在做一个小项目,以了解项目的后端部分。在这种情况下,我使用 Angular 作为客户端,使用 NestJS 作为后端。

在这个项目中,我正在学习如何通过 Steam OpenID 登录。我使用了一个名为“Passport-steam”的第三方包来让策略进行身份验证。

在我的客户端,当我单击日志按钮时,我将窗口位置更改为控制器,其中我有自动调用策略的方法。

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

此策略需要一个返回 URL,即“.../api/auth/steam/callback”,这是控制器中的另一种方法。

@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();
  }
}

我不知道如何在客户端接收用户配置文件,因为在回调方法中,如果我重定向到我的页面,我没有任何信息。我创建了一个配置文件方法来获取存储在回调中的用户信息,然后在我的客户端订阅它以获取此信息。唯一的问题是我不能处理多个连接的人,这不是我的本意。

如何将用户信息从服务器端传递到客户端?

标签: angularauthenticationjwtnestjsopenid

解决方案


推荐阅读