Using nano.auth correctly
Asked Answered
M

1

5

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):

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' }

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?

Moorings answered 16/4, 2014 at 4:41 Comment(0)
M
7

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) -> 
Moorings answered 16/4, 2014 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.