我已经在 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)