Giter Site home page Giter Site logo

Comments (19)

JulianKingman avatar JulianKingman commented on July 20, 2024

Try with 1.1.13. It shouldn't be null unless you're not signed in.

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

Ok I'll test right now.
Very quick :)

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

No, it is still null.
When I disconnect the server ('Disconnected from DDP server.'), the user is still detected. But as soon as I refresh the react native app, user is null.
It happens only with the 'users' collection.

from react-native-meteor-offline.

JulianKingman avatar JulianKingman commented on July 20, 2024

Did you try in the example, or do you have a repo that I can test? It works for me in the example (if you go to "Profile").

BTW, in case you don't already know about it, there's a preference pane for osx called "Link Conditioner" that allows you to quickly test network connectivity, without jumping on and off wifi.

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

No I didn't try in the example, but I will in 7 hours when I'm back home.
I also have a repo, I will give it to you but I need to commit tonight.

Oh I didn't know this spec, perfect cheers for the info !

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

I just tested with the example and it doesn't work (with the version 1.1.13).
The user is not persisted offline (still null).

I have added console.log(user); line 28 in index.js file (RNApp).
Then disconnected the meteor server.
Then refreshed the RNApp.

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

[FIXED]
I don't think this is a good pattern, but I have replaced user: Meteor.user(), by user: Meteor.collection('users').findOne() in createContainer and the user object is persisted offline.

from react-native-meteor-offline.

JulianKingman avatar JulianKingman commented on July 20, 2024

I'll take a look, but in the meantime that's not a bad solution. react-native-meteor does that on the backend, I believe.

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

Ok I think I got it.
Actually the problem is not your package but the react-native-meteorone (User.js file).

When you do a Meteor.user() if it doesn't find a this._userIdSaved, it returns null.
if it finds it, it returns this.collection('users').findOne(this._userIdSaved).

user() {
    if(!this._userIdSaved) return null;

    return this.collection('users').findOne(this._userIdSaved);
  }

This is exactly the same with Meteor.userId():

userId() {
    if(!this._userIdSaved) return null;

    const user = this.collection('users').findOne(this._userIdSaved);
    return user && user._id;
}

The this._userIdSavedis defined when login, maybe we should ask to contributors of this repo to set one more item (result.id at the first login on the app) in the Asyncstorage:

_handleLoginCallback(err, result) {
    if(!err) {//save user id and token
      AsyncStorage.setItem(TOKEN_KEY, result.token);
      Data._tokenIdSaved = result.token;
      this._userIdSaved = result.id;
      Data.notify('onLogin');
    } else {
      Data.notify('onLoginFailure');
      this.handleLogout();
    }
    Data.notify('change');
  },

from react-native-meteor-offline.

JulianKingman avatar JulianKingman commented on July 20, 2024

That makes sense. So, the _handleLoginCallback should be fine, it's the _loginWithToken that calls it in a callback from a Meteor.call, which then requires a connection. Probably best would be to call _handleLoginCallback from initMeteorRedux. Thoughts?

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

Yeah I think you're right, calling _handleLoginCallback() would be the best option.

from react-native-meteor-offline.

JulianKingman avatar JulianKingman commented on July 20, 2024

Huh, this is tricky... The userId is obtained from the Meteor.call, not from AsyncStorage, so I can't pass the required parameters. Ideas?

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

Yeah a bit complicated ...
Why not just create 2 functions in your package for now like:

user() {
    return Meteor.collection('users').findOne();
  }

and

userId() {
    const user = Meteor.collection('users').findOne();
    return user && user._id;
}

It will avoid going through this._userIdSaved.
It won't make us in trouble because there will be only one user using the app (normally).

And then asking for react-native-meteor contributors to register this._userIdSaved in the asyncstorage ...

from react-native-meteor-offline.

JulianKingman avatar JulianKingman commented on July 20, 2024

If you have more than one user, is that guaranteed to return the logged in user?

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

With autopublish package I don't know. (I think it should work)
But if you remove it, then you just have to create a custom publish for user data:

Meteor.publish("userData", function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId});
  } else {
  this.ready();
  }
});

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

In fact I think it works in every cases, even without the above snippet

from react-native-meteor-offline.

JulianKingman avatar JulianKingman commented on July 20, 2024

But I mean if you have multiple users, how do you know the user you get back is the logged in one? You could have 100 users and it could return any random one, couldn't it?

from react-native-meteor-offline.

balibou avatar balibou commented on July 20, 2024

You have the const TOKEN_KEY = 'reactnativemeteor_usertoken'; that is stored on the Asyncstorage. So it is linked to a unique userId.

Then, once you're connected you will get info of the user connected with the token, even if there are 100 other users on the database

from react-native-meteor-offline.

JulianKingman avatar JulianKingman commented on July 20, 2024

I added MO.user() in v2.2.0

from react-native-meteor-offline.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.