MDSLKTR / pouch-vue

Pouchdb bindings for Vue.js
MIT License
90 stars 17 forks source link

Unauthorized user #25

Closed mbambirra closed 5 years ago

mbambirra commented 5 years ago

When i try to sync local pouchdb with remote couchdb i get "401 (Unauthorized)".

I'm trying to do this:

this.$pouch.getSession().then((data) => {
      if (data.status === 0) {
        this.$router.push('/login')
        console.log('most likely offline')
        return;
      }
      if (!data.user || !data.hasAccess) {
        this.$router.push('/login')
        return
      }
      console.log(data)
      this.session = data.user
      this.$pouch.sync('userdb-' + toHex(this.session.name), 'http://[couchip]:5984/userdb-' + toHex(this.session.name), {
        selector: {
          type: 'userdb',
          assignee: this.session.name
        }
      })
    })
assemblethis commented 5 years ago

Hi KaelM, this might have to do with the AuthSession cookie. If you go into your browser's dev tools and don't see the AuthSession cookie then you'll get back a 401 Unauthorized message from your couch database when you try to access it.

If it's there an you are still getting a 401 Unauthorized message, it's probably due to the fetch API (PWAs use the fetch api) not including the AuthSession cookie when requesting data from the couch db. You need to provide the optionsDB default with an override to the fetch function to include credentials, which is different from when you use XHR. The code below is from the TypeScript example on the readme page of this project:

Vue.use(pouchVue,{
  pouch: PouchDB,
  defaultDB: 'todos',
  optionsDB: {
    fetch: function (url:any, opts:any) {
        opts.credentials = 'include';
        return PouchDB.fetch(url, opts);
    }    
  }
})

See also issue #3

If that doesn't do it, let me know.

assemblethis commented 5 years ago

This issue may be fixed in the just released pouchdb v7.1: It was a bug and the adapter now includes options.credentials = 'include';

See: https://github.com/pouchdb/pouchdb/issues/7391

MDSLKTR commented 5 years ago

Please reopen if you have any further problems.