pouchdb-community / pouchdb-authentication

User authentication plugin for PouchDB and CouchDB.
Apache License 2.0
776 stars 118 forks source link

Create database after login issue #134

Closed eeschiavo closed 6 years ago

eeschiavo commented 7 years ago

Hi,

I'm trying doing the following: I have a local database (using PouchDB), I check if user is logged in (with pouchdb-authentication login function) and if true I sync the locale db with the remote one.

Unfortunately, when I try to create a new database on CouchDB (I want one db for every user) I always get the error {"error":"not_found","reason":"no_db_file"}. I saw this is a common error described in PouchDB documentation (https://pouchdb.com/guides/databases.html#remote-databases) but CORS is enabled and I can't figure out where is the problem.

My couchdb configuration is:

schermata 2017-01-02 alle 23 25 03

I do the login as follow:

      var user = {
              name: 'name',
              password: 'password'
      };
      var url = "http://ip/";
            var pouchOpts = {
              skipSetup: true
       };
      var ajaxOpts = {
          ajax: {
                headers: {
                      Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
                }
           }
      };
      var db = new PouchDB(url+'auth', pouchOpts);

       db.login(user.name, user.password, ajaxOpts).then(function() {
                return db.allDocs();
       }).then(function(docs) {

           //HERE I TRY TO CREATE THE NEW DATABASE
           pouchDBService.sync(url+"newDatabase", user);

      }).catch(function(error) {
                console.error(error);
      });

And, in my pouchDBService I have:

     var database;

     //I call this function as app starts
     this.setDatabase = function(databaseName) {
         database = new PouchDB(databaseName, {
             adapter: 'websql'
        });
      }

      this.sync = function(remoteDatabase, user) {
            var remoteDB = new PouchDB(remoteDatabase, {
                auth: {
                    username: user.name,
                    password: user.password
                },
                skip_setup: true // without this I get the login popup!
                // Why if I'm passing the auth params???
            });
            remoteDB.info().then(function (info) {
                console.log(info);
                database.sync(remoteDB, {live:true, retry: true})
            })
      }

Is there something wrong? Any help is appreciated.

Thanks

nolanlawson commented 7 years ago

Hm, I believe no_db_file should only occur when the database is not already created but it expects it should be created. Maybe the user doesn't have authorization to create databases?

eeschiavo commented 7 years ago

Hi @nolanlawson, thanks you for your help. I don't think is an authorization issue since the user tested was admin.

Can "require_valid_user = true" on couchdb be a problem?

My goal is to have multiple databases for each user and allow them to access each db. I tried doing this:

  1. Signup a new user (not admin!) with the built-in signup function of pouchdb authentication
  2. if signup is ok, login the user with the login function of pouchdb authentication
  3. create a new (or more than once) PouchDB remote database. At this point, if I understood the pouchdb documentation, if the db doens't exists on couchdb it will be created.

I have multiple issues in this process:

  1. If I singup a user with require_valid_user = true (on couchdb) and skipSetup (on pouchdb authentication) and without having a valid cookie in browser (so I'm in a new session) the signup fails. I get the authentication popup asking me for an account (on browser). So I set require_valid_user = false and everything works. Obviously doing this lets my db opened and anyone can access it using direct link.
  2. If I login with the admin user (since with require_valid_user = true I can't signup a new user!) I try to create a new database (as exposed above) but I always got no_db_file. (3. This evening I want to do the following: set require_valid_user = false and after signup a new user trying to create a new database, I don't know if I'll get the no_db_file, I'll let you know!)

Do you have any ideas?

I think there is something I miss but I can't figure out what is it. Please let me know if I can supply you more informations.

(sorry for my english!)

Thanks

Update:

I just tried setting require_valid_user = false on couchdb and logging in with a valid user with pouchdb. After success login I tried creating a new database (new Pouchdb(...)) but unfortunately I get the no_db_file error.

dodomui commented 7 years ago

Hi I am having same issue. Try to create new db at cloudant but always failed...

Anybody had idea what's the problem?

var db = new PouchDB(https://$USERNAME.cloudant.com/$DATABASE, {auth: {username: admin username, password: admin password}});

error:"forbidden" message:"server_admin access is required for this request" name:"forbidden" reason:"server_admin access is required for this request" status:403

ptitjes commented 6 years ago

@eeschiavo Would you mind trying to make tiny repo that shows the problem with the readme telling the Couch configuration and users ?

ptitjes commented 6 years ago

Closing this issue as it seems to be stale. Please do reopen a ticket if you still experience this.