Skip to content

Commit

Permalink
improved mongodb authentication
Browse files Browse the repository at this point in the history
Signed-off-by: Wouter Vroege <wouter@woutervroege.nl>
  • Loading branch information
woutervroege committed Feb 26, 2013
1 parent 8e7d7d9 commit 01745dd
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions lib/shared/backends/mongo.js
Expand Up @@ -6,30 +6,18 @@
* @author Wouter Vroege <wouter AT woutervroege DOT nl>
* @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
*/
exports.getConnection = function (options, callback) {
var options = options || {};
var mongo = require("mongodb"),
server = new mongo.Server(options.host || 'localhost', options.port || 27017, {
auto_reconnect: true
}),
db = new mongo.Db(options.db || 'jsdav', server, {
safe: true
});
db.open(function (err, db) {
if(options.username && options.password) {
db.authenticate(options.username, options.password, function(err) {
if(err) {
return callback(err)
}
else {
callback(null, db);
}
})
} else {
if(err)
return callback(err)
else
callback(null, db);
}
})
}
exports.getConnection = function(options, callback) {
MongoClient = require("mongodb").MongoClient,
options = options || {};

if (options.username && options.password)
connURL = "mongodb://" + options.username + ":" + options.password + "@" + options.host + ":" + options.port + "/" + options.db;
else
connURL = "mongodb://" + options.host + ":" + options.port + "/" + options.db;

MongoClient.connect(connURL, function(err, db) {
if(err)
return callback(err);
callback(null, db);
});
};

0 comments on commit 01745dd

Please sign in to comment.