Files
api-AT/src/user/jwt.strategy.ts
T

29 lines
846 B
TypeScript
Raw Normal View History

2025-10-02 17:03:25 -06:00
// jwt.strategy.ts
import { Injectable, ForbiddenException } from '@nestjs/common';
2025-09-12 13:18:11 -04:00
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { ConfigService } from '@nestjs/config';
2025-10-02 17:03:25 -06:00
export interface JwtPayload {
id: string;
usuario: string;
}
2025-09-12 13:18:11 -04:00
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private configService: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
2025-10-02 17:03:25 -06:00
secretOrKey: configService.get<string>('JWT_SECRET')!,
2025-09-12 13:18:11 -04:00
});
}
2025-10-02 17:03:25 -06:00
async validate(payload: JwtPayload) {
if (!payload || !payload.id || !payload.usuario) {
throw new ForbiddenException('Token inválido o corrupto');
}
2025-09-19 17:15:05 -06:00
return { id: payload.id, usuario: payload.usuario };
2025-09-12 13:18:11 -04:00
}
}