passport.jsnestjsfastifypassport-google-oauth2nestjs-passport

Cannot read property 'encrypted' of undefined


I am trying to set up google oauth2 using Passportjs and NestJS(Fastify as underlying HTTP framework). I am not able to initialize google auth screen and the error comes up every time I try. I have tried a few different ways to do this, but there seems to be a problem with the AuthGuard and the code breaks every time on this step. I am sharing a snippet of the controller here which is handling the route for initializing the auth screen.

Here is the controller:


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

The Google strategy:

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'],
    });
  }
}

The module looks like this:


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


Dependencies

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

Solution

  • Soon realized that PassportJS is meant to work with only ExpressJS and since I am using Fastify as an underlying HTTP framework, it was acting in an unexpected way.

    I so wish for PassportJS to make themselves compatible with other frameworks soon.