I am trying to protect certain routes with user roles (some routes accept more than one role). When I list the accepted role in the route handler, only the first user role that is listed is permitted access - I need to allow all roles that are listed to have access.
Here is the auth middleware file that decodes the JWT tokens (auth.js
):
import jwt from "jsonwebtoken";
const auth = (req, res, next) => {
const token = req.header("x-auth-token");
if (!token)
return res
.status(401)
.send({ message: "Access denied. No token provided!" });
try {
const decoded = jwt.verify(token, "SomethingPrivate");
req.user = decoded;
next();
} catch (ex) {
res.status(400).send({ message: "Invalid Token." });
}
};
export const superAdmin = (req, res, next) => {
const token = req.header("x-auth-token");
if (!auth && !token)
return res.status(401).send({ message: "Access denied." });
const decoded = jwt.verify(token, "SomethingPrivate");
if (auth && decoded.role === "superAdmin") {
res.status(200);
next();
} else {
res.status(400).send({ message: "Access denied!" });
}
};
export const admin = (req, res, next) => {
const token = req.header("x-auth-token");
if (!auth && !token)
return res.status(401).send({ message: "Access denied." });
const decoded = jwt.verify(token, "SomethingPrivate");
if (auth && decoded.role === "admin") {
res.status(200);
next();
} else {
res.status(400).send({ message: "Access denied!" });
}
};
export const teacher = (req, res, next) => {
const token = req.header("x-auth-token");
if (!auth || !token)
return res.status(401).send({ message: "Access denied." });
const decoded = jwt.verify(token, "SomethingPrivate");
if (auth && decoded.role === "teacher") {
res.status(200);
next();
} else {
res.status(400).send({ message: "Access denied!" });
}
};
export const student = (req, res, next) => {
const token = req.header("x-auth-token");
if (!auth || !token)
return res.status(401).send({ message: "Access denied." });
const decoded = jwt.verify(token, "SomethingPrivate");
if (auth && decoded.role === "student") {
res.status(200);
next();
} else {
res.status(400).send({ message: "Access denied!" });
}
};
export const parent = (req, res, next) => {
const token = req.header("x-auth-token");
if (!auth) return res.status(401).send({ message: "Access denied." });
const decoded = jwt.verify(token, "SomethingPrivate");
if (auth && decoded.role === "parent") {
res.status(200);
next();
} else {
res.status(400).send({ message: "Access denied!" });
}
};
Here is the router file (userRoute.js
):
import express from "express";
import {
superAdmin,
admin,
teacher,
student,
parent,
} from "../middleware/auth.js";
const router = express.Router();
import {
view,
find,
me,
create,
edit,
update,
viewUser,
} from "../controllers/userController.js";
// Routes
router.get("/", [superAdmin, admin], view); //The route I am struggling with at the moment//
router.post("/", find);
router.get("/me", me);
router.post("/create", superAdmin, create);
router.get("/edituser/:userID", edit);
router.post("/edituser/:userID", [], update);
router.get("/viewuser/:userID", viewUser);
export { router as user };
And finally the data that is inserted into the JWT payload when signing takes place on login and stored in response header:
const token = jwt.sign(
{
userID: result[0].userID,
firstName: result[0].firstName,
lastName: result[0].lastName,
email: result[0].email,
role: result[0].role,
},
In the userRoute.js
file I tried using the pipe operator between each accepted role for that route but I'm can't seem to get the roles to be seen as boolean values.
Any help would be appreciated! (this is the backend that will be paired with a React frontend in the near future.)
CodePudding user response:
Array of middleware functions always works as if the routes were specified serially. It means, if you invoke res.send()
in one of the middleware functions, all next functions in the array are already not used.
I would propose it this way, for example:
Middleware for route accessible by parent, student, admin
export const parent = (req, res, next) => {
const token = req.header("x-auth-token");
if (!auth) return res.status(401).send({ message: "Access denied." });
const decoded = jwt.verify(token, "SomethingPrivate");
if (auth && ["parent", "student", "admin"].includes(decoded.role)) {
res.status(200);
} else {
res.status(400).send({ message: "Access denied!" });
}
};
Middleware for route accessible by admin
only:
export const parent = (req, res, next) => {
const token = req.header("x-auth-token");
if (!auth) return res.status(401).send({ message: "Access denied." });
const decoded = jwt.verify(token, "SomethingPrivate");
if (auth && ["admin"].includes(decoded.role))) {
res.status(200);
} else {
res.status(400).send({ message: "Access denied!" });
}
};