diff --git a/backend/.env b/backend/.env index 082cb8f..5d3a242 100644 --- a/backend/.env +++ b/backend/.env @@ -2,7 +2,7 @@ PORT=5000 MONGO_URI=mongodb://localhost:27017/sistema_combustible -JWT_SECRET=super_secret_key +JWT_SECRET=ControlCombustible26@ SQL_SERVER=ABIGAILV\SQLEXPRESS diff --git a/backend/src/app.ts b/backend/src/app.ts index 997bec9..f5e7fe5 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -1,7 +1,8 @@ import express from "express"; import cors from "cors"; -import roleRoutes from "./modules/roles/role.routes"; -import userRoutes from "./modules/user/user.routes"; +import roleRouter from "./modules/roles/role.routes"; +import userRouter from "./modules/user/user.routes"; +import authRouter from "./modules/auth/auth.routes"; const app = express(); @@ -9,8 +10,9 @@ app.use(cors()); app.use(express.json()); -app.use("/api/roles", roleRoutes); -app.use("/api/users", userRoutes) +app.use("/api/roles", roleRouter); +app.use("/api/user", userRouter); +app.use("/api/auth", authRouter); app.use(express.urlencoded({ diff --git a/backend/src/modules/auth/auth.controller.ts b/backend/src/modules/auth/auth.controller.ts new file mode 100644 index 0000000..82133d9 --- /dev/null +++ b/backend/src/modules/auth/auth.controller.ts @@ -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 + }); + + } + } +} \ No newline at end of file diff --git a/backend/src/modules/auth/auth.middleware.ts b/backend/src/modules/auth/auth.middleware.ts new file mode 100644 index 0000000..7a64c7b --- /dev/null +++ b/backend/src/modules/auth/auth.middleware.ts @@ -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" + }); + } +}; \ No newline at end of file diff --git a/backend/src/modules/auth/auth.routes.ts b/backend/src/modules/auth/auth.routes.ts new file mode 100644 index 0000000..47d1f04 --- /dev/null +++ b/backend/src/modules/auth/auth.routes.ts @@ -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; \ No newline at end of file diff --git a/backend/src/modules/auth/auth.service.ts b/backend/src/modules/auth/auth.service.ts new file mode 100644 index 0000000..749f124 --- /dev/null +++ b/backend/src/modules/auth/auth.service.ts @@ -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 + }; + } +} \ No newline at end of file diff --git a/backend/src/modules/roles/role.routes.ts b/backend/src/modules/roles/role.routes.ts index e17e5a2..7253994 100644 --- a/backend/src/modules/roles/role.routes.ts +++ b/backend/src/modules/roles/role.routes.ts @@ -2,12 +2,12 @@ import { Router } from "express"; import { RoleController } from "./role.controller"; -const roleRoutes = Router(); +const roleRouter = Router(); const controller = new RoleController(); -roleRoutes.get("/", controller.getAll); +roleRouter.get("/", controller.getAll); -roleRoutes.post("/", controller.create); +roleRouter.post("/", controller.create); -export default roleRoutes ; \ No newline at end of file +export default roleRouter ; \ No newline at end of file diff --git a/backend/src/modules/user/user.routes.ts b/backend/src/modules/user/user.routes.ts index df28ea9..9a71e15 100644 --- a/backend/src/modules/user/user.routes.ts +++ b/backend/src/modules/user/user.routes.ts @@ -1,19 +1,20 @@ import { Router } from "express"; import { UserController } from "./user.controller"; +import { authMiddleware } from "../auth/auth.middleware"; -const userRoutes = Router(); +const userRouter = Router(); 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; \ No newline at end of file +export default userRouter; \ No newline at end of file diff --git a/backend/src/modules/user/user.service.ts b/backend/src/modules/user/user.service.ts index 1510cb4..4ffca18 100644 --- a/backend/src/modules/user/user.service.ts +++ b/backend/src/modules/user/user.service.ts @@ -1,3 +1,4 @@ +import bcrypt from "bcrypt"; import userModel from "./user.model"; export class UserService { @@ -20,6 +21,13 @@ export class UserService { async create( data:any ) { + const passwordHash = await bcrypt.hash( + data.passwordHash, + 10 + ); + + data.passwordHash = passwordHash; + return await userModel.create(data); }