mirror of
https://github.com/openmhz/trunk-server.git
synced 2026-11-09 23:37:31 -05:00
107 lines
2.0 KiB
JavaScript
107 lines
2.0 KiB
JavaScript
// Defining a User Model in mongoose
|
|
// Code modified from https://github.com/sahat/hackathon-starter
|
|
const bcrypt = require("bcrypt");
|
|
const mongoose = require("mongoose");
|
|
|
|
|
|
|
|
|
|
const UserSchema = new mongoose.Schema({
|
|
local: {
|
|
name: String,
|
|
email: String,
|
|
password: String
|
|
},
|
|
email: {
|
|
type: String,
|
|
unique: true,
|
|
lowercase: true
|
|
},
|
|
password: String,
|
|
screenName: {
|
|
type: String,
|
|
unique: true,
|
|
lowercase: true
|
|
},
|
|
firstName: String,
|
|
lastName: String,
|
|
location: String,
|
|
email: String,
|
|
resetPasswordToken: String,
|
|
resetPasswordTTL: Date,
|
|
confirmEmail: {
|
|
type: Boolean,
|
|
default: false
|
|
},
|
|
confirmEmailToken: String,
|
|
confirmEmailTTL: Date,
|
|
admin: {
|
|
type: Boolean,
|
|
default: false
|
|
},
|
|
terms: {
|
|
type: Number,
|
|
default: 0
|
|
},
|
|
ver: {
|
|
type: Number,
|
|
default: 1.1
|
|
},
|
|
sysCount: Number,
|
|
lastLogin: { type : Date, default: Date.now }
|
|
})
|
|
|
|
/**
|
|
* Password hash middleware.
|
|
*/
|
|
/*
|
|
UserSchema.pre("save", function(next) {
|
|
var user = this
|
|
if (!user.isModified("password")) return next()
|
|
bcrypt.genSalt(8, (err, salt) => {
|
|
if (err) return next(err)
|
|
bcrypt.hash(user.password, salt, null, (err, hash) => {
|
|
if (err) return next(err)
|
|
user.password = hash
|
|
user.local.password = hash;
|
|
next()
|
|
})
|
|
})
|
|
})*/
|
|
|
|
UserSchema.pre("save", function(next) {
|
|
if(!this.isModified("password")) {
|
|
return next();
|
|
}
|
|
hashed = bcrypt.hashSync(this.password, 8);
|
|
this.password = hashed;
|
|
this.local.password = hashed;
|
|
|
|
next();
|
|
});
|
|
|
|
/*
|
|
Defining our own custom document instance method
|
|
*/
|
|
/*
|
|
UserSchema.methods = {
|
|
comparePassword: function(candidatePassword, cb) {
|
|
bcrypt.compare(candidatePassword, this.local.password, (err, isMatch) => {
|
|
if (err) return cb(err)
|
|
cb(null, isMatch)
|
|
})
|
|
}
|
|
}*/
|
|
|
|
UserSchema.methods.comparePassword = function(plaintext, callback) {
|
|
return callback(null, bcrypt.compareSync(plaintext, this.local.password));
|
|
};
|
|
|
|
|
|
/**
|
|
* Statics
|
|
*/
|
|
UserSchema.statics = {}
|
|
|
|
module.exports = mongoose.model("User", UserSchema)
|