<commit_before>"use strict"; 

var express                 = require('../express-inherit'),
    utils                   = require('../utils'),
    current_absolute_url    = require('../middleware/route').current_absolute_url,
    base_url                = require('../middleware/route').base_url,
    passport                = require('passport'),
    popit                   = require('../popit'),
    Account                 = require('../schemas/account'),
    Permissions             = require('../schemas/permissions'),
    LocalStrategy           = require('passport-local').Strategy,
    BasicStrategy           = require('passport-http').BasicStrategy,
    legacyAccountUpgrade    = require('../middleware/legacy-account-upgrade');

// Legacy password check
passport.use('legacy', new LocalStrategy({
  usernameField: 'email',
  passReqToCallback: true
}, function(req, username, password, done) {
  // Find the user, check password, store in session
  var User = req.popit.model('User');
  User.findOne({ email: username }, function(err, user) {
    if (err) {
      return done(err);
    }
    if (!user) {
      return done(null, false, { message: 'Invalid email or password' });
    }

    utils.password_hash_compare(password, user.hashed_password, function(valid) {
      if (!valid) {
        return done(null, false, { message: 'Invalid email or password' });
      }

      done(null, user);
    });
  });
}));

passport.use('local', new LocalStrategy({
  usernameField: 'email',
  passReqToCallback: true
}, function(req, username, password, done) {
    var account = req.popit.accounts();
    account.findByUsername(username, function(err, user) {
        if (err) { return done(err); }

        if (user) {
            return user.authenticate(password, done);
        } else {
            return done(null, false, { message: 'Invalid email or password' });
        }
    });
}));

passport.use('legacy-basic', new BasicStrategy({
  passReqToCallback: true
}, function(req, username, password, done) {
  var User = req.popit.model('User');

  User.findOne({ email: username }, function(err, user) {
    if (err) {
      return done(err);
    }
    if (!user) {
      return done(null, false);
    }

    utils.password_hash_compare(
      password,
      user.hashed_password,
      function(valid) {
        if (!valid) {
          return done(null, false);
        }
        done(null, user);
      }
    );
  });
}));


passport.use('basic', new BasicStrategy({
  passReqToCallback: true
}, function(req, username, password, done) {
  var Account = req.popit.accounts();
  Account.findByUsername(username, function(err, user) {
    if (err) {
      return done(err);
    }
    if (!user) {
      return done(null, false);
    }
    user.authenticate(password, done);
  });
}));

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(req, id, done) {
  var User = req.popit.model('User');
  User.findById(id, function(err, user) {
    if (user) {
      return done(null, user);
    }

    req.popit.accounts().findById(id, done);
  });
});


var app = express();


var capture_common_parameters = function (req,res,next) {

  // If there is a redirect_to param use it to store the destination for
  // post auth
  var redirect_to = req.param('redirect_to');
  if (redirect_to) {
    req.session.post_login_redirect_to = redirect_to;
  }
  
  // Are we trying to login as a guest?
  res.locals.as_guest = req.param('as_guest') && req.popit.allow_guest_access() || false;
  res.locals.isUpgrading = typeof req.session.legacyInstanceId === 'string';

  next();
};

module.exports.capture_common_parameters = capture_common_parameters;

app.get('/login', capture_common_parameters, function (req,res) {

  // pass through some of the values
  res.locals.email  = req.param('email');
  res.locals.errors = [];

  res.render('login.html');<commit_msg>Remove User lookup from deserializeUser<commit_after>"use strict"; 

var express                 = require('../express-inherit'),
    utils                   = require('../utils'),
    current_absolute_url    = require('../middleware/route').current_absolute_url,
    base_url                = require('../middleware/route').base_url,
    passport                = require('passport'),
    popit                   = require('../popit'),
    Account                 = require('../schemas/account'),
    Permissions             = require('../schemas/permissions'),
    LocalStrategy           = require('passport-local').Strategy,
    BasicStrategy           = require('passport-http').BasicStrategy,
    legacyAccountUpgrade    = require('../middleware/legacy-account-upgrade');

// Legacy password check
passport.use('legacy', new LocalStrategy({
  usernameField: 'email',
  passReqToCallback: true
}, function(req, username, password, done) {
  // Find the user, check password, store in session
  var User = req.popit.model('User');
  User.findOne({ email: username }, function(err, user) {
    if (err) {
      return done(err);
    }
    if (!user) {
      return done(null, false, { message: 'Invalid email or password' });
    }

    utils.password_hash_compare(password, user.hashed_password, function(valid) {
      if (!valid) {
        return done(null, false, { message: 'Invalid email or password' });
      }

      done(null, user);
    });
  });
}));

passport.use('local', new LocalStrategy({
  usernameField: 'email',
  passReqToCallback: true
}, function(req, username, password, done) {
    var account = req.popit.accounts();
    account.findByUsername(username, function(err, user) {
        if (err) { return done(err); }

        if (user) {
            return user.authenticate(password, done);
        } else {
            return done(null, false, { message: 'Invalid email or password' });
        }
    });
}));

passport.use('legacy-basic', new BasicStrategy({
  passReqToCallback: true
}, function(req, username, password, done) {
  var User = req.popit.model('User');

  User.findOne({ email: username }, function(err, user) {
    if (err) {
      return done(err);
    }
    if (!user) {
      return done(null, false);
    }

    utils.password_hash_compare(
      password,
      user.hashed_password,
      function(valid) {
        if (!valid) {
          return done(null, false);
        }
        done(null, user);
      }
    );
  });
}));


passport.use('basic', new BasicStrategy({
  passReqToCallback: true
}, function(req, username, password, done) {
  var Account = req.popit.accounts();
  Account.findByUsername(username, function(err, user) {
    if (err) {
      return done(err);
    }
    if (!user) {
      return done(null, false);
    }
    user.authenticate(password, done);
  });
}));

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(req, id, done) {
  req.popit.accounts().findById(id, done);
});


var app = express();


var capture_common_parameters = function (req,res,next) {

  // If there is a redirect_to param use it to store the destination for
  // post auth
  var redirect_to = req.param('redirect_to');
  if (redirect_to) {
    req.session.post_login_redirect_to = redirect_to;
  }
  
  // Are we trying to login as a guest?
  res.locals.as_guest = req.param('as_guest') && req.popit.allow_guest_access() || false;
  res.locals.isUpgrading = typeof req.session.legacyInstanceId === 'string';

  next();
};

module.exports.capture_common_parameters = capture_common_parameters;

app.get('/login', capture_common_parameters, function (req,res) {

  // pass through some of the values
  res.locals.email  = req.param('email');
  res.locals.errors = [];

  res.render('login.html');
{'fraction_non_alphanumeric': 0.14755371472948486, 'fraction_numerical': 0.0, 'mean_word_length': 2.857713429855217, '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': '6369427', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2334, 'n_words': 520}