27 lines
866 B
TypeScript
27 lines
866 B
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
|
|
import { Environment } from '../../../config/env.validation';
|
|
import { AccessPayload } from '../application/auth.types';
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor(config: ConfigService<Environment, true>) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: config.getOrThrow('JWT_SECRET'),
|
|
algorithms: ['HS256'],
|
|
});
|
|
}
|
|
|
|
validate(payload: AccessPayload): AccessPayload {
|
|
if (payload.type !== 'access') {
|
|
throw new UnauthorizedException({ code: 'UNAUTHORIZED' });
|
|
}
|
|
return payload;
|
|
}
|
|
}
|