简体   繁体   English

使用CouchDB的PouchDB和Cookie身份验证实际上并未登录用户

[英]PouchDB and Cookie Authentication with CouchDB not actually logging in the user

I have a node app that uses CouchDB for the database and PouchDB on the client. 我有一个节点应用程序,该数据库将CouchDB用于数据库,并将PouchDB用于客户端。 To log in, a request is sent to the node server, which authenticates the user's credentials with the CouchDB instance, before sending a cookie back to the client. 要登录,将请求发送到节点服务器,该节点服务器在将cookie发送回客户端之前,先使用CouchDB实例对用户的凭据进行身份验证。 The issue is, after restarting the browser and logging in, CouchDB sends a 401 Unauthorized response when Pouch tries to connect to the DB, and the browser brings up its default login popup. 问题是,重新启动浏览器并登录后,当Pouch尝试连接到数据库时,CouchDB发送401未经授权的响应,并且浏览器显示其默认登录弹出窗口。 This happens even though the AuthSession cookie is in the browser. 即使AuthSession cookie在浏览器中,也会发生这种情况。

The full error in Chrome is: Failed to load resource: the server responded with a status of 401 (Unauthorized) http://localhost:5984/user/?_nonce=rItPZR1fgbHrwn0a Chrome中的完整错误是: Failed to load resource: the server responded with a status of 401 (Unauthorized) http://localhost:5984/user/?_nonce=rItPZR1fgbHrwn0a

After entering the user credentials in the Chrome pop-up box and refreshing the page, it loads as normal. 在Chrome弹出框中输入用户凭据并刷新页面后,它会正常加载。 Going to the page listed and entering the user credentials gives various metadata about the user. 转到列出的页面并输入用户凭据将提供有关用户的各种元数据。

I assume this is happening because PouchDB doesn't actually use the AuthSession token sent by the node server, but the cookie that is given to it by CouchDB after the user is prompted with a 401. Is there any way to get around this? 我认为发生这种情况是因为PouchDB实际上并不使用节点服务器发送的AuthSession令牌,而是CouchDB在向用户提示401之后为其提供的cookie。有什么办法可以解决此问题?

Thanks in advance. 提前致谢。

You may need to intercept and modify PouchDB's fetch calls to add the credentials option, which you can easily do when you first construct the database object: 您可能需要拦截和修改PouchDB的fetch调用以添加credentials选项,当您首次构造数据库对象时,可以轻松做到这一点:

db = new PouchDB('https://yourcouch/yourdb', {
    fetch: (url, opts) => fetch(url, { ...opts, credentials: 'include' /* OR 'same-origin' */ })
});

Note that you'll want to set it to same-origin where appropriate, or include for cross origin / no origin requests. 请注意,您将需要在适当的地方将其设置为same-origin ,或者include跨源/无源请求。

Actually, as long as the cookie is getting set on the client, authentication should automatically be picked up by PouchDB. 实际上,只要在客户端上设置了cookie,PouchDB就会自动获取身份验证。 I have a plugin that demonstrates this: https://github.com/nolanlawson/pouchdb-authentication . 我有一个插件可以证明这一点: https : //github.com/nolanlawson/pouchdb-authentication You can use this plugin to talk directly to your CouchDB and it will definitely work. 您可以使用此插件直接与您的CouchDB对话,它肯定会工作。 Maybe your Node proxy is not passing the cookie between PouchDB and CouchDB? 也许您的Node代理没有在PouchDB和CouchDB之间传递cookie?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM