-1

This is my combined Auth Guard in this I am getting the error "context.switchToHttp is not a function."

import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { JwtAuthGuard } from './jwt.strategy';
import { KakaoAuthGuard } from './kakao.strategy';
import { GoogleAuthGuard } from './oauth.strategy';

@Injectable()
export class CombinedAuthGuard implements CanActivate {
  async canActivate(context: ExecutionContext): Promise<boolean> {
    try {
      
    const jwtAuthGuard = new JwtAuthGuard();
    const googleAuthGuard = new GoogleAuthGuard();
    const kakaoAuthGuard = new KakaoAuthGuard();

    const request = context.switchToHttp().getRequest();
    const jwtAuthCanActivate = await jwtAuthGuard.canActivate(request);
    const googleAuthCanActivate = await googleAuthGuard.canActivate(
      request,
    );
    const kakaoAuthCanActivate = await kakaoAuthGuard.canActivate(request);

    return (
      !!googleAuthCanActivate || !!jwtAuthCanActivate || !!kakaoAuthCanActivate
    );

  } catch (error) {
      console.log(error);
  }
  }
}

I have tried to get context type and then apply checks on the basis of it. But I was not able to get request. Basically I want to get request from the context.

  • Which NestJS version are you currently using? You can get that info by running the following command: `nest info`. If you are running a version lower than 7, please upgrade and it should work, the code looks ok to me – GoranLegenda Aug 16 '23 at 13:06

1 Answers1

1

You're passing request to the jwtAtuhGuard.canActivate(), the googleAuthGuard.canActivate(), and the kakaoAuthGuard.canActivate(). Instead, you should pass context. Assuming that these are both guards that implements CanActivate, you're passing in the wrong value.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147