<commit_before>/**
 * Module dependencies.
 */

var page = require('page');
var Citizen = require('./model');
var log = require('debug')('democracyos:citizen');

/**
 * Instantiate and expose citizen
 */

var citizen = module.exports = new Citizen();

citizen.load("me");

/**
 * Force citizen's data if logged in
 * or redirect to '/' if not
 *
 * @param {Object} ctx page's context
 * @param {Function} next callback after load
 * @api private
 */

citizen.required = function(ctx, next) {
  log('Required in context %o', ctx);

  if ("unloaded" === citizen.state()) {
    setTimeout(loggedout, 0);
  } else if ("loading" === citizen.state()) {
    citizen.once('error', loggedout);
  }

  citizen.ready(function() {
    citizen.off('error', loggedout);
    next();
  });
};

/**
 * Load citizen's data if logged in
 *
 * @param {Object} ctx page's context
 * @param {Function} next callback after load
 * @api private
 */

citizen.optional = function(ctx, next) {
  ctx.citizen = citizen;
  citizen.load('me');
  citizen.once('error', function(err) {
    log('Found error %s', err);
    log('unregistered entering "%s"', ctx.path);
    next();
  });
  citizen.ready(function() {
    log('registered entering "%s"', ctx.path);
    next();
  });
};

/**
 * If staff let go middleware
 *
 * @param {Object} ctx
 * @param {Function} next
 * @api public
 */

citizen.isStaff = function isStaff(ctx, next) {
  citizen.ready(function onready() {
    if (citizen.staff) return next();
    page('/');
  });
}

/**
 * Log user logged out and redirect to '/'<commit_msg>[citizen] - Updated staff client middleware. Closes #286<commit_after>/**
 * Module dependencies.
 */

var page = require('page');
var Citizen = require('./model');
var log = require('debug')('democracyos:citizen');

/**
 * Instantiate and expose citizen
 */

var citizen = module.exports = new Citizen();

citizen.load("me");

/**
 * Force citizen's data if logged in
 * or redirect to '/' if not
 *
 * @param {Object} ctx page's context
 * @param {Function} next callback after load
 * @api private
 */

citizen.required = function(ctx, next) {
  log('Required in context %o', ctx);

  if ("unloaded" === citizen.state()) {
    setTimeout(loggedout, 0);
  } else if ("loading" === citizen.state()) {
    citizen.once('error', loggedout);
  }

  citizen.ready(function() {
    citizen.off('error', loggedout);
    next();
  });
};

/**
 * Load citizen's data if logged in
 *
 * @param {Object} ctx page's context
 * @param {Function} next callback after load
 * @api private
 */

citizen.optional = function(ctx, next) {
  ctx.citizen = citizen;
  citizen.load('me');
  citizen.once('error', function(err) {
    log('Found error %s', err);
    log('unregistered entering "%s"', ctx.path);
    next();
  });
  citizen.ready(function() {
    log('registered entering "%s"', ctx.path);
    next();
  });
};

/**
 * If staff let go middleware
 *
 * @param {Object} ctx
 * @param {Function} next
 * @api public
 */

citizen.isStaff = function isStaff(ctx, next) {
  ctx.citizen = citizen;
  citizen.load('me');
  citizen.once('error', function(err) {
    log('Found error %s', err);
    page('/');
  });
  citizen.ready(function() {
    if (!citizen.staff) return page('/');
    next();
  });
}

/**
 * Log user logged out and redirect to '/'
{'fraction_non_alphanumeric': 0.18333333333333332, 'fraction_numerical': 0.0015151515151515152, 'mean_word_length': 3.509562841530055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1741714', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1062, 'n_words': 299}