Actualizar CRUD módulo de tipos de combustibles
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import bcrypt from "bcrypt";
|
||||
import jwt from "jsonwebtoken";
|
||||
import userModel from "../user/user.model";
|
||||
import auditLogModel from "../auditLogs/auditLog.model";
|
||||
|
||||
export class AuthService {
|
||||
|
||||
@ -18,8 +19,13 @@ export class AuthService {
|
||||
throw new Error("Usuario no encontrado");
|
||||
}
|
||||
|
||||
const validPassword =
|
||||
await bcrypt.compare(
|
||||
//Validar si el usuario está activo
|
||||
if(!user.active){
|
||||
throw new Error("Usuario inactivo")
|
||||
}
|
||||
|
||||
//Validar contraseña
|
||||
const validPassword =await bcrypt.compare(
|
||||
password,
|
||||
user.passwordHash
|
||||
);
|
||||
@ -31,6 +37,17 @@ export class AuthService {
|
||||
user.lastLogin = new Date();
|
||||
await user.save();
|
||||
|
||||
//Registrar login en auditoría
|
||||
|
||||
await auditLogModel.create({
|
||||
userId: user._id,
|
||||
module:"Authentication",
|
||||
action:"LOGIN",
|
||||
description:"El usuario inició sesión.",
|
||||
status:"EXITOSO"
|
||||
|
||||
});
|
||||
|
||||
const role = user.roleId as any;
|
||||
|
||||
const token = jwt.sign(
|
||||
@ -38,8 +55,7 @@ export class AuthService {
|
||||
|
||||
id:user._id,
|
||||
roleId: user.roleId,
|
||||
|
||||
roleName:role.roleName
|
||||
roleName: role.roleName
|
||||
},
|
||||
|
||||
process.env.JWT_SECRET as string,
|
||||
@ -59,10 +75,26 @@ export class AuthService {
|
||||
|
||||
roleId: role._id,
|
||||
|
||||
rolename: role.roleName,
|
||||
roleName: role.roleName,
|
||||
|
||||
active: user.active
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async logout (
|
||||
userId:string
|
||||
){
|
||||
await auditLogModel.create({
|
||||
userId,
|
||||
module:"Authentication",
|
||||
action:"LOGOUT",
|
||||
description:"El usuario cerró sesión.",
|
||||
status:"EXITOSO"
|
||||
});
|
||||
|
||||
return{
|
||||
message:"Sesión cerrada exitosamente"
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user