Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie auth don't work? #145

Open
pavellzubkov opened this issue Feb 1, 2019 · 1 comment
Open

Cookie auth don't work? #145

pavellzubkov opened this issue Feb 1, 2019 · 1 comment

Comments

@pavellzubkov
Copy link

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:https://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:
@Morched23MJ
Copy link

Morched23MJ commented Apr 12, 2022

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:https://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:https://localhost:5984';
var couchServer = nano({ url: COUCHDB_URL, requestDefaults: { auth: { username: 'admin', password: 'admin' } } });

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants