Using nano.auth correctly

So According to the nano docs here: https://github.com/dscape/nano#using-cookie-authentication, you can authenticate a user via the following: (In coffeescript)

nano = require('nano') 'http://localhost:5984'
nano.auth username, password

I’m fine with this and I can get the correct response, what I’m having trouble with is what to do afterwards. What my initial thought was is to do the following (when using the admins username and password setup via futon):

  • express 3.0 and passport authentication
  • Passport.js + Express.js forward user to original destination after authenticating
  • Adding a cookie value on Socket.IO
  • Sails.js Waterlock How to override login action (How to write custom login action)
  • Can I add cookies to a webpack dev server proxy?
  • req.session undefined and req.session.user_id not working
  • nano = require('nano') 'http://localhost:5984'
    nano.auth username, password
    nano.db.create 'test', (err, body) -> #err here is always [Error: you are not a server admin.]
    

    If I debug the err, body and headers returned from nano.auth I get:

    err: null
    body: { ok: true, name: null, roles: [ '_admin' ] }
    header: { 'set-cookie': [ 'AuthSession=bm9kZV9hZG1pbjo1MzRFMTEwQzoGNe9XUrMu5IKYPK3BP3GQyHeRWQ; Version=1; Path=/; HttpOnly' ],
        date: 'Wed, 16 Apr 2014 05:11:40 GMT',
        'content-type': 'text/plain; charset=utf-8',
        'cache-control': 'must-revalidate',
        'status-code': 200,
        uri: 'http://127.0.0.1:5984/_session' }
    

    EDIT: In my testing I’ve also tried the following that doesn’t seem to work either

    nano = require('nano') "#{prefix}://#{security.couchdb.url}"
    cookie = ''
    nano.auth security.couchdb.admin_user.username, security.couchdb.admin_user.password, (err, response, headers) ->
        console.log "Nano_admin Setup"
        console.log err
        console.log response
        console.log headers
        cookie = headers['set-cookie']
    nano = require('nano')
        url: "#{prefix}://#{security.couchdb.url}"
        cookie: "AuthSession=#{cookie}"
    nano.db.create 'test', (err, body) -> #err here is always [Error: you are not a server admin.]
    

    Can anyone point out where I’ve gone wrong/misunderstood something please?

  • ldap nodejs active directory authentication
  • Sails.js : How to return access token after user registration using sails-generate-auth?
  • LTI Launch Authentication with Node.js
  • Express.js 4 - use middleware for authentication before static files
  • Sails.js API passport.js authentication
  • Simple REST Authentication Strategy?
  • One Solution collect form web for “Using nano.auth correctly”

    I figured it out – and I feel silly now. Allways remember that node.js is async.

    Correct way of doing this:

    nano = require('nano') 'http://localhost:5984'
    nano.auth username, password, (err, response, headers) ->
        nano = require('nano')
            url: 'http://localhost:5984'
            cookie: headers['set-cookie']
        nano.db.create 'test', (err, body) -> 
    
    Node.js is the Best Javascript runtime in the world.