inProgress-team / react-native-meteor

Meteor Reactivity for your React Native application :)
MIT License
693 stars 210 forks source link

Meteor.user() is null after Meteor.loginWithPassword() #236

Open Diwei-Chen opened 7 years ago

Diwei-Chen commented 7 years ago

Hi there,

I met an issue when after doing Meteor.loginWithPassword(), and in the callback did const user = Meteor.user(), it shows user === null.

This happened when my app connected with URL wss://<DOMAIN_NAME>/websocket, but when the app connected with a local host ws://localhost:3000/websocket, it works completely fine, ie it returns the user object as expected.

Am I doing anything wrong with the package or wws stuff?

FYI @adamgins

Regards,

twairball commented 7 years ago

loginWithPassword() callback doesn't have arguments on success. Docs: http://docs.meteor.com/api/accounts.html#Meteor-loginWithPassword

The correct way is to wait for Meteor to update props to component:


export default createContainer(params=>{
  return {
      user: Meteor.user()
  };
}, MyComponent)
Diwei-Chen commented 7 years ago

@twairtall Ah right! Thank you very much.

PolGuixe commented 7 years ago

@twairball I am doing this way however the container isn't refreshed after Accounts.createUser. In that case Meteor.user() is still undefined. If I refresh the app then works.

twairball commented 7 years ago

RE: Accounts.createUser I'm not a meteor expert, but I think most authentication systems don't login automatically after registration, so this pattern is expected.

For me, I login explicitly after registration.

Diwei-Chen commented 7 years ago

What Meteor variable(s) or way(s) can you determine you have log in succeeded tho? After loginWithPassword()?

Nauzer commented 7 years ago

@Diwei-Chen use Meteor.user() in your createContainer function. It's Reactive.

Sent from my OnePlus ONEPLUS A3003 using FastHub

adamgins commented 7 years ago

User() or userId(), which is reactive, is not null

Adam Ginsburg adam.ginsburg@buzzy.buzz tel: +61 411 151 732 www.buzzy.buzz

On 5 Jul 2017, at 4:26 pm, Diwei-Chen notifications@github.com wrote:

What Meteor variable(s) or way(s) can you determine you have log in succeeded tho? After loginWithPassword()?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Diwei-Chen commented 7 years ago

I found Meteor.user() is reactive when connecting to local server, but not for remote server. Anyone else has this issue?

Nauzer commented 7 years ago

Please share your code. Are you using within createcontainer?

Sent from my OnePlus ONEPLUS A3003 using FastHub

Diwei-Chen commented 7 years ago

Thanks @Nauzer

The scenario is, after a user clicks Login button it will call Meteor.loginWithPassword(...) and the component will wait till the user object comes back and dispatches two actions. But the problem is when I connect to my remote server, Meteor.user() is always null, but it works fine when connects with local server.

Here are some of the codes, hope I was missing something..

shouldComponentUpdate(nextProps, nextStates) {
    if (nextProps.user) {
      this.props.dispatch({ type: Type.LOGIN_USER_SUCCESS, payload: { user: nextProps.user } });
      this.props.navigation.dispatch({ type: Type.GO_TO_MAIN });
    }
    return true;
}

const LoginFormContainer = createContainer((props) => {
  return {
    user: Meteor.user()
  }
}, LoginForm);
Nauzer commented 7 years ago

That is what I'm doing as well and should work. What happens if you output anything in your render function on the basis of Meteor.user() 's value?

render() {
    if(this.props.user) {
        return (
            <Text>We have a user</Text> 
        )
    } else {
        return (
            <Text>No we don't</Text>
        )
    }
}
Diwei-Chen commented 7 years ago

It returns No we don't in my case. I found what interesting is the users collection is always added through listening to ddp connection Meteor.ddp.on('added', (obj, ...args) and the user object is in this collection.

jackkav commented 7 years ago

I had this issue because a few of our users had ids generated by non-meteor apis and had non-meteor _ids. react-native-meteor will log in and issue a token with the non-meteor id but after some debugging it would appear that the collection.findOne returns null give an _id type it isn't expecting. I have tested with a regular meteor app and login works so it would appear to be an issue with react-native-meteor.

185

Edit: this bug comes from minimongo-cache added a hacky fix to https://github.com/jackkav/minimongo-cache/

jeffreyflynt commented 6 years ago

I am still facing this issue, did anyone ever come up with a solution?

jon617 commented 6 years ago

@jeffreyflynt maybe try the new withTracker which replaces createContainer, docs here https://guide.meteor.com/react.html#using-withTracker and put the Meteor.userId() within the withTracker section, something like:

const mapMeteorToProps = (props) => {
  return {
    userId: Meteor.userId(),
  };
};
export default withTracker( mapMeteorToProps )( ClassName );

And in the class (ClassName in this example), use this.props.userId

MikePowar commented 5 years ago

Thanks for this @jon617 ! It worked for us & also appreciated a link to the Meteor documentation - which I found to be written with helpful unambiguous language.