Google App Engine上的CouchDB登录访问 [英] CouchDB login access on Google App Engine

查看:98
本文介绍了Google App Engine上的CouchDB登录访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Google App Engine上运行一个CouchDB实例.我已经成功创建了SSH隧道来访问Fauxton通过本地主机.我已启用CORS,并且正在使用https.我已经添加了NETWORK: *到我的缓存清单中.

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.

现在,我需要将PouchDB数据同步到远程CouchDB数据库.

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

此相关问题使用用户名和密码的Couchdb同步访问显示如何在cloudant上托管的CouchDB实例上传递用户ID和密码. (我还检查了所有其他相关问题.)

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.)

在Google App Engine上可以使用类似的方法吗?

像这样吗?

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

根据 PouchDB:入门,格式为http://user:pass@myname.example.com/dbname

我不确定是否应该使用主机名(myapp.appspot.com)或主机的IP地址.

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

application.js

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

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));
});

控制台中的错误消息

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

推荐答案

感谢

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

您正在尝试在字符串上调用login()(您的this.remote是一个 细绳).因此,TypeError: this.remote.login is not a function.你 应该做的:this.remote = new PouchDB(remoteorigin + '/myPdb');

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');

(我发现这很令人困惑,因为远程数据库是CouchDB,但是您就可以了.)

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

登录现在可以正常工作,但同步仍然无法正常工作.

登录功能

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);
};

同步功能

请参阅:Manav Manocha在Github上的PouchNotes版本

这篇关于Google App Engine上的CouchDB登录访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆