728x90

- 설치 

yarn add @nestjs/jwt

- 적용

@Module({
  imports: [
    TypeOrmModule.forFeature([
      Auth, //
    ]),
    JwtModule.register({}),
  ],
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule {}

빈 객체가 들어간 이유는 access와 refresh의 설정을 다르게 할 것이기 때문

 

const refreshToken = this.jwtService.sign(
      { sub: userNumber },
      {
        secret: this.configService.get<string>('REFRESHTOKEN_SECRET'),
        expiresIn: '24h',
      },
    );

비동기 필요하면 sign 대신에 signAsync

 

const refreshTokenSecret = this.configService.get<string>(
      'REFRESH_TOKEN_SECRET',
    );

return {
      refreshToken: await this.jwtService.signAsync(
        {
          sub: user.id,
          role: user.role,
          type: 'refresh',
        },
        {
          secret: refreshTokenSecret,
          expiresIn: '24h',
        },
      ),
    }

- JWT란?

  • 무상태 (Stateless) 인증에 사용된다.
  • 인터넷으로 전송할만큼 작으며 인증에필요한 모든 정보가 자체적으로 담겨있다.
  • Header, Payload, Signature 세개의 구간으로 이루어져있다.
  • 표준화됨 클레임이 존재한다. (iss: 발급, exp: 만료, sub: 식별자(id), aud: 대상(admin))
  • 인증 (Authentication), 인가 (Authorization)에 효율적이다.

- passport

https://springdream0406.tistory.com/185

 

passport

- 설치yarn add @nestjs/passport passport passport-local //passport-jwtyarn add --dev @types/passport-local //jwt

springdream0406.tistory.com

 

728x90

'코딩 > Nest.js' 카테고리의 다른 글

Middleware  (0) 2024.10.22
passport  (0) 2024.10.22
Mapped Type  (0) 2024.10.22
Pipe  (2) 2024.10.22
환경변수 ConfigModule (+ TypeORM DB 설정)  (0) 2024.10.21

+ Recent posts