DEV Community

Margaret W.N
Margaret W.N

Posted on

Day 35

I spent countless hours trying to implementing user permissions. It's still not making much sense. Authorization is complex and tutorials don't seem to be working out for me. Away from that i created an attribute: token where I'll save the token in the database. I also added a middleware to find users by id before performing the get, update and delete functions. Probably the only code that was working today.
Code in user controllers

const findUser = (req, res, next) => {
  User.findById(req.params.userId, (err, user) => {
    if (err) {
      return res.send(err);
    }
    if (user) {
      req.user = user;
      return next();
    }
    return res.sendStatus(404);
  })
};
Enter fullscreen mode Exit fullscreen mode

Code in user routes

router.use('/users/:userId', userController.findUser);
Enter fullscreen mode Exit fullscreen mode

We live to try another day.
Day 35

Latest comments (1)

Collapse
 
mtee profile image
Margaret W.N

😅😅, Yes you will, and with the speed your learning with, it will be sooner that you expect, you'll see it's not as complex and your imagining right now.