30 lines
711 B
TypeScript
30 lines
711 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
interface JwtPayload {
|
|
sub: string;
|
|
email: string;
|
|
role: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor(configService: ConfigService) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: configService.get('jwt.secret'),
|
|
});
|
|
}
|
|
|
|
async validate(payload: JwtPayload) {
|
|
return {
|
|
id: payload.sub,
|
|
email: payload.email,
|
|
role: payload.role,
|
|
};
|
|
}
|
|
}
|