Question

I have a node application written in express.js with passport for authentication and connect-roles for users. I also have a group table that I would like to connect to connect-roles. Is there any documentation on how to go about this. I want connect-roles to use my group table rows for assigning roles.

Was it helpful?

Solution

connect-roles doesn't define any storing mechanisms. it simply injects into the authenticated user you have in session object. Storing the strings equivalent to your roles you test for in connect-roles is up to you. You will normally authenticate the user first (which gives you the user object and his roles) and then do authorize (where connect-roles gets executed). Passport middleware should be used before connect-roles (as per connect-roles documentation)

In my case, I added a roles collection to my user model and took care of retrieving that from my database. You still need to implement the role test function and that's where you reference your User model roles property as you defined it.

Example:

roles.use(function (req, action) {
  if (req.isAuthenticated()){
    if (req.user.securityRoles.indexOf('admin') >= 0) return true; //admins can access all pages
    else return req.user.securityRoles.indexOf(action) >= 0;
  }
});

then you protect a route like this:

app.get('/offers', roles.is('offer.read'), offers.index);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top