se agregaron caso-esp, cust-alum, helpers
This commit is contained in:
+51
-20
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user