4

所以根据这里的纳米文档:https://github.com/dscape/nano#using-cookie-authentication,您可以通过以下方式对用户进行身份验证:(在咖啡脚本中)

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 回答