pouchdb-community / pouchdb-authentication

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

Can't make it work with React Native #253

Closed mqtik closed 5 years ago

mqtik commented 6 years ago

Hello everyone,

I'm trying to make this work with the plugin PouchDB Authentication, with no luck.

It throws an error: Reference error: Blob is not defined

I tried to install another library reading blob, change the source code, among other things, but still wont work.

import PouchDB from 'pouchdb-react-native'
import PouchDBAuthentication from 'pouchdb-authentication'

PouchDB.plugin(PouchDBAuthentication)
const remoteDB = new PouchDB('http://mqserv.com:5489/_users', {skip_setup: true}) // also tried without /_users endpoint
remoteDB.signUp(username, password, function (err, response) {
  if (err) {
    if (err.name === 'conflict') {
      console.log("Already exists")
    } else if (err.name === 'forbidden') {
      // invalid username
      console.log("Username invalido")
    } else {
      console.log("ERROR:", err)

      // HTTP error, cosmic rays, etc.
       //  (here is where I am)
    }

  }
});

Hope you can help me out with this.

Kind regards.

ptitjes commented 6 years ago

Hi! Thanks for the issue.

You are saying that your code snippet throws an error, right ? Would you mind sharing the error that is output to console please ?

Just to be clear, the signUp method is for creating a user. To actually log in, you have to use the logIn method.

mqtik commented 6 years ago

Yes, I did use this library before in web development. This is the first time using it with React Native.

Well, this is the output:

screen shot 2018-11-28 at 08 40 13

Seems like pouchdb-ajax is giving problems

ptitjes commented 6 years ago

From the look of the error, it seems that this has nothing to do with PouchDB Authentication.

You omitted to fill in the provided template for issues. So please provide all the information that was requested. We need those.

PaulMest commented 5 years ago

@matikups I experienced the issue you're having with React Native many years ago. You are probably missing the blob polyfill (among other things).

Here is my current config to get PouchDB 7.0.0 to work in React Native...

// File: pouchdb-dependencies.js
// These are used for PouchDB's checks to make sure SQLite is present

import SqlitePlugin from 'react-native-sqlite-storage';
import { Buffer } from 'buffer';
import 'blob-polyfill';

global.cordova = global.cordova || true;
global.sqlitePlugin = global.sqlitePlugin || SqlitePlugin;
global.openDatabase = global.openDatabase || true;
process.nextTick = process.nextTick || setImmediate;

// These rest are polyfills to make sure that btoa, atob, and blob are present for PouchDB to serialize/deserialize properly
global.Buffer = global.Buffer || Buffer;
if (typeof btoa === 'undefined') {
  global.btoa = function (str) {
    return new Buffer(str).toString('base64');
  };
}

if (typeof atob === 'undefined') {
  global.atob = function (b64Encoded) {
    return new Buffer(b64Encoded, 'base64').toString();
  };
}
// File: cleverpoint-pouch-react-native.js
import './pouchdb-dependencies';

import PouchDB from 'pouchdb-core';
import CordovaSQLitePouch from 'pouchdb-adapter-cordova-sqlite';
import HttpPouch from 'pouchdb-adapter-http';
import replication from 'pouchdb-replication';
import mapreduce from 'pouchdb-mapreduce';
import UpsertPouch from 'pouchdb-upsert';

PouchDB
  .plugin(CordovaSQLitePouch)
  .plugin(HttpPouch)
  .plugin(replication)
  .plugin(mapreduce)
  .plugin(UpsertPouch);

export default PouchDB;
// File: calling-code.js
import PouchDB from './cleverpoint-pouch-react-native';
const db = new PouchDB('my_database', { adapter: 'cordova-sqlite' });

Hope this helps!

mqtik commented 5 years ago

@PaulMest thanks for your response!

Well, I found out that the problem was due to the React/React Native version.

ReactNative 16 ships with support for Blobs. I had version 15. There's still problems with PouchDB-React-Native. It seems like it doesn't support quiet yet _attachments.

Closing, since this issue's solved.