<commit_before>var util = require('util')
  , actions = require('../context/http/actions')
  , Context = require('../context/http/context')


// TODO: Implement support for authenticting with multiple strategies.
module.exports = function authenticate(name, options, callback) {
  if (!callback && typeof options === 'function') {
    callback = options;
    options = {};
  }
  options = options || {};
  
  return function authenticate(req, res, next) {
    var passport = this;
    var delegate = {};
    delegate.success = function(user, extra) {
      if (callback) {
        return callback(null, user, extra);
      }
      
      req.logIn(user, options, function(err) {
        if (err) { return next(err); }
        // TODO: Add an option to automatically redirect on success.
        next();
      });
    }
    delegate.fail = function() {
      if (callback) {
        return callback(null, false);
      } else if (options.failureRedirect) {
        return res.redirect(options.failureRedirect)
      }
      // When failure handling is not delegated to the application, the default
      // is to respond with 401 Unauthorized.  Note that the WWW-Authenticate
      // header will be set according to the strategies in use (see
      // actions#fail).
      res.statusCode = 401;
      res.end('Unauthorized');
    }
    
    // Get the strategy, which will be used as prototype from which to create
    // a new instance.  Action functions will then be bound to the strategy
    // within the context of the HTTP request/response pair.
    var prototype = passport._strategy(name);
    if (!prototype) { return next(new Error('no strategy registered under name: ' + name)); }
    
    var strategy = Object.create(prototype);
    var context = new Context(delegate, req, res, next);
    augment(strategy, actions, context);
    
    strategy.authenticate(req);
  }
}


function augment(strategy, actions, ctx) {
  for (var method in actions) {
    strategy[method] = actions[method].bind(ctx);
  }
}
<commit_msg>Add successRedirect option to authenticate middleware.<commit_after>var util = require('util')
  , actions = require('../context/http/actions')
  , Context = require('../context/http/context')


// TODO: Implement support for authenticting with multiple strategies.
module.exports = function authenticate(name, options, callback) {
  if (!callback && typeof options === 'function') {
    callback = options;
    options = {};
  }
  options = options || {};
  
  return function authenticate(req, res, next) {
    var passport = this;
    var delegate = {};
    delegate.success = function(user, extra) {
      if (callback) {
        return callback(null, user, extra);
      }
      
      req.logIn(user, options, function(err) {
        if (err) { return next(err); }
        if (options.successRedirect) {
          return res.redirect(options.successRedirect);
        }
        next();
      });
    }
    delegate.fail = function() {
      if (callback) {
        return callback(null, false);
      } else if (options.failureRedirect) {
        return res.redirect(options.failureRedirect);
      }
      // When failure handling is not delegated to the application, the default
      // is to respond with 401 Unauthorized.  Note that the WWW-Authenticate
      // header will be set according to the strategies in use (see
      // actions#fail).
      res.statusCode = 401;
      res.end('Unauthorized');
    }
    
    // Get the strategy, which will be used as prototype from which to create
    // a new instance.  Action functions will then be bound to the strategy
    // within the context of the HTTP request/response pair.
    var prototype = passport._strategy(name);
    if (!prototype) { return next(new Error('no strategy registered under name: ' + name)); }
    
    var strategy = Object.create(prototype);
    var context = new Context(delegate, req, res, next);
    augment(strategy, actions, context);
    
    strategy.authenticate(req);
  }
}


function augment(strategy, actions, ctx) {
  for (var method in actions) {
    strategy[method] = actions[method].bind(ctx);
  }
}

{'fraction_non_alphanumeric': 0.11514271891630382, 'fraction_numerical': 0.002902757619738752, 'mean_word_length': 2.742081447963801, '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': '4997655', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1119, 'n_words': 370}