Below is my sample code referring to this doc:
https://github.com/dscape/nano#using-cookie-authentication
var nano = require('nano')('http://localhost:5984')
const username = 'test'
const userpass = '123123'
const callback = console.log // this would normally be some callback
const cookies = {} // store cookies, normally redis or something
nano.auth(username, userpass, function (err, body, headers)
{
if (err)
{
return callback(err);
}
if (headers && headers['set-cookie'])
{
cookies[username] = headers['set-cookie'];
console.log(cookies)
}
if (!err)
{
callback("success ", "it worked");
}
});
It works perfectly when my require_valid_user = false
{ test:
[ 'AuthSession=dGVzdDo1QTQ1RTFENDoVzjtA5v82S3bJRyI9Mz_J9dXrWA; Version=1;
Expires=Thu, 28-Dec-2017 22:43:56 GMT; Max-Age=600; Path=/; HttpOnly' ] }
//success it worked
However, when I set require_valied_user = true
, it fails with the following error:
name: 'Error', error: 'unauthorized', reason: 'Authentication required.', scope: 'couch', statusCode: 401, request: { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8', accept: 'application/json' }, uri: 'http://localhost:5984/_session', body: 'name=test&password=123123' }, headers: { 'www-authenticate': 'Basic realm="server"', date: 'Fri, 29 Dec 2017 06:42:10 GMT', 'content-type': 'application/json', 'cache-control': 'must-revalidate', statusCode: 401, uri: 'http://localhost:5984/_session' }, errid: 'non_200',
description: 'couch returned 401' }
I am not sure what I am doing wrongly as it suppose to work both ways?