Files
quicklanch-server/src/app/middlewares/auth.ts
T
abumahid 0f7af70b90 ♻️ refactor(account, order, plan, profile, support, email): restructure application modules and enhance error handling
Updated Docker configuration, refactored middleware for improved error handling, and restructured account, order, plan, profile, and support modules, including their routes, services, and validations. Enhanced email processing queues and utilities for token generation, pagination, and response management to streamline the application architecture and enhance maintainability.
2026-04-21 03:12:39 +06:00

31 lines
918 B
TypeScript

import { NextFunction, Request, Response } from "express";
import { configs } from "../configs/index.js";
import { AppError } from "../utils/app_error.js";
import { jwtHelpers, JwtPayloadType } from "../utils/JWT.js";
type Role = "ADMIN" | "USER";
const auth = (...roles: Role[]) => {
return async (req: Request, res: Response, next: NextFunction) => {
try {
const token = req.headers.authorization || req.cookies.access_token;
if (!token) {
throw new AppError("You are not authorize!!", 401);
}
const verifiedUser = jwtHelpers.verifyToken(
token,
configs.jwt.access_token as string,
);
if (!roles.length || !roles.includes(verifiedUser.role)) {
throw new AppError("You are not authorize!!", 401);
}
req.user = verifiedUser as JwtPayloadType;
next();
} catch (err) {
next(err);
}
};
};
export default auth;