meteorrn / meteor-react-native

Meteor client for React Native matching Meteor Spec
https://guide.meteor.com/react-native.html
Other
59 stars 31 forks source link

Accounts.createUser does not immediately log user in #22

Closed NickTheTurtle closed 4 years ago

NickTheTurtle commented 4 years ago

Describe the bug Either Accounts.createUser does not immediately log user in, or Meteor.user() and Meteor.userId() does not get updated automatically when Accounts.createUser is successful

To Reproduce Steps to reproduce the behavior:

App.js code:

class App extends React.Component {
  render() {
    return (
      <SafeAreaProvider>
        <IconRegistry icons={EvaIconsPack} />
        <ApplicationProvider {...eva} theme={eva.light}>
          {this.props.currentUser ? <ThisPage /> : <LoginPage />}
        </ApplicationProvider>
      </SafeAreaProvider>
    );
  }
}

export default withTracker(() => {
  return {
    currentUser: Meteor.user(),
  };
})(App);

Accounts.createUser code:

Accounts.createUser(
          {
            email: this.state.email,
            password: this.state.password,
          })

I can verify that a new user is indeed created in the database. However, upon successful creation of user, app does not automatically switch to but stays on .

Expected behavior Upon successful creation of user, app does automatically switch to .

Screenshots N/A

Device (please complete the following information):

TheRealNate commented 4 years ago

Hi @NickTheTurtle

Do you encounter any similar issues when logging in regularly? I'm still trying to reproduce the issue on my end.

Thanks!

NickTheTurtle commented 4 years ago

Hi, thanks for the quick reply. Logging in normally does not give any weird behaviors. I found this blog online that documented the same error: https://medium.com/differential/react-native-meteor-auth-with-email-username-and-password-d2085c732276. Seems like the bug existed for quite some time.

TheRealNate commented 4 years ago

One more thing to try, can you add a handler for the Accounts.onLoginFailure event:

Accounts.onLoginFailure(err => {
    console.error(err);
});
NickTheTurtle commented 4 years ago

I tried Accounts.onLoginFailure, and the error it logged was… "undefined", curiously. I attached the image below:

image
TheRealNate commented 4 years ago

Currently, onLoginFailure doesn't actually tell you the error, just calls the callback when there is one. However, this points to an error in the createUser call. Are you calling createUser with a callback?

Accounts.createUser({...}, err => {
    if(err) console.error(err);
});

The callback should have an error since onLoginFailure is being called. That should give you some more info.

NickTheTurtle commented 4 years ago

I can verify that a user is indeed created by checking with "meteor mongo". Also, the error callback in createUser is not called as I set it to display pop up boxes if errors occurred (and the pop up boxes do appear when, say the email has already been associated with another account).

TheRealNate commented 4 years ago

Thats really strange. When createUser is called, whatever error/result it receives is passed directly to _handleLoginCallback which in turn triggers onLoginFailure if there was an error, but the same error should have been passed to your callback.

If you would be willing to make some changes to your local version of the package, you can make a change to:

node_modules/@meteorrn/core/src/user/Accounts.js

Inside the createUser method, you can log the error and result received to confirm that there is no error and that a token is being returned.

If you'd prefer not to make these changes, I have a release planned for tomorrow that implements some deeper debugging.

NickTheTurtle commented 4 years ago

Ok, so after putting some console messages in Accounts.js, I found out that every function that's supposed to be called is called, and err was always undefined. However, after making the changes to Accounts.js, the app reloaded on the second time and the user got logged in. This did not happen when changing my ReactNative files, curiously. This makes me think that the app did successfully log the user in after account creation, but the notification did not get propagated and Meteor.user() did not change.

TheRealNate commented 4 years ago

Could you try adding a timeout and logging Meteor.user() and Meteor._userIdSaved (note that _userIdSaved is a string value not a function)? Once the login callback is handled, _userIdSaved is immediately set, but its possible that for some reason the user object isn't being received from the server.

NickTheTurtle commented 4 years ago

It logs null and undefined, respectively.

TheRealNate commented 4 years ago

So, when _handleLoginCallback is called _userIdSaved should be immediately set (unless there is an err). When you put your console messages, did you confirm that _handleLoginCallback was not receiving an err object? If so, something else is happening, possibly related to your other issue (how the functions are being bound to this).

NickTheTurtle commented 4 years ago

Yeah, no error occurred. Also, I have a logging in indicator that fires when the user is logging in but doesn't upon account creation. I was able to verify that Users._startLoggingIn was called though. If I manually call Meteor._startLoggingIn, the logging in indicator works.

Next, instead of calling Accounts.createUser, I did this:

Meteor._startLoggingIn();
Meteor.call(
  'createUser',
  {
    email: this.state.email,
    password: this.state.password,
  },
  (error, result) => {
    Meteor._endLoggingIn();

    Meteor._handleLoginCallback(error, result);

    if (error) {
      //handle error
    }
  },
);

and it now works as expected. Strange.

copleykj commented 4 years ago

A reproduction of this and #20 might be helpful if possible.

TheRealNate commented 4 years ago

Hey @NickTheTurtle, any chance this issue could be related to #21, where this wasn't being bound correctly?

TheRealNate commented 4 years ago

Hi @NickTheTurtle, just following up to see if this issue is resolved?

NickTheTurtle commented 4 years ago

I don't think it's a "this" issue since I'm calling Accounts.createUser in a class method.

TheRealNate commented 4 years ago

Hi @NickTheTurtle, could you see if the issue is still happening with release 2.0.13?

TheRealNate commented 4 years ago

Hi @NickTheTurtle, any updates on this?

TheRealNate commented 4 years ago

Closing this as it is stale. Feel free to re open if the issue persists.