Authorization Middleware
Overview
The guards.js file provides middleware for authorizing requests based on JSON Web Tokens (JWTs) and user roles. It ensures that only authenticated and authorized users can access certain routes.
Dependencies
jsonwebtoken: For verifying JWTs.../helper/logger.js: The custom logger module.../services/user/user.services.js: For fetching user data from the database.
SYSTEM_ROLES
A constant array of the valid roles in the system: admin, student, and teacher.
verifyJWT(req)
This function verifies the JWT from the request headers.
Functionality
- It checks if the
authorizationheader exists and is a string. - It extracts the token from the header.
- It uses
jwt.verify()to verify the token with the secret from theJWT_SECRETenvironment variable. - If the token is valid, it attaches the decoded user information to the
req.userobject and returnstrue. - If the token is invalid or missing, it returns
false.
isAuthorized(allowedRoles)
This is a middleware function that checks if a user is authorized to access a route.
Parameters
allowedRoles: An array of roles that are allowed to access the route.
Functionality
- Verify JWT: It calls
verifyJWT()to check for a valid token. If the token is invalid, it sends a401 Unauthorizedresponse. - Check User: It fetches the user from the database using the ID from the JWT. If the user is not found, it sends a
401 Unauthorizedresponse. - Check System Role: It checks if the user's role is one of the
SYSTEM_ROLES. If not, it sends a403 Forbiddenresponse. - Check Allowed Role: It checks if the user's role is included in the
allowedRolesarray. If not, it sends a403 Forbiddenresponse. - Call Next Middleware: If all checks pass, it calls
next()to pass control to the next middleware in the stack.
Usage
import { isAuthorized } from "./guards.js";
// This route can only be accessed by admins
router.get("/admin", isAuthorized(["admin"]), (req, res) => {
res.send("Welcome, admin!");
});
// This route can be accessed by admins and teachers
router.get("/dashboard", isAuthorized(["admin", "teacher"]), (req, res) => {
res.send("Welcome to the dashboard!");
});
Exports
isAuthorized: The middleware function for authorization.