Joey Yi Zhao
Joey Yi Zhao

Reputation: 42604

How to authenticate `couchdb` when connecting to it from `pouchdb` in nodejs?

CouchDB is running in the server and I am using pouchdb in a nodejs. I have trouble to connecting to CouchDB and I always got 401 Unauthorized error. Below is the code I am using:

const db = new PouchDB(url, {
    ajax: {
      withCredentials: false,
      headers: {
        authorization: 'signed 4252342xxxx ...',
      }
    }
  });

The error I got is: { message: 'Unauthorized', status: 401, name: 'unknown' }

Is it the correct way to do authentication?

Upvotes: 2

Views: 551

Answers (1)

IanC
IanC

Reputation: 883

As M-I pointed out, your method of getting the database does not match the PouchDB API. This is the example from the manual that shows how to add headers to the request:

var db = new PouchDB('http://example.com/dbname', {
  fetch: function (url, opts) {
    opts.headers.set('X-Some-Special-Header', 'foo');
    return PouchDB.fetch(url, opts);
  }
});

You do not seem to be using username/password, but if you are the "auth" option can be added to the options parameter.

Upvotes: 1

Related Questions