{"id":8118,"date":"2015-11-26T13:55:44","date_gmt":"2015-11-26T13:55:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-sync-authorization-open-source-projects-pouchdb-pouchdb\/"},"modified":"2022-08-30T15:03:00","modified_gmt":"2022-08-30T15:03:00","slug":"pouchdb-sync-authorization-open-source-projects-pouchdb-pouchdb","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-sync-authorization-open-source-projects-pouchdb-pouchdb\/","title":{"rendered":"PouchDB sync authorization?-open source projects pouchdb\/pouchdb"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/eaee4a968ac2e346ec28faf0c46ffdc7?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>twilson63<\/strong><\/p>\n<p>There is a PouchDB plugin built by Nolan Lawson that provides PouchDb with an authentication API:<\/p>\n<pre><code>var db = new PouchDB('http:\/\/mysite:5984\/mydb');\ndb.login('batman', 'brucewayne').then(function (batman) {\n  console.log(\"I'm Batman.\");\n  return db.logout();\n});\n<\/code><\/pre>\n<p>Here are the methods it mixes in:<\/p>\n<ul>\n<li>signup<\/li>\n<li>login<\/li>\n<li>logout<\/li>\n<li>getSession<\/li>\n<li>getUser<\/li>\n<\/ul>\n<p>To prevent browser HTTP basic authentication modal dialogs of ye olde times, we have to be subtle in the way we use PouchDB. To prevent a rouge unauthenticated request to CouchDB (used to check whether the remote DB exists), pass skipSetup: true in Pouch&#8217;s constructor options. Secondly, to authenticate the request against _session, add the HTTP basic authorization header to db.login()&#8217;s AJAX options.<\/p>\n<pre><code>var user = {\n  name: 'admin',\n  password: 'admin'\n};\n\nvar pouchOpts = {\n  skipSetup: true\n};\n\nvar ajaxOpts = {\n  ajax: {\n    headers: {\n      Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)\n    }\n  }\n};\n\nvar db = new PouchDB('http:\/\/localhost:5984\/test', pouchOpts);\n\ndb.login(user.name, user.password, ajaxOpts).then(function() {\n  return db.allDocs();\n}).then(function(docs) {\n  console.log(docs);\n}).catch(function(error) {\n  console.error(error);\n});\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>twilson63 There is a PouchDB plugin built by Nolan Lawson that provides PouchDb with an authentication API: var db = new PouchDB(&#8216;http:\/\/mysite:5984\/mydb&#8217;); db.login(&#8216;batman&#8217;, &#8216;brucewayne&#8217;).then(function (batman) { console.log(&#8220;I&#8217;m Batman.&#8221;); return db.logout(); }); Here are the methods it mixes in: signup login logout getSession getUser To prevent browser HTTP basic authentication modal dialogs of ye olde times, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8118","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8118"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8118\/revisions"}],"predecessor-version":[{"id":8607,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8118\/revisions\/8607"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}