小编Lor*_*bri的帖子

React WebApp 调用 NestJS 后端中使用 PassportStrategy 的 Google 登录无法正常工作

我已经在 NestJS 后端中使用 PassportStrategy 实现了 Google 登录(NestJS 后端开发基于本指南: https: //medium.com/@nielsmeima/auth-in-nest-js-and-angular-463525b6e071)。

@Get('google')
@UseGuards(AuthGuard('google'))
googleLogin()
{
    // initiates the Google OAuth2 login flow
}

@Get('google/callback')
@UseGuards(AuthGuard('google'))
googleLoginCallback(@Req() req, @Res() res)
{
    // handles the Google OAuth2 callback
    const jwt: string = req.user.jwt;
    if (jwt)
        return res.status(HttpStatus.OK).cookie('jwt', jwt, {
            httpOnly: true
        }).redirect('/');
    else 
        res.redirect('http://localhost:4200/login/failure');
}
Run Code Online (Sandbox Code Playgroud)

使用 @UseGuards(AuthGuard('google')) 应该启动 google 登录:

super({
        clientID    : 'XXX',     // <- Replace this with your client id
        clientSecret: 'XXX', // <- Replace this with your client secret …
Run Code Online (Sandbox Code Playgroud)

reactjs passport.js google-signin nestjs

2
推荐指数
1
解决办法
5190
查看次数

标签 统计

google-signin ×1

nestjs ×1

passport.js ×1

reactjs ×1