Search code examples
authenticationsynchronizationcouchbasepouchdbgateway

Authentication for multiple databases in couchbase sync gateway


How can I authenticate for more than one database simultaneously using Couchbase sync gateway?

I'm developing a mobile app where I use pouchdb that synchronizes via couchbase sync gateway with a couchbase server. I have to databases that should be synchronized: "messages" and "profiles".

The Sync works really well, but I have one problem: I only can authenticate for ONE database at the same time, but not for both simultaneously: I'm using custom authentication as described here which is achieved by calling the "/database1/_session" endpoint. This then returns a cookie that logs me in for database 1. If I now want to synchronize the second database too, I make a call to "/database2/_session" which overrides the first cookie, i.e. I'm now able to synchronize the second database, but not the first anymore.

Is there a way to enable authentication for more than one database? Is there a way to create global users, i.e. users that are not valid just for one database? Or is there another way to solve this problem?

Thank you in advance!


Solution

  • ok, after hours of trying to write proxies that manipulate the cookies etc. I found a much easier solution:

    In my server app, I can set a token as password for the user for all databases, which I can send to the app on login. The app can then just use http basic authentication:

      this.db.sync(this.cfg.couchbase + 'messages', {
        live: true,
        retry: true,
        ajax: {
          headers: {
              'Authorization': 'Basic ' + btoa(this.userID + ':' + token)
          }
        }
      }).on('change', this.changed.bind(this))
        .on('error', err => console.error(err));
    });