Implementar modulo de Auth
This commit is contained in:
@ -2,7 +2,7 @@ PORT=5000
|
|||||||
|
|
||||||
MONGO_URI=mongodb://localhost:27017/sistema_combustible
|
MONGO_URI=mongodb://localhost:27017/sistema_combustible
|
||||||
|
|
||||||
JWT_SECRET=super_secret_key
|
JWT_SECRET=ControlCombustible26@
|
||||||
|
|
||||||
SQL_SERVER=ABIGAILV\SQLEXPRESS
|
SQL_SERVER=ABIGAILV\SQLEXPRESS
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import roleRoutes from "./modules/roles/role.routes";
|
import roleRouter from "./modules/roles/role.routes";
|
||||||
import userRoutes from "./modules/user/user.routes";
|
import userRouter from "./modules/user/user.routes";
|
||||||
|
import authRouter from "./modules/auth/auth.routes";
|
||||||
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -9,8 +10,9 @@ app.use(cors());
|
|||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.use("/api/roles", roleRoutes);
|
app.use("/api/roles", roleRouter);
|
||||||
app.use("/api/users", userRoutes)
|
app.use("/api/user", userRouter);
|
||||||
|
app.use("/api/auth", authRouter);
|
||||||
|
|
||||||
|
|
||||||
app.use(express.urlencoded({
|
app.use(express.urlencoded({
|
||||||
|
|||||||
36
backend/src/modules/auth/auth.controller.ts
Normal file
36
backend/src/modules/auth/auth.controller.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
|
import { AuthService } from "./auth.service";
|
||||||
|
|
||||||
|
const authService = new AuthService();
|
||||||
|
|
||||||
|
export class AuthController {
|
||||||
|
|
||||||
|
async login (
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
){
|
||||||
|
try {
|
||||||
|
|
||||||
|
const {
|
||||||
|
userName,
|
||||||
|
password
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
|
const result =
|
||||||
|
await authService.login(
|
||||||
|
userName,
|
||||||
|
password
|
||||||
|
);
|
||||||
|
|
||||||
|
res.json(result);
|
||||||
|
|
||||||
|
} catch(error:any){
|
||||||
|
|
||||||
|
res.status(401).json({
|
||||||
|
message:error.message
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
43
backend/src/modules/auth/auth.middleware.ts
Normal file
43
backend/src/modules/auth/auth.middleware.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
export const authMiddleware = (
|
||||||
|
req:Request,
|
||||||
|
res:Response,
|
||||||
|
next:NextFunction
|
||||||
|
) => {
|
||||||
|
|
||||||
|
const authHeader = req.headers.authorization;
|
||||||
|
|
||||||
|
if(!authHeader){
|
||||||
|
return res.status(401).json({
|
||||||
|
message:"Token requerido"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = authHeader.split(" ")[1];
|
||||||
|
|
||||||
|
if (!token)
|
||||||
|
{
|
||||||
|
return res.status(401).json({
|
||||||
|
message:"Token invalido"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
const decoded = jwt.verify (
|
||||||
|
token,
|
||||||
|
process.env.JWT_SECRET as string,
|
||||||
|
);
|
||||||
|
|
||||||
|
(req as any).user =
|
||||||
|
decoded;
|
||||||
|
|
||||||
|
next();
|
||||||
|
} catch{
|
||||||
|
return res.status(401).json({
|
||||||
|
message:"Token invalido"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
14
backend/src/modules/auth/auth.routes.ts
Normal file
14
backend/src/modules/auth/auth.routes.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
|
||||||
|
import { AuthController } from "./auth.controller";
|
||||||
|
|
||||||
|
const authRouter = Router();
|
||||||
|
|
||||||
|
const controller = new AuthController();
|
||||||
|
|
||||||
|
authRouter.post(
|
||||||
|
"/login",
|
||||||
|
controller.login
|
||||||
|
);
|
||||||
|
|
||||||
|
export default authRouter;
|
||||||
51
backend/src/modules/auth/auth.service.ts
Normal file
51
backend/src/modules/auth/auth.service.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import bcrypt from "bcrypt";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
import userModel from "../user/user.model";
|
||||||
|
|
||||||
|
export class AuthService {
|
||||||
|
|
||||||
|
async login (
|
||||||
|
userName:string,
|
||||||
|
password:string
|
||||||
|
){
|
||||||
|
const user =
|
||||||
|
await userModel.findOne({
|
||||||
|
userName
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new Error("Usuario no encontrado");
|
||||||
|
}
|
||||||
|
|
||||||
|
const validPassword =
|
||||||
|
await bcrypt.compare(
|
||||||
|
password,
|
||||||
|
user.passwordHash
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!validPassword){
|
||||||
|
throw new Error("Contraseña incorrecta");
|
||||||
|
}
|
||||||
|
|
||||||
|
user.lastLogin = new Date();
|
||||||
|
await user.save();
|
||||||
|
|
||||||
|
const token = jwt.sign(
|
||||||
|
{
|
||||||
|
|
||||||
|
id:user._id,
|
||||||
|
roleId: user.roleId
|
||||||
|
},
|
||||||
|
|
||||||
|
process.env.JWT_SECRET as string,
|
||||||
|
{
|
||||||
|
expiresIn:"8h"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
token,
|
||||||
|
user
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,12 +2,12 @@ import { Router } from "express";
|
|||||||
|
|
||||||
import { RoleController } from "./role.controller";
|
import { RoleController } from "./role.controller";
|
||||||
|
|
||||||
const roleRoutes = Router();
|
const roleRouter = Router();
|
||||||
|
|
||||||
const controller = new RoleController();
|
const controller = new RoleController();
|
||||||
|
|
||||||
roleRoutes.get("/", controller.getAll);
|
roleRouter.get("/", controller.getAll);
|
||||||
|
|
||||||
roleRoutes.post("/", controller.create);
|
roleRouter.post("/", controller.create);
|
||||||
|
|
||||||
export default roleRoutes ;
|
export default roleRouter ;
|
||||||
@ -1,19 +1,20 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
|
|
||||||
import { UserController } from "./user.controller";
|
import { UserController } from "./user.controller";
|
||||||
|
import { authMiddleware } from "../auth/auth.middleware";
|
||||||
|
|
||||||
const userRoutes = Router();
|
const userRouter = Router();
|
||||||
|
|
||||||
const controller = new UserController();
|
const controller = new UserController();
|
||||||
|
|
||||||
userRoutes.get("/", controller.getAll);
|
userRouter.get("/", authMiddleware, controller.getAll);
|
||||||
|
|
||||||
userRoutes.get("/:id", controller.getById);
|
userRouter.get("/:id", authMiddleware, controller.getById);
|
||||||
|
|
||||||
userRoutes.post("/", controller.create);
|
userRouter.post("/", authMiddleware, controller.create);
|
||||||
|
|
||||||
userRoutes.put("/:id", controller.update);
|
userRouter.put("/:id", controller.update);
|
||||||
|
|
||||||
userRoutes.delete("/:id", controller.detele);
|
userRouter.delete("/:id", controller.detele);
|
||||||
|
|
||||||
export default userRoutes;
|
export default userRouter;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
import bcrypt from "bcrypt";
|
||||||
import userModel from "./user.model";
|
import userModel from "./user.model";
|
||||||
|
|
||||||
export class UserService {
|
export class UserService {
|
||||||
@ -20,6 +21,13 @@ export class UserService {
|
|||||||
async create(
|
async create(
|
||||||
data:any
|
data:any
|
||||||
) {
|
) {
|
||||||
|
const passwordHash = await bcrypt.hash(
|
||||||
|
data.passwordHash,
|
||||||
|
10
|
||||||
|
);
|
||||||
|
|
||||||
|
data.passwordHash = passwordHash;
|
||||||
|
|
||||||
return await userModel.create(data);
|
return await userModel.create(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user