couchdb-nano icon indicating copy to clipboard operation
couchdb-nano copied to clipboard

Cookie auth don't work?

Open pavellzubkov opened this issue 7 years ago • 1 comments

Can't get session details from couch db (cookies don't set)

Expected Behavior

Request with cookies.

Current Behavior

Request without cookies. Session response with null name

Possible Solution

?

Steps to Reproduce (for bugs)

`const nano = require('nano')({ url: 'http://localhost:5984', requestDefaults: { jar: true } })

const username = 'jan'

const userpass = 'apple'

export function dbtestauth () { nano.auth(username, userpass).then((body) => { console.log('Body - ' + JSON.stringify(body)) }) } //Body - {"ok":true,"name":"jan","roles":[]}

export function dbtestsession () { nano.session().then((body) => { console.log(JSON.stringify(body)) }) } //{"ok":true,"userCtx":{"name":null,"roles":[]},"info":{"authentication_db":"_users","authentication_handlers":["cookie","default"]}} `

Context

Your Environment

  • Version used: 8,0,0
  • Browser Name and version: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 YaBrowser/19.1.0.2644 Yowser/2.5 Safari/537.36
  • Operating System and version (desktop or mobile): Win 10 desktop
  • Link to your project:

pavellzubkov avatar Feb 01 '19 19:02 pavellzubkov

For future visitors

For some reason, the documentation is not really clear on this auth thingy.

There are basically two ways to authenticate per my little experience using nano:

  1. Use nano.auth method:
const nano = require('nano');
const COUCHDB_URL = process.env.COUCHDB_URL || 'http://localhost:5984';
var couchServer = nano({ url: COUCHDB_URL });

couchServer.auth(
    'admin',
    'admin',
    function (err, response, headers) {
        console.log(err, response, headers)

        // set cookie to the returned headers cookie
        couchServer = nano({ url: COUCHDB_URL, cookie: headers['set-cookie'] })

        // do authenticated work here
        couchServer.db.create('test1', function (err) {
            if (err) {
                console.error(err);
            }
        });
    })

  1. Use requestDefaults property when initializing nano object to set authentication credentials:
const nano = require('nano');
const COUCHDB_URL = process.env.COUCHDB_URL || 'http://localhost:5984';
var couchServer = nano({ url: COUCHDB_URL, requestDefaults: { auth: { username: 'admin', password: 'admin' } } });

// do authenticated work here
couchServer.db.list().then(console.log)

Morched23MJ avatar Apr 12 '22 07:04 Morched23MJ