ForbesLindesay/connect-roles

Strategies not running as advertised in the readme

Closed this issue · 2 comments

Consider this example:

roles.use(function(req, action) {
  console.log('STRATEGY 1');
  if (!req.user.isAuthenticated) {
    console.log('RETURNING:', action === 'login');
    return action === 'login';
  } else {
    return null;
  }
});

roles.use(function(req) {
  console.log('STRATEGY 2');
  if (req.user.roles.indexOf('admin') >= 0) {
    console.log('RETURNING: true');
    return true;
  } else {
    return false;
  }
});

In my console I get the following:

STRATEGY 1
RETURNING: true
STRATEGY 2
TypeError: Array.prototype.indexOf called on null or undefined

The readme tells me that if a strategy returns either true or false, all subsequent strategies are ignored and the user is considered "allowed" or "denied". According to this example, that's not what's happening. I return true from the first one, and it runs the next one anyway (failing because no user is logged in).

Great, thanks!