CouchDB login access on Google App Engine

2019-03-04 17:50发布

I am running a CouchDB instance on Google App Engine. I have successfully created a SSH tunnel to access Fauxton on it via localhost. I have enabled CORS and am using https. I have added NETWORK: * to my cache manifest.

Now I need to sync my PouchDB data to my remote CouchDB database.

This related question Couchdb sync access with userid and password shows how to pass in the userid and password on a CouchDB instance hosted on cloudant. (I've also checked all the other related questions.)

Is there a similar method to use for doing this on Google App Engine?

Something like this?

https://<api_key>:<key_passwd>@<username>.<my_ip_address>:5984/<db_name>

According to PouchDB: getting started the format is http://user:pass@myname.example.com/dbname

I'm not sure if I should use the hostname (myapp.appspot.com) or the host's IP address though.

Code in application.js

PouchNotesObj = function (databasename, remoteorigin) {
    'use strict';

    Object.defineProperty(this, 'pdb', {writable: true});
    Object.defineProperty(this, 'remote', {writable: true});
    Object.defineProperty(this, 'formobject', {writable: true});
    Object.defineProperty(this, 'notetable', {writable: true});
    Object.defineProperty(this, 'searchformobject', {writable: true});
    Object.defineProperty(this, 'errordialog', {writable: true});
    Object.defineProperty(this, 'dbname', {writable: true});

    var databasename = 'myPdb';
    var remoteorigin = 'https://<IP ADDRESS>';


    this.dbname = databasename;
    this.pdb = new PouchDB(databasename);
    this.remote = remoteorigin + '/myPdb';

//from https://github.com/pouchdb-community/pouchdb-authentication/issues/121      
    var user = {
      name: 'admin',
      password: '<myPassword>'
    };
    var pouchOpts = {
      skipSetup: true
    };
    var ajaxOpts = {
      ajax: {
        headers: {
          Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
        }
      }
    };
    this.remote.login(user.name, user.password, ajaxOpts).then(function() {
      return db.allDocs();
    }).then(function(docs) {
      console.log(docs);
    }).catch(function(error) {
      console.error(error);
    });

};

pouchdb.authentication.js

exports.login = utils.toPromise(function (username, password, opts, callback) {
  var db = this;
  if (typeof callback === 'undefined') {
    callback = opts;
    opts = {};
  }
  if (['http', 'https'].indexOf(db.type()) === -1) {
    return callback(new AuthError('this plugin only works for the http/https adapter'));
  }

  if (!username) {
    return callback(new AuthError('you must provide a username'));
  } else if (!password) {
    return callback(new AuthError('you must provide a password'));
  }

  var ajaxOpts = utils.extend(true, {
    method : 'POST',
    url : utils.getSessionUrl(db),
    headers : {'Content-Type': 'application/json'},
    body : {name: username, password: password}
  }, opts.ajax || {});
  utils.ajax(ajaxOpts, wrapError(callback));
});

Error message in Console

Uncaught TypeError: this.remote.login is not a function

1条回答
再贱就再见
2楼-- · 2019-03-04 18:49

Thanks to ptitjes on Github for an answer on what was going wrong with the login() function:

you are trying to call login() on a string (your this.remote is a string). Hence the TypeError: this.remote.login is not a function. You should have done: this.remote = new PouchDB(remoteorigin + '/myPdb');

(I find that confusing because the remote database is CouchDB, but there you go.)

The login is now working but the sync still isn't working.

Login function

PouchNotesObj = function (databasename, remoteorigin) {
'use strict';

Object.defineProperty(this, 'pdb', {writable: true});
Object.defineProperty(this, 'remote', {writable: true});
Object.defineProperty(this, 'formobject', {writable: true});
Object.defineProperty(this, 'notetable', {writable: true});
Object.defineProperty(this, 'searchformobject', {writable: true});
Object.defineProperty(this, 'errordialog', {writable: true});
Object.defineProperty(this, 'dbname', {writable: true});

var databasename = 'pouchnotes';
var remoteorigin = 'https://ip-address:5984';

this.dbname = databasename;
this.pdb = new PouchDB(databasename);
this.remote = new PouchDB(remoteorigin + '/pouchnotes');

var user = {
  name: 'admin',
  password: 'password'
};
var pouchOpts = {
  skipSetup: true
};
var ajaxOpts = {
  ajax: {
    headers: {
      Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
    }
  }
};

this.remote.login(user.name, user.password, ajaxOpts, function (err, response) {
  if (err) {
    if (err.name === 'unauthorized' || err.name === 'forbidden') {
      console.log('Unauthorised user');
    } else {
      //return this.remote.allDocs();
      console.log('Successful login');
    }
  }
});


var opts = {live: true};
this.pdb.replicate.to(this.remote, opts);
this.pdb.replicate.from(this.remote, opts);
};

Sync function

See: Manav Manocha's version of PouchNotes on Github

查看更多
登录 后发表回答