ForbesLindesay / connect-roles

Provides dynamic roles based authorisation for node.js connect and express servers.
749 stars 62 forks source link

Separate rules in modules #52

Closed crdil closed 8 years ago

crdil commented 8 years ago

I'm using connect-roles with passportjs and express. For now my code (snippets of it) looks like this..

// roles.js
import connectRoles from 'connect-roles';

export const roles = new connectRoles();

// routes.js
import { roles } from '../libs/roles';
...
...

roles.use((req, action) => {
  if (!req.isAuthenticated()) return action === 'access home page';
});

roles.use('access admin page', (req, action) => {
  if (req.user.role === 'admin') {
    return true;
  }
});

.get(passport.authenticate('jwt', { session: false }), roles.can('access admin page'), controller.TestController.getTest);

// app.js
...
...
import { roles } from '../libs/roles';

app.use(roles.middleware());

But since I want to be able to use the same rules on other routes then the ones in routes.js I need to put them into a module, but I'm not sure how I can do that. If anyone can point me in the right direction I would really appreciate that.

Thanks

crdil commented 8 years ago

Nevermind, got it sorted

yassin-mokni commented 7 years ago

Hi @crdil , can you tell please how you solve this ?