{"size": 1082, "ext": "js", "max_stars_repo_path": "models/User.js", "max_stars_repo_name": "YarosJ/solv1", "max_stars_repo_stars_event_min_datetime": "2018-05-13T12:37:28.000Z", "max_stars_repo_stars_event_max_datetime": "2018-05-13T12:37:39.000Z", "max_issues_repo_path": "models/User.js", "max_issues_repo_name": "YarosJ/solv1", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "models/User.js", "max_forks_repo_name": "YarosJ/solv1", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.0816326531, "max_line_length": 72, "alphanum_fraction": 0.6802218115}
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

const { Schema } = mongoose;
const bcrypt = require('bcryptjs');
const passportLocalMongoose = require('passport-local-mongoose');

/**
 * @description :: A model definition.  Represents a database for users.
 * @type {*|Mongoose.Schema}
 */

const UserSchema = new Schema({
  email: {
    type: String,
    unique: true,
    required: true,
  },
  hashedPassword: {
    type: String,
    required: true,
  },
  refreshToken: {
    type: String,
    required: false,
  },
  createdAt: { type: Date },
});

UserSchema.virtual('password')
  .set(function (password) {
    this.salt = bcrypt.genSaltSync(10);
    this.hashedPassword = this.encryptPassword(password);
  });

UserSchema.methods = {
  encryptPassword(password) {
    return bcrypt.hashSync(password, this.salt);
  },

  validPassword(password) {
    return bcrypt.compareSync(password, this.hashedPassword);
  },
};

UserSchema.plugin(passportLocalMongoose, { usernameField: 'email' });

module.exports = mongoose.model('User1', UserSchema);
