{"size": 1207, "ext": "js", "max_stars_repo_path": "server/passportConfig.js", "max_stars_repo_name": "sehnsucht13/code-drop", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/passportConfig.js", "max_issues_repo_name": "sehnsucht13/code-drop", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/passportConfig.js", "max_forks_repo_name": "sehnsucht13/code-drop", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.2391304348, "max_line_length": 75, "alphanum_fraction": 0.5608947804}
// const User = require("./user");
const bcrypt = require("bcryptjs");
const LocalStrategy = require("passport-local").Strategy;
const db = require("./models");

module.exports = function (passport) {
  passport.use(
    // eslint-disable-line consistent-return
    new LocalStrategy(async (username, password, done) => {
      const user = await db.Users.findOne({ where: { username } });

      if (user === null) {
        return done(null, false);
      }

      bcrypt.compare(password, user.dataValues.password, (err, result) => {
        if (err) {
          console.log("error loggin in user");
          return done(null, false);
        }
        if (result === true) {
          return done(null, user);
        }
        return done(null, false);
      });
      return undefined;
    })
  );

  passport.serializeUser((user, cb) => {
    cb(null, user.dataValues.id);
  });
  passport.deserializeUser(async (id, cb) => {
    try {
      const user = await db.Users.findOne({ where: { id } });
      const userInformation = {
        username: user && user.username,
        uid: user && user.id,
      };
      cb(null, userInformation);
    } catch (err) {
      cb(err, null);
    }
  });
};
