MongoDB, Node.js and admin auth

There is a big challenge to make MongoDB driver to work with Node.js, but here is an cool and working example to do that:

Require:

var mongodb = require("mongodb"); //require node-mongodb-native
var settings = require('./mconfig'); //for settings

Connect to the admin database:

var mongoserver = new mongodb.Server(settings.mongo_host, settings.mongo_port, {});
admindb = new mongodb.Db('admin', mongoserver);	

Try to auth with admin cretentials:


exports.adminlogin = function(callback) {

		admindb.open(function (error, client) {
			
			admindb.authenticate(settings.mongo_user, settings.mongo_pass, function (err, val) {
		        if (err) {
		             callback(0);
		        } else {
		            	callback(1);
		          		         
		        }
   		 });

 	 });
	
	
};

It returns 1 on success and 0 on failure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.