4

したがって、ここの nano docs によると: https://github.com/dscape/nano#using-cookie-authentication、次の方法でユーザーを認証できます: (coffeescript で)

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

私はこれで大丈夫で、正しい応答を得ることができます。私が問題を抱えているのは、その後どうするかです。私の最初の考えは、次のことを行うことでした(布団を介して管理者のユーザー名とパスワードの設定を使用する場合):

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.]

nano.auth から返されたエラー、本文、およびヘッダーをデバッグすると、次のようになります。

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

私のテストでは、次のことも試しましたが、どちらもうまくいかないようです

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.]

私が間違っている/誤解している場所を誰か指摘できますか?

4

1 に答える 1

6

私はそれを理解しました-そして私は今ばかげていると感じています. node.js は非同期であることを常に覚えておいてください。

これを行う正しい方法:

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) -> 
于 2014-04-16T06:22:10.903 に答える