25 lines
760 B
TypeScript
25 lines
760 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor(private readonly configService: ConfigService) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: configService.get<string>('JWT_SECRET') || 'default-secret-key',
|
|
});
|
|
}
|
|
|
|
async validate(payload: any) {
|
|
return {
|
|
sub: payload.sub,
|
|
username: payload.username,
|
|
roles: payload.roles,
|
|
accountSequence: payload.accountSequence,
|
|
};
|
|
}
|
|
}
|