我正在尝试使用Passportjs和NestJS(Fastify作为基础HTTP框架)设置google oauth2。我无法初始化google auth屏幕,每次尝试时都会出现错误。我尝试了几种不同的方法来执行此操作,但是AuthGuard似乎存在问题,并且此步骤每次都会中断代码。我在这里共享一个控制器的片段,该片段正在处理初始化auth屏幕的路由。
这是控制器:
import { Controller, Get, Post, Body, UseGuards, Req, Res } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Controller('auth')
export class AuthController {
@Get('google')
@UseGuards(AuthGuard('google'))
intializeGoogleLogin() {}
}
Google策略:
import { Strategy } from 'passport-google-oauth2';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
constructor() {
super({
clientID: <cleintId>,
clientSecret: <clientSecret>,
callbackURL: `/auth/google/callback`,
scope: ['profile', 'email'],
});
}
}
该模块如下所示:
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { GoogleStrategy } from './google.strategy';
@Module({
imports: [],
controllers: [AuthController],
providers: [AuthService, GoogleStrategy],
exports: [],
})
export class AuthModule {}
依赖项
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/passport": "^7.0.0",
"@nestjs/platform-fastify": "^7.0.5",
"@nestjs/typeorm": "^7.0.0",
"passport": "^0.4.1",
"passport-google-oauth2": "^0.2.0",
"passport-google-oauth20": "^2.0.0",
答案 0 :(得分:0)
Soon意识到PassportJS只能与ExpressJS一起使用,并且由于我将Fastify用作基础HTTP框架,因此它以一种意外的方式起作用。
我希望PassportJS能够尽快与其他框架兼容。