강의

멘토링

로드맵

Inflearn Community Q&A

yanghw09115601's profile image
yanghw09115601

asked

Slack Clone Coding [Backend with NestJS + TypeORM]

passport 관련 질문드립니다

Written on

·

224

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 에러가 나는데 이유를 잘모르겠습니다.

nodejsexpressNestJSTypeORM

Answer 1

0

zerocho님의 프로필 이미지
zerocho
Instructor

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

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

yanghw09115601's profile image
yanghw09115601

asked

Ask a question