<commit_before>'use strict';

const AbstractService = require('kermit/Service');
const LocalStrategy = require('passport-local').Strategy;
const passport = require('passport');

class AuthService extends AbstractService {
  constructor(serviceManager) {
    super(serviceManager);

    this.passport = new passport.Authenticator();
  }

  bootstrap() {
    this.accountService = this.serviceManager.get('accountService');
    this.registerCredentialsStrategy();
    this.initializeUserSessionSerialization();

    return this;
  }

  initializeUserSessionSerialization() {
    this.passport.serializeUser((user, callback) => {
      callback(null, user._id);
    });

    this.passport.deserializeUser((id, callback) => {
      this.accountService.findById(id, (err, user) => {
        if (user) {
          user.id = id;
        }

        callback(err, user)
      });
    });
  }

  registerCredentialsStrategy() {
    this.passport.use('credentials', new LocalStrategy({
      usernameField: 'email',
    }, (username, password, done) => {
        this.accountService.authenticateWithCredentials({
          email: username,
          password: password
        }, (err, user) => {
          if (err || !user) {
            return void done(null, false, { message: 'Invalid credentials.' });
          }

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

  requireUser() {
    return (req, res, next) => {
      if (req.user) {
        return void next();
      }

      res.redirect('/signin');
    };
  }

  authenticate(strategy, options) {
    return this.passport.authenticate(strategy, options);
  }

  registerMiddleware(server) {
    server.use(this.passport.initialize());
    server.use(this.passport.session());
    server.use((req, res, next) => {
      res.locals.user = req.user;
      next();
    });
  }
}

module.exports = AuthService;<commit_msg>Inject targetUrl for empowering proper after signin redirect.<commit_after>'use strict';

const AbstractService = require('kermit/Service');
const LocalStrategy = require('passport-local').Strategy;
const passport = require('passport');

class AuthService extends AbstractService {
  constructor(serviceManager) {
    super(serviceManager);

    this.passport = new passport.Authenticator();
  }

  bootstrap() {
    this.accountService = this.serviceManager.get('accountService');
    this.registerCredentialsStrategy();
    this.initializeUserSessionSerialization();

    return this;
  }

  initializeUserSessionSerialization() {
    this.passport.serializeUser((user, callback) => {
      callback(null, user._id);
    });

    this.passport.deserializeUser((id, callback) => {
      this.accountService.findById(id, (err, user) => {
        if (user) {
          user.id = id;
        }

        callback(err, user)
      });
    });
  }

  registerCredentialsStrategy() {
    this.passport.use('credentials', new LocalStrategy({
      usernameField: 'email',
    }, (username, password, done) => {
        this.accountService.authenticateWithCredentials({
          email: username,
          password: password
        }, (err, user) => {
          if (err || !user) {
            return void done(null, false, { message: 'Invalid credentials.' });
          }

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

  requireUser() {
    return (req, res, next) => {
      if (req.user) {
        return void next();
      }

      let redirectUrl = '/signin';

      if (req.url !== '/signin') {
        redirectUrl += `?targetUrl=${req.url}`;
      }

      res.redirect(redirectUrl);
    };
  }

  authenticate(strategy, options) {
    return this.passport.authenticate(strategy, options);
  }

  registerMiddleware(server) {
    server.use(this.passport.initialize());
    server.use(this.passport.session());
    server.use((req, res, next) => {
      res.locals.user = req.user;
      next();
    });
  }
}

module.exports = AuthService;
{'fraction_non_alphanumeric': 0.14405487804878048, 'fraction_numerical': 0.0, 'mean_word_length': 2.710650329877474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '5595178', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1132, 'n_words': 210}