pouchdb-community / pouchdb-authentication

User authentication plugin for PouchDB and CouchDB.
Apache License 2.0
777 stars 117 forks source link

401 Unauthorized after successful login #268

Open rehovicova opened 3 years ago

rehovicova commented 3 years ago

Expected Behavior

I expect I can query the database after login without getting 401

Current Behavior

I am getting 401 after successful login

Code

let PouchDB = require('pouchdb')
PouchDB.plugin(require('pouchdb-authentication'))

let localDb = PouchDB(dbConfig.dbName);
let remoteDb = PouchDB(`http://${dbConfig.host}:5984/${dbConfig.dbName}`, { skip_setup: true })
remoteDb.login(dbConfig.username, dbConfig.password).then( result => {
    console.log(result) // <--- { ok: true, name: 'admin', roles: [ '_admin' ] }

    remoteDb.get(id).then( r => console.log(r)).catch( e => console.log(e));
    // Prints
    //  {
    //    error: 'unauthorized',
    //    reason: 'You are not authorized to access this db.',
    //     status: 401,
    //    name: 'unauthorized',
    //    message: 'You are not authorized to access this db.',
    //    docId: 'some_id'
    //  }

}).catch( e => logger.error(`${TAG}: ${e}`))

Your Environment

PS: I am connecting to the same database from a different project using JavaScript and PouchDB 7.2.1 and auth 1.1.3 (so exactly the same) and it works jut fine

f5cs commented 3 years ago

After login, the subsequent request does not carry the token of permission verification,i don't understand why there are such low-level mistakes here, but the problem can be solved in the following ways: let remoteDb = PouchDB( http://${dbConfig.host}:5984/${dbConfig.dbName}, { skip_setup: true, fetch:function(url,opts){ opts.headers.set('Authorization','Basic ' + btoa(dbConfig.username+":"+dbConfig.password');); } })

ibalaji777 commented 3 years ago

no need this package PouchDB.plugin(require('pouchdb-authentication'))

new PouchDB('url', {
  auth: {
    username: 'user',
    password: 'pass'
  }
}
ibalaji777 commented 3 years ago

no need this package PouchDB.plugin(require('pouchdb-authentication'))

new PouchDB('url', {
  auth: {
    username: 'user',
    password: 'pass'
  }
}
HZSamir commented 2 years ago

Any progress on this? Facing the same issue.