This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
userbase-mongo.js
62 lines (51 loc) · 1.6 KB
/
userbase-mongo.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* vim: set sw=2 ts=2 nocin si: */
/* Hey, why don't we just deprecate this module?
* Since we'll be using LDAP anyway, it sure is the right thing to do...
* Okay so this is formally deprecated. Or obsolete. Whatever.
*/
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/net9-auth');
mongoose.model('User', new mongoose.Schema({
username: { type: String, index: true },
password: String,
bio: String
}));
var User = mongoose.model('User');
exports.checkUser = function (username, callback) {
User.count({ username: username }, function (err, count) {
callback(count !== 0);
});
};
exports.create = function (userinfo, callback) {
var newUser = new User(userinfo);
newUser.save(function (err) {
if (err) callback(false, err);
else callback(true, newUser.toObject());
});
};
exports.getByName = function (username, callback) {
User.findOne({ username: username }, function (err, user) {
if (user === null) {
callback(false, 'no-such-user');
} else {
callback(true, user.toObject());
}
});
};
exports.authenticate = function (username, password, callback) {
User.findOne({ username: username }, function (err, user) {
if (user === null || user.password !== password) {
callback(false, 'user-pass-no-match');
} else {
callback(true, user.toObject());
}
});
};
exports.update = function (userinfo, callback) {
User.findOne({ username: userinfo.username }, function (err, user) {
user.merge(userinfo).save(function (err) {
if (err) callback(false, err);
else callback(true, user.toObject());
});
});
};