There is a PouchDB plugin created by Nolan Lawson that provides the PouchDb authentication API:
var db = new PouchDB('http://mysite:5984/mydb'); db.login('batman', 'brucewayne').then(function (batman) { console.log("I'm Batman."); return db.logout(); });
Here are the methods in which it is mixed:
- registration
- To come in
- Output
- getSession
- Getuser
To prevent basic HTTP browser authentication authentication in the old days, we need to be subtle about how we use PouchDB. To prevent rushing of an unauthorized request to CouchDB (used to check for the presence of a remote database), pass skipSetup: true in the parameters of the Pouch constructor. Secondly, to authenticate the request against _session, add the HTTP basic authorization header to the db.login () parameters of AJAX.
var user = { name: 'admin', password: 'admin' }; var pouchOpts = { skipSetup: true }; var ajaxOpts = { ajax: { headers: { Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password) } } }; var db = new PouchDB('http://localhost:5984/test', pouchOpts); db.login(user.name, user.password, ajaxOpts).then(function() { return db.allDocs(); }).then(function(docs) { console.log(docs); }).catch(function(error) { console.error(error); });
source share