• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

401 unauthorized문제

24.06.18 21:51 작성 조회수 76

0

안녕하세요 제로초님 LocalGuard에서 can으로 넘어가지가 않아서 질문드립니다.

import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
  async canActivate(context: ExecutionContext): Promise<boolean> {
    const request = context.switchToHttp().getRequest();
    console.log('LocalAuthGuard canActivate - request.body:', request); // 추가 로그

    console.log('LocalAuthGuard canActivate - before super.canActivate'); // 추가 로그
    let can = await super.canActivate(context);
    console.log(can);

    if (can) {
      const request = context.switchToHttp().getRequest();
      console.log('login for cookie');
      console.log('request.user:', request.user); // 추가 로그
      await super.logIn(request);
    }

    return true;
  }
}

여기서 보면 requsetbody에는 request.body: { teacherId: 'hee', password: '12534' } 이렇게 제가 설정한값이 들어가고

UnauthorizedException: Unauthorized

at LocalAuthGuard.handleRequest (/Users/mac/Desktop/sideProject/piano-erp-back/node_modules/@nestjs/passport/dist/auth.guard.js:60:30)

at /Users/mac/Desktop/sideProject/piano-erp-back/node_modules/@nestjs/passport/dist/auth.guard.js:44:124

at /Users/mac/Desktop/sideProject/piano-erp-back/node_modules/@nestjs/passport/dist/auth.guard.js:83:24

at allFailed (/Users/mac/Desktop/sideProject/piano-erp-back/node_modules/passport/lib/middleware/authenticate.js:110:18)

at attempt (/Users/mac/Desktop/sideProject/piano-erp-back/node_modules/passport/lib/middleware/authenticate.js:183:28)

at strategy.fail (/Users/mac/Desktop/sideProject/piano-erp-back/node_modules/passport/lib/middleware/authenticate.js:314:9)

at Strategy.authenticate (/Users/mac/Desktop/sideProject/piano-erp-back/node_modules/passport-local/lib/strategy.js:75:17)

at attempt (/Users/mac/Desktop/sideProject/piano-erp-back/node_modules/passport/lib/middleware/authenticate.js:378:16)

at authenticate (/Users/mac/Desktop/sideProject/piano-erp-back/node_modules/passport/lib/middleware/authenticate.js:379:7)

at /Users/mac/Desktop/sideProject/piano-erp-back/node_modules/@nestjs/passport/dist/auth.guard.js:88:3 {

response: { message: 'Unauthorized', statusCode: 401 },

status: 401,

options: {}

}

undefined

teacher.decorator.ts undefined

이렇게 에러가나는데 원인은 잘모르겠습니다. 혹시 어떤부분을 살펴봐야할까요? 추가로 저기이외에 strategy쪽에는 console찍은게 아에 안들어옵니다.

답변 1

답변을 작성해보세요.

0

윤희중님의 프로필

윤희중

질문자

2024.06.18

죄송합니다. strategy쪽에 filed가 오타가났네요..

채널톡 아이콘