<commit_before>var GoogleStrategy = require('passport-google').Strategy;
var User = require('../models/user').User;

module.exports.setupAuth = function(app, passport) {
  passport.serializeUser(function(user, done) {
    done(null, user._id);
  });

  passport.deserializeUser(function(obj, done) {
    User.findOne({ _id: obj }, function(err, result) {
      done(err, result);
    });
  });

  function processAuth(identifier, profile, done) {
    User.findOne({ openid: identifier }, function(err, result) {
      if (result == null) {
        var user = new User({
          openid: identifier,
          name: profile.displayName,
          email: profile.emails[0].value,
          createdAt: Date.now()
        });
        user.save(function(err, user) {
          if (err) {
            throw err;
          } else {
            done(err, user);
          }
        });
      } else {
        done(err, result);
      }
    });
  }

  passport.use(new GoogleStrategy({
    returnURL: app.realm + 'auth/google/return',
    realm: app.realm
  }, function(identifier, profile, done) {
    identifier = 'gl-' + identifier.substr(identifier.lastIndexOf('id=') + 3);
    processAuth(identifier, profile, done);
  }));

  app.get('/login', function(req, res) {
    res.redirect('/auth/google');
  })

  app.get('/auth/google', passport.authenticate('google'), function(req, res) {
    res.send('Unknown failure.');
  });

  app.get('/auth/google/return', passport.authenticate('google', { failureRedirect: '/login' }), function(req, res) {
    res.redirect('/');
  });
};<commit_msg>Add logout<commit_after>var GoogleStrategy = require('passport-google').Strategy;
var User = require('../models/user').User;

module.exports.setupAuth = function(app, passport) {
  passport.serializeUser(function(user, done) {
    done(null, user._id);
  });

  passport.deserializeUser(function(obj, done) {
    User.findOne({ _id: obj }, function(err, result) {
      done(err, result);
    });
  });

  function processAuth(identifier, profile, done) {
    User.findOne({ openid: identifier }, function(err, result) {
      if (result == null) {
        var user = new User({
          openid: identifier,
          name: profile.displayName,
          email: profile.emails[0].value,
          createdAt: Date.now()
        });
        user.save(function(err, user) {
          if (err) {
            throw err;
          } else {
            done(err, user);
          }
        });
      } else {
        done(err, result);
      }
    });
  }

  passport.use(new GoogleStrategy({
    returnURL: app.realm + 'auth/google/return',
    realm: app.realm
  }, function(identifier, profile, done) {
    identifier = 'gl-' + identifier.substr(identifier.lastIndexOf('id=') + 3);
    processAuth(identifier, profile, done);
  }));

  app.get('/auth/google', passport.authenticate('google'), function(req, res) {
    res.send('Unknown failure.');
  });

  app.get('/auth/google/return', passport.authenticate('google', { failureRedirect: '/login' }), function(req, res) {
    res.redirect('/');
  });

  app.get('/login', function(req, res) {
    res.redirect('/auth/google');
  })

  app.get('/logout', function(req, res) {
    req.logOut();
    res.redirect('/');
  })
};
{'fraction_non_alphanumeric': 0.17352579852579852, 'fraction_numerical': 0.0012285012285012285, 'mean_word_length': 2.7137970353477767, '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': '5345473', 'n_tokens_mistral': 1019, 'n_tokens_neox': 1001, 'n_words': 190}