28 lines
886 B
TypeScript
28 lines
886 B
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { Strategy, ExtractJwt, StrategyOptions } from 'passport-jwt';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor(configService: ConfigService) {
|
|
const secret = configService.get<string>('JWT_SECRET');
|
|
if (!secret) {
|
|
throw new Error('JWT_SECRET is not defined');
|
|
}
|
|
|
|
const opts: StrategyOptions = {
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: secret,
|
|
};
|
|
|
|
super(opts);
|
|
}
|
|
|
|
async validate(payload: any) {
|
|
// Devuelve lo que estará disponible en Request.user
|
|
return { userId: payload.sub, email: payload.email, origen: payload.tipo, tipo: payload.tipo, };
|
|
}
|
|
}
|