Search code examples
node.jsauthenticationcouchdbcouchdb-nano

Authentication with node.js, nano and CouchDB


Is there a way to change the config parameters in nano after initialization? I'd like to init nano with:

nano = require('nano')('http://127.0.0.1:5984')

and later change user and password, after a user submits the login form. I always get an error:

nano.cfg.user = params.user.name
TypeError: Cannot set property 'user' of undefined

Or should I fork nano and write an auth function to adjust the values?


Solution

  • The authentication can be send as part of the http header:

    if(cfg.user && cfg.pass) {
      req.headers['Authorization'] = "Basic " + new Buffer(cfg.user+":"+cfg.pass).toString('base64');
    }
    

    The username and password can be set with a 'auth'-function:

    function auth_db(user, password, callback) {
      cfg.user = user;
      cfg.pass = password;
      return relax({db: "_session", method: "GET"}, callback);
    }