33 lines
935 B
TypeScript
33 lines
935 B
TypeScript
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 {
|
|
idPersona: string;
|
|
user: string;
|
|
userType: 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.idPersona || !payload.user || !payload.userType) {
|
|
throw new ForbiddenException('Token inválido o corrupto');
|
|
}
|
|
return {
|
|
idPersona: payload.idPersona,
|
|
user: payload.user,
|
|
userType: payload.userType,
|
|
};
|
|
}
|
|
}
|