25 lines
735 B
TypeScript
25 lines
735 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { Role } from './role.enum';
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor(configService: ConfigService) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: configService.get<string>('JWT_SECRET') as string,
|
|
});
|
|
}
|
|
|
|
validate(payload: { sub: string; username: string; roles: Role[] }) {
|
|
return {
|
|
userId: payload.sub,
|
|
username: payload.username,
|
|
roles: payload.roles,
|
|
};
|
|
}
|
|
}
|