29 lines
846 B
TypeScript
29 lines
846 B
TypeScript
// jwt.strategy.ts
|
|
import { Injectable, ForbiddenException } from '@nestjs/common';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
export interface JwtPayload {
|
|
id: string;
|
|
usuario: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor(private configService: ConfigService) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: configService.get<string>('JWT_SECRET')!,
|
|
});
|
|
}
|
|
|
|
async validate(payload: JwtPayload) {
|
|
if (!payload || !payload.id || !payload.usuario) {
|
|
throw new ForbiddenException('Token inválido o corrupto');
|
|
}
|
|
return { id: payload.id, usuario: payload.usuario };
|
|
}
|
|
}
|