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.resetPassword not return userId reactively #20

Closed nastaransamui closed 4 years ago

nastaransamui commented 4 years ago

Describe the bug Try to reset password with Token from reset password email it's return true but Meteor.userId() still be null when close and open application it will update and login user.

To Reproduce Steps to reproduce the behavior:

  1. Create User and verify
  2. try for reset password email sent successfully.
  3. click on link open application add new password send new password with Token from URL return result but Meteor.userId() still null
  4. See error

Expected behavior After reset password Meteor.userId() update and user automatic login.

Device (please complete the following information):

Additional context


      if (err) {
        setClicked(false);
        ToastAndroid.show(
          `${err.message}`,
          ToastAndroid.LONG,
          ToastAndroid.BOTTOM,
        );
        // props.navigation.navigate('AuthTab');
      } else {
        setTimeout(() => {
          console.log(Meteor.userId())
          console.log(props.User)
        }, 3000);
        setClicked(false);
        ToastAndroid.show(
          'Your password was changed and logged in.',
          ToastAndroid.LONG,
          ToastAndroid.BOTTOM,
        );
        // props.navigation.navigate('AuthTab');
      }
    });
  };```
TheRealNate commented 4 years ago

Hi @nastaransamui. Thanks for submitting the issue.

When the Accounts.resetPassword callback is called, that means the password has been changed, but not necessarily that the user has been logged in yet. I see that you're using a 3 second timeout before checking, but just to be safe, could you try waiting for a longer time period (5-10 seconds) and checking? Sometimes it can take Meteor a while to login.

If you are willing to dig around in the code in:

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

You can update the resetPassword module to console log the token being returned by the server, just in case theres an issue on the Meteor server end.

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);
});
nastaransamui commented 4 years ago

Thank you @TheRealNate for reply After I checked even in 10-second userId() still null, then I dig inside Accounts.js and User.js I figure out the token is return successfully and _handleLoginCallback is called but

_handleLoginCallback(err, result) {
    if (!err) {
      //save user id and token
      Data._options.AsyncStorage.setItem(TOKEN_KEY, result.token);
      Data._tokenIdSaved = result.token;
      this._userIdSaved = result.id;
      Data.notify('onLogin');
--------------------------------------------
  notify(eventName) {
    this._cbs.map(cb => {
      if (cb.eventName == eventName && typeof cb.callback == 'function') {
        cb.callback();
      }
    });

I doubt Data.notify with value 'onLogin' there is not any eventName with "onLogin". Thanks for your help

TheRealNate commented 4 years ago

Hi @nastaransamui, in _handleLoginCallback after the if statement Data.notify('change') is called, which triggers the reactive update of Meteor.user if the user is logged in.

Are you experiencing the same issue when you use Meteor.loginWithPassword?

nastaransamui commented 4 years ago

@TheRealNate Meteor.loginWithPassword is work as expected but still resetPassword not update Meteor.user reactively. It's still an issue.

TheRealNate commented 4 years ago

Hi @nastaransamui, two things:

  1. Could you also console log Meteor._userIdSaved, as this should be set immediately
  2. Could you provide the way you are calling resetPassword with more context

Thanks!

nastaransamui commented 4 years ago

Hi, @TheRealNate I checked Meteor._userIdSaved it's still null. I don't think so my calling has any problem as when I restart the app it's automatic login. But I got Token from URL and plus to new password send request below is my whole submit function:

const onSubmit = (data) => {
    setClicked(true);
    const LastPassword = data.NewPassword;
    const Token = props.route.params.Token;
    Accounts.resetPassword(Token, LastPassword, (err, done) => {
      if (err) {
        setClicked(false);
        ToastAndroid.show(
          `${err.message}`,
          ToastAndroid.LONG,
          ToastAndroid.BOTTOM,
        );
        // props.navigation.navigate('AuthTab');
      } else {
        SetLogedin(true);
        setClicked(false);
        console.log(Meteor._userIdSaved)
        ToastAndroid.show(
          'Your password was changed and logedin.',
          ToastAndroid.LONG,
          ToastAndroid.BOTTOM,
        );
      }
    });
  };

and I checked _handleLoginCallback in User.js and Data.notify('change') is called.

_handleLoginCallback(err, result) {
    if (!err) {
      //save user id and token
      Data._options.AsyncStorage.setItem(TOKEN_KEY, result.token);
      Data._tokenIdSaved = result.token;
      this._userIdSaved = result.id;
      Data.notify('onLogin');
    console.log('Data.notify Change called in handleLogincallback from inside')
    Data.notify('change');
    } else {
      Data.notify('onLoginFailure');
      this.handleLogout();
    }
    Data.notify('change');
  },

I'm really stacking on this and tried all I could.

TheRealNate commented 4 years ago

One more thing I'd like to try, its possible this is related to an issue another person was having. Could you try installing beta version 2.0.10-beta1 of this package?

nastaransamui commented 4 years ago

@TheRealNate I updated to 2.0.10-beta1 but still both Meteor._userIdSaved and Meteor.userId() is null till reload whole app.

TheRealNate commented 4 years ago

Hey @nastaransamui, one more thing to try. Could you update to 2.0.10-beta2?

nastaransamui commented 4 years ago

Hi, @TheRealNate I updated to the new version and logged in successfully. Thanks for all effort. but look like this new version 2.0.10-beta2 has effect on User.connect(); on User.js line 30. I didn't have time to dig more but throw an exception on logout (even log in from Meteor.loginWithPassword or from Accounts.resetPassword. TypeError: User.connect is not a function. (In 'User.connect()', 'User.connect' is undefined) I will investigate and if there is any bug I will open new issue.

TheRealNate commented 4 years ago

HI @nastaransamui, sorry about that. Could you try 2.0.10-beta3?

nastaransamui commented 4 years ago

Hi @TheRealNate in User.Js call import duplicate and prevent to build

import call from '../Call';
import Mongo from '../Mongo';
import {call, connect, isVerbose} from '../Meteor.js';

when import call from import call from '../Call'; only build is successful and log in and resetPassword work as expected but on Logout still exception for connect() in line 30 of User.js

TypeError: (0, _Meteor.connect) is not a function. (In '(0, _Meteor.connect)()', '(0, _Meteor.connect)' is undefined)
  logout(callback) {
    call('logout', err => {
      User.handleLogout();
      connect();

      typeof callback == 'function' && callback(err);
    });
  },
TheRealNate commented 4 years ago

Hey @nastaransamui, sorry about that. Could you try 2.0.13. I've done some extended validation on this release with a production app to ensure there are no issues.

nastaransamui commented 4 years ago

Hey @TheRealNate is it any changed happen to connection, I can't connect to web socket in new version 2.0.13 and Meteor.connect(WEB_SOCKET) return undefined, I didn't change anything from my end but with the last version, I can't connect to WebSocket.

TheRealNate commented 4 years ago

Hi @nastaransamui, there haven't been any changes related to connecting in 2.0.13. Keep in mind that Meteor.connect doesn't return anything.

Because of the fact that some previous beta releases had some bugs, I have done some extensive testing on this release to ensure there are no other bugs.

Is your URL in the format wss://yourapp.domain.com/websocket or ws://yourapp.domain.com/websocket?

nastaransamui commented 4 years ago

@TheRealNate Thank you for your effort version 2.0.13 is work as expected and I will close this.

TheRealNate commented 4 years ago

Hi @nastaransamui just to confirm, the issue with reset password is fixed?

nastaransamui commented 4 years ago

Hi @TheRealNate yes reset password is fixed