2025-05-02 16:06:58 -06:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { JwtService } from '@nestjs/jwt';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class AuthService {
|
2025-06-27 20:43:08 -06:00
|
|
|
constructor(private jwtService: JwtService) { }
|
2025-05-02 16:06:58 -06:00
|
|
|
|
|
|
|
|
generarToken(payload: any) {
|
2025-06-27 21:49:50 -06:00
|
|
|
console.log('JWT_SECRET:', process.env.JWT_SECRET);
|
2025-05-02 16:06:58 -06:00
|
|
|
return this.jwtService.sign(payload, {
|
2025-06-27 20:43:08 -06:00
|
|
|
secret: process.env.JWT_SECRET,
|
2025-06-27 21:49:50 -06:00
|
|
|
|
2025-05-02 16:06:58 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 21:49:50 -06:00
|
|
|
async verificarToken(token: string) {
|
|
|
|
|
console.log('JWT_SECRET:', process.env.JWT_SECRET);
|
|
|
|
|
console.log('JWT_SECRET:', process.env.JWT_SECRET);
|
|
|
|
|
console.log('Verificando token:', token);
|
2025-06-27 22:23:48 -06:00
|
|
|
console.log('retirar estos console.log');
|
2025-06-27 20:43:08 -06:00
|
|
|
|
2025-05-02 16:06:58 -06:00
|
|
|
try {
|
2025-06-27 21:49:50 -06:00
|
|
|
const result = await this.jwtService.verify(token, {
|
2025-06-27 20:43:08 -06:00
|
|
|
secret: process.env.JWT_SECRET,
|
2025-05-02 16:06:58 -06:00
|
|
|
});
|
2025-06-27 21:49:50 -06:00
|
|
|
console.log('Resultado de verificación:', result);
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error('Token inválido o expirado');
|
2025-05-02 16:06:58 -06:00
|
|
|
}
|
2025-06-27 21:49:50 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-02 16:06:58 -06:00
|
|
|
}
|
|
|
|
|
}
|