강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

devdev님의 프로필 이미지
devdev

작성한 질문수

Slack 클론 코딩[백엔드 with NestJS + TypeORM]

passport 관련 질문드립니다

작성

·

226

0

controller

@ApiBody({
  schema: {
    properties: {
      email: { type: 'string' },
      password: { type: 'string' }
    }
  }
})
@UseGuards(LocalAuthGuard)
@Post('login')
async login(@Req() req) {
  console.log('user controller');
  return this.authService.login(req.user);
}

local.strategy

constructor(private authService: AuthService) {
  super();
}

async validate(email: string, password: string): Promise<any> {
  console.log('local strategy');
  console.log(`${email} , ${password}`);
  const user = await this.authService.validateUser(email, password);
  if (!user) {
    throw new UnauthorizedException();
  }
  return user;
}

local-auth.guard

async canActivate(context: ExecutionContext): Promise<boolean> {
  console.log(context);
  const can = await super.canActivate(context);
  console.log('can', can);
  if (can) {
    const request = context.switchToHttp().getRequest();
    console.log('login for cookie');
    await super.logIn(request);
  }

  return true;
}

이렇게 구성되어있는데

canActivate에서 401 에러가 나는데 이유를 잘모르겠습니다.

답변 1

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

로그인 시 에러가 나는 건가요? 아니면 로그인 후에 다른 요청에서 에러가 나는 건가요?

validateUser가 통과하는지 console.log(user)도 해보세요.

devdev님의 프로필 이미지
devdev

작성한 질문수

질문하기