Attaching User To Request Object In NodeJS/Express Middleware
Middleware attaches user to request object in Node.js/Express, making it accessible for role-based access control. `userIsAuthenticatedMiddleware` checks token validity and sets `req.user` if authenticated.
Middleware to attach a user to the request object because nodejs/express does not do that for you out of the box. Say i have a controller that need to check whether a user is of certain role in order to let them access a certain resource. Then this middleware will be helpful as it makes a user available to this function. Then i can do something like user.role === <some_role> ? do_something: do_this Here is how i might implement it. /** * @param {Request} req * @param {Response} res */ const userIsAuthenticatedMiddleware = async (req, res, next) => { const token = req.headers["...