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

查看:30
本文介绍了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 sync access with userid and password显示如何在 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: getting started 格式为 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

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

控制台中的错误消息

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

推荐答案

感谢 ptitjes 在 Github 上获取关于 login() 函数出了什么问题的答案:

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

同步功能

查看:Github 上 Manav Manocha 的 PouchNotes 版本

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

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