se agregaron caso-esp, cust-alum, helpers

This commit is contained in:
evenegas
2025-05-26 15:29:47 -06:00
parent b786976565
commit ee1e8d18c5
24 changed files with 1252 additions and 151 deletions
+51 -20
View File
@@ -1,31 +1,62 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { bcrypt } from 'bcrypt';
@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 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),
};
}
async comparar(password,dbPassword){
if (!bcrypt.compareSync(password, dbPassword)){
return false
}else{
return true;
}
}
async encriptar(password){
bcrypt.hashSync(password, Number(process.env.SALT_ROUNDS))
}
async generarPassword(){
const length = 8;
const charset =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let password = '';
for (let i = 0, n = charset.length; i < length; ++i){
password += charset.charAt(Math.floor(Math.random() * n))
}
async jwtCreate(idUsuario: number, idTipoUsuario:number) {
const payload = { Usuario:idUsuario , tipoUsuario: idTipoUsuario };
return {
access_token: this.jwtService.sign(payload),
};
}
return password;
}
}