-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
35 lines (30 loc) · 937 Bytes
/
users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
User model
represents a user of the wiki
*/
var Mongoose = require("mongoose"), Schema = Mongoose.Schema;
var passwordHash = require('password-hash');
function usernameValidator (v){
return v.length > 0;
};
var User = new Schema({
username : {type: String, index:true, validate: [usernameValidator, 'username must be at least 1 character long']},
password : {type: String, index:true},
role : {type: String}
});
User.static({
authenticate : function(username,password,callback){
this.findOne({username:username},function(err,doc){
console.log('findOne returned with '+err+doc);
if(err || !doc){
callback(false);
} else if(passwordHash.verify(password, doc.password)){
callback(doc);
}
else{ // password mismatch
callback(false);
}
})
}
});
Mongoose.model('User',User);