ultima versión
This commit is contained in:
@@ -7,16 +7,13 @@ import { AuthService } from './auth.service';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule, // Ya es global, pero lo puedes dejar
|
||||
JwtModule.registerAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) => ({
|
||||
secret: config.get('JWT_SECRET'),
|
||||
signOptions: { expiresIn: '1d' },
|
||||
}),
|
||||
}),
|
||||
JwtModule.register({
|
||||
secret: process.env.JWT_SECRET
|
||||
})
|
||||
|
||||
|
||||
],
|
||||
providers: [AuthService],
|
||||
exports: [AuthService, JwtModule],
|
||||
})
|
||||
export class AuthModule {}
|
||||
export class AuthModule { }
|
||||
|
||||
@@ -6,20 +6,33 @@ export class AuthService {
|
||||
constructor(private jwtService: JwtService) { }
|
||||
|
||||
generarToken(payload: any) {
|
||||
console.log('JWT_SECRET:', process.env.JWT_SECRET);
|
||||
return this.jwtService.sign(payload, {
|
||||
secret: process.env.JWT_SECRET,
|
||||
expiresIn: '1d',
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
verificarToken(token: string) {
|
||||
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);
|
||||
|
||||
|
||||
try {
|
||||
return this.jwtService.verify(token, {
|
||||
const result = await this.jwtService.verify(token, {
|
||||
secret: process.env.JWT_SECRET,
|
||||
});
|
||||
} catch {
|
||||
return null;
|
||||
console.log('Resultado de verificación:', result);
|
||||
return result;
|
||||
|
||||
} catch (error) {
|
||||
|
||||
|
||||
throw new Error('Token inválido o expirado');
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user