Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login() fails with any user password combination. #130

Closed
charrondev opened this issue Dec 9, 2016 · 13 comments
Closed

login() fails with any user password combination. #130

charrondev opened this issue Dec 9, 2016 · 13 comments
Assignees

Comments

@charrondev
Copy link

charrondev commented Dec 9, 2016

I am trying to sign in as the admin user I created. I want to avoid the window popup so I am passing in the Authorization header, and that seems to be working properly, but when I call the login function

const user = {
      name: 'admin',
      password: 'password'
    }

    const pouchOpts = {
      skipSetup: true
    }

    const ajaxOpts = {
      ajax: {
        headers: {
          Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
        }
      }
    }

    console.log(ajaxOpts)

    const db = new PouchDB(`http://localhost:5984/example`, pouchOpts)
    db.login(user.name, user.password, ajaxOpts).then((res) => {
    })

It throws an error saying that the authorization is invalid. It can't be invalid because it authorized access through the header, but it throws the following error on the server:

[error] [<0.135.0>] httpd 500 error response:
 {"error":"badmatch","reason":"{\"name\":\"admin\",\"password\":\"password\"}"}

I'm using CouchDB 1.6.1.

If I manually do the authorization using Postman, or an ajax library it seems to work properly though.

@charrondev charrondev changed the title Tests failing in electron, chrome v55, and Safari 10.0.1 login() fails with any user password combination. Dec 9, 2016
@charrondev
Copy link
Author

charrondev commented Dec 12, 2016

So I've gotten it to work. The issue seems to be in lib/index.js when you JSON encode the username and password. body : JSON.stringify({name: username, password: password})

If I pass options:

const ajaxOpts = {
  ajax: {
    headers: {
      Authorization: 'Basic ' + window.btoa('admin:password')
    },
    body: {
      name: 'admin',
      password: 'password'
    }
  }
}

into login() then it works properly, because it overrides whatever weird thing goes on in the json encoding. It's quite odd to be honest. Is there a particular reason why this library JSON encodes the body, when it already gets json encoded in pouchdb-ajax?

@quarthex
Copy link

I made a project that reproduce this issue.

@runchman
Copy link

runchman commented Jan 31, 2017

I also have this issue, with a caveat - it only fails when run on the server side, not the client side.

I've created a user in _user of username "guest", password "guest"

when I run db.login('guest','guest') on client side (chrome), works fine.
when I run db.login('guest','guest') within my node.js server code, I get the same error as in this thread.

Similar to Charrondev, the following ajax fixes the problem for me on the server code: (sorry don't know how to format for the wiki)

const ajaxOpts = { ajax: { body: { name: 'guest', password: 'guest' } } }

And my error message:
{ error: 'badmatch', reason: '{"name":"guest","password":"guest"}', name: 'badmatch', status: 500, message: '{"name":"guest","password":"guest"}' }

@charrondev
Copy link
Author

Sorry that I haven't been able to get into this recently, even though I opened the issue. If it works in the browser and not node, that makes sense for my situation, as I am using Electron, which straddles the line between the two.

@natcohen
Copy link

Any chance we can get that working for Node.js? I'm having the same issue...

fredericgermain added a commit to fredericgermain/pouchdb-authentication that referenced this issue Sep 25, 2017
@ptitjes ptitjes self-assigned this Nov 11, 2017
ptitjes added a commit to ptitjes/pouchdb-authentication that referenced this issue Nov 12, 2017
ptitjes added a commit to ptitjes/pouchdb-authentication that referenced this issue Nov 14, 2017
@ptitjes
Copy link
Collaborator

ptitjes commented Nov 20, 2017

Fixed by 5b61b00.

@ptitjes ptitjes closed this as completed Nov 20, 2017
@vogelbeere
Copy link

vogelbeere commented Nov 27, 2017

The error I am getting is Uncaught TypeError: this.remote.login is not a function - is that the same error you were getting? (I am still getting the error even though I have applied the fix.)(5b61b00)

@ptitjes
Copy link
Collaborator

ptitjes commented Nov 27, 2017

@vogelbeere, no this is a different problem:
There is another problem with using pouchdb-authentication on Node.js, coming from a pouchdb-ajax bug. This bug have been fixed in pouchdb's master branch (see pouchdb/pouchdb#6814).

We are waiting for the next (coming soon) release of pouchdb to bump this dependency in pouchdb-authentication. Then you'll have a fully working stack in Node.js.

In the mean time, you can manually npm install pouchdb-ajax/master to your node_modules. But, if you want to do that in your package.json, you would have to do some postinstall script magic because pouchdb is a monorepo...

@vogelbeere
Copy link

Thanks @ptitjes - I am not using Node though.

@ptitjes
Copy link
Collaborator

ptitjes commented Nov 27, 2017

Sorry, this issue is about some failures that happened with Node.js. So I thought this was you problem.

Would you mind opening another issue and explaining fully your problem, please ? (You'll see there is an issue template that guides you into what informations we need to help you solve your problem.)
Thank you in advance @vogelbeere.

@vogelbeere
Copy link

Sorry - I am pretty sure that my issue is not caused by the code itself, but just wanted to know what error message this issue gave rise to. I have created a StackOverflow question.

@ptitjes
Copy link
Collaborator

ptitjes commented Nov 27, 2017

@vogelbeere If you had open an issue, that could have help others later. Also you are polluting an unrelated issue... Please, next time open an issue!

Anyway, I looked at your SO question (which I cannot answer because I don't have an SO account) and 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');

@vogelbeere
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants