【问题标题】:Cannot read property 'encrypted' of undefined无法读取未定义的“加密”属性
【发布时间】:2020-08-16 23:27:34
【问题描述】:

我正在尝试使用 Passportjs 和 NestJS(Fastify 作为底层 HTTP 框架)设置 google oauth2。我无法初始化谷歌身份验证屏幕,每次尝试时都会出现错误。我尝试了几种不同的方法来做到这一点,但 AuthGuard 似乎存在问题,并且每次在此步骤中代码都会中断。我在这里共享一个控制器的 sn-p,它正在处理初始化身份验证屏幕的路由。

这里是控制器:


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() {}
}

谷歌战略:

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",

【问题讨论】:

    标签: passport.js nestjs fastify passport-google-oauth2 nestjs-passport


    【解决方案1】:

    很快意识到 PassportJS 只能与 ExpressJS 一起使用,并且由于我使用 Fastify 作为底层 HTTP 框架,它的行为方式出乎意料。

    我非常希望 PassportJS 能够尽快与其他框架兼容。

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2022-01-27
      • 1970-01-01
      • 2020-04-06
      • 2020-10-30
      • 2019-10-09
      相关资源
      最近更新 更多