DEV Community

Fyp Services
Fyp Services

Posted on

Implementing JWT & Role-Based Access Control (Student, Supervisor, Admin) in a MERN FYP

Most FYP portals in Pakistani universities have the same three users: the student who submits, the supervisor who reviews, and the admin or coordinator who manages both. Most groups still build one login and then hide buttons in React for the other roles. Hiding a button is not access control — anyone with the API URL and a token can call the route directly. Here is the clean way to do JWT plus role-based access control (RBAC) in a MERN stack, the way an external examiner expects to see it.

The user model

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  passwordHash: { type: String, required: true },
  role: { type: String, enum: ["student", "supervisor", "admin"], required: true }
});
Enter fullscreen mode Exit fullscreen mode

One collection, one role field. Do not make three separate login systems — that is the first design question panels ask about, and one model with roles is the defensible answer. Hash passwords with bcrypt before saving; never store or return the plain password.

Issue the token at login

const jwt = require("jsonwebtoken");

function makeToken(user) {
  return jwt.sign(
    { id: user._id, role: user.role },
    process.env.JWT_SECRET,
    { expiresIn: "1d" }
  );
}
Enter fullscreen mode Exit fullscreen mode

Put the role inside the token so your middleware does not need a database call on every request. Keep the secret in an environment variable, never in the code you show in your report screenshots.

Middleware 1: is the user logged in?

function requireAuth(req, res, next) {
  const header = req.headers.authorization || "";
  const token = header.startsWith("Bearer ") ? header.slice(7) : null;
  if (!token) return res.status(401).json({ message: "Login required" });
  try {
    req.user = jwt.verify(token, process.env.JWT_SECRET);
    next();
  } catch (err) {
    return res.status(401).json({ message: "Invalid or expired token" });
  }
}
Enter fullscreen mode Exit fullscreen mode

Middleware 2: is this role allowed here?

function requireRole(...roles) {
  return (req, res, next) => {
    if (!roles.includes(req.user.role)) {
      return res.status(403).json({ message: "Not allowed for your role" });
    }
    next();
  };
}
Enter fullscreen mode Exit fullscreen mode

Use both on the routes

app.get("/api/proposals/mine", requireAuth, requireRole("student"), myProposals);
app.patch("/api/proposals/:id/review", requireAuth, requireRole("supervisor"), reviewProposal);
app.get("/api/admin/users", requireAuth, requireRole("admin"), listUsers);
Enter fullscreen mode Exit fullscreen mode

Now the rule lives on the server, in one place per route. The React side can still hide buttons for a cleaner UI, but the API stays safe even if someone calls it from Postman — which is exactly the test an examiner describes in the viva.

The viva questions this sets you up for

  • 401 vs 403: 401 means "we do not know who you are" (no or bad token); 403 means "we know who you are and your role cannot do this." Mixing these up is the most common slip.
  • Why does the token expire? A stolen token should not work forever; 1 day is a reasonable demo choice, and you can explain refresh tokens as future work.
  • Where do you check the role — frontend or backend? Backend, always. Frontend checks are only for display.
  • What stops a student from changing their role in the token? The signature. If they edit the payload, jwt.verify fails, because they do not have the secret.

Build the roles around your actual workflow — submission, review, approval — and the same code also gives you your use case diagram and your Chapter 3 access-control section almost for free.

Top comments (0)