- 로그인 이후 access/refresh token 생성 및 반환 로직 구현
This commit is contained in:
27
src/middleware/auth/jwt.strategy.ts
Normal file
27
src/middleware/auth/jwt.strategy.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { AccountRepo } from 'src/modules/account/account.repo';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(
|
||||
private readonly accountRepo: AccountRepo
|
||||
, private readonly configService: ConfigService
|
||||
, private readonly jwtService: JwtService
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
secretOrKey: configService.get<string>('JWT_SECRET')!
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
const account = await this.accountRepo.findById(payload.id);
|
||||
if (!account || account.length < 1) throw new UnauthorizedException();
|
||||
return account[0];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user