Files
yudao-ui-admin-vben/apps/backend-mock/src/modules/auth/jwt.strategy.ts
2024-06-30 14:09:44 +08:00

27 lines
769 B
TypeScript

import type { JwtConfig, JwtPayload } from '@/types';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(configService: ConfigService) {
super({
ignoreExpiration: false,
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.get<JwtConfig>('jwt').secret,
});
}
async validate(payload: JwtPayload) {
console.log('jwt strategy validate payload', payload);
return {
id: payload.id,
roles: payload.roles,
username: payload.username,
};
}
}