Averroes
Averroes

Reputation: 31

How can I sync PouchDB with CouchDB when pouchdb-authentication doesn't work with the .sync function?

I am able to log into couchDB from my node instance to the remote DB using the pouch-authentication plugin, but I'm not sure how to use the .sync method to replicate because it doesn't use the credentials when calling. On pouch-authentication's docs it simply says:

"But the pouchdb-authentication API will operate on your remote PouchDB object, not your local one."

Because of this, I keep getting a 401 error for unathorized/unauthenticated credentials.

So, how do I do live syncing then? Below is my code:

    const PouchDB = require("pouchdb");
    const PouchDBAuth = require('pouchdb-authentication');
    
    PouchDB.plugin(PouchDBAuth)
    
    // Create a new database instance
    const localDB = new PouchDB('database')
    
    const remoteDB = await new PouchDB("http://127.0.0.1/database", {skip_setup: true})
            remoteDB.logIn('user', '12345678', function (err, response) {
                if (err) {
                    if (err.name === 'unauthorized' || err.name === 'forbidden') {
                      console.log("4", err)
                    } else {
                       console.log("other")
                    }
                }
                remoteDB.getSession(function (err, response) {
                    if (err) {
                      // network error
                    } else if (!response.userCtx.name) {
                      // nobody's logged in
                    } else {
                      // response.userCtx.name is the current user
                    }
                localDB.sync(remoteDB, {
                    live: true
                }).on('change', function (change) {
                    // yo, something changed!
                    console.log("Yo, something changed")
                }).on('error', function (err) {
                    console.log("Yo, we got an error", err)
                    // yo, we got an error! (maybe the user went offline?)
                });
            })
       })

Upvotes: 0

Views: 56

Answers (1)

user22987395
user22987395

Reputation: 11

At the end of PouchDB Authentication package Overview there is: Since version 1.0.0, this plugin does support Node.js.

Upvotes: 1

Related Questions