{"size": 1328, "ext": "js", "max_stars_repo_path": "schemas/User.js", "max_stars_repo_name": "sedobrengocce/ffonline", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "schemas/User.js", "max_issues_repo_name": "sedobrengocce/ffonline", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "schemas/User.js", "max_forks_repo_name": "sedobrengocce/ffonline", "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": 22.5084745763, "max_line_length": 72, "alphanum_fraction": 0.484939759}
'use strict';
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

// Thanks to http://blog.matoski.com/articles/jwt-express-node-mongoose/

// set up a mongoose model
const user = new mongoose.Schema({
    name: {
        type: String,
        unique: true,
        required: true
    },
    password: String,
    oauth:{
        type: {
            token: String,
            facebook: String,
            twitter: String,
            google: String,
        }
    },
    role: {
        type: String,
        enum: ['ff_admin', 'user']
    }
});

user.pre('save', function (next) {
    let user = this;
    if (this.isModified('password') || this.isNew) {
        bcrypt.genSalt(10, function (err, salt) {
            if (err) {
                return next(err);
            }
            bcrypt.hash(user.password, salt, function (err, hash) {
                if (err) {
                    return next(err);
                }
                user.password = hash;
                next();
            });
        });
    } else {
        return next();
    }
});

user.methods.comparePassword = function (passw, cb) {
    bcrypt.compare(passw, this.password, function (err, isMatch) {
        if (err) {
            return cb(err);
        }
        cb(null, isMatch);
    });
};

module.exports = user;
