se hizo el service cuestionario-alumno
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
JwtModule.register({
|
||||
secret: 'secretoSuperSeguro', // Usa un .env en producción
|
||||
signOptions: { expiresIn: '1h' },
|
||||
}),
|
||||
],
|
||||
providers: [AuthService],
|
||||
exports: [AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(private readonly jwtService: JwtService) {}
|
||||
|
||||
async jwtVerificar(token:string){
|
||||
try{
|
||||
const payload= this.jwtService.verify(token)
|
||||
return payload;
|
||||
}catch(err){
|
||||
if (err.name === 'TokenExpiredError') {
|
||||
throw new UnauthorizedException('El token ha expirado');
|
||||
} else if (err.name === 'JsonWebTokenError') {
|
||||
throw new UnauthorizedException('Token inválido');
|
||||
} else {
|
||||
throw new UnauthorizedException('Error al verificar el token');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
async jwtCreate(idUsuario: number, idTipoUsuario:number) {
|
||||
const payload = { Usuario:idUsuario , tipoUsuario: idTipoUsuario };
|
||||
return {
|
||||
access_token: this.jwtService.sign(payload),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user