spencercarli / react-native-meteor-boilerplate

MIT License
628 stars 139 forks source link

Send error after wrong sign in #40

Closed ninjagrizzly closed 8 years ago

ninjagrizzly commented 8 years ago

If i am trying to sign in with wrong login and password, Meteor send error, this is the handler

handleSignIn() {
    if (this.validInput(true)) {
      const { username, password } = this.state;
      Meteor.loginWithPassword(username, password, (err) => {
        if (err) {
          this.setState({ error: err.reason });
        }
      });
    }
  }

This code trying to this.setState, but after loginWithPassword the loggingIn status was changed, new <LoggedOut /> component was created, so this context specifies to old component and this.setState as a result working with unmounted component. Can you advice some elegant method to avoid it, please?)

thebigsmall commented 8 years ago

This experience really helped me.. and.. How about if we do it this way ...

const RNApp = (props) => {
  const { status, user, loggingIn } = props;
  console.log('props', props);

  if (status.connected === false){
    return <Loading />;

  } else if(user !== null){
    return <LoggedIn />;

  } else {
    return <LoggedOut />;
  }
};
handleSignIn() {
    const { username, email, password, confirmPasswordInputVisible } = this.state;

    if (!confirmPasswordInputVisible && this.validInput(true)) {
      //NOTE because react-native-meteor doesn't login right away after sign in
      //Meteor.loginWithPassword(email, password, (err) => {
      Meteor.loginWithPassword(username, password, (err) => {
        if (err) {
          console.log(err.reason);
          this.setState({ confirmPasswordInputVisible: true });
        } else {
          console.log('login success!');
          this.setState({ error: 'no error'});
        }
      });
    } else {
      if(confirmPasswordInputVisible !== false) {
        LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
        this.setState({ confirmPasswordInputVisible: false });
        this.setState({ error: null });
      }
    }
  }

handleCreateAccount() {
    const { username, email, password, confirmPasswordInputVisible } = this.state;

    if(confirmPasswordInputVisible && this.validInput()) {
      //Account.createUser({ email, password }, (err) => {
      Accounts.createUser({ username: username, password: password }, (err) => {
        if(err) {
          this.setState({ error: err.reason });
        } else {
          //NOTE because react-native-meteor doesn't login right away after sign in
          this.setState({ confirmPasswordInputVisible: false });
          Meteor.loginWithPassword(username, password, (err) => {
            if (err) {
              this.setState({
                error: err.reason,
                confirmPasswordInputVisible: true
              });
            } else {
              console.log('login success!');
            }
          });

        }
      });
    } else {
      if(confirmPasswordInputVisible !== true) {
        LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
        this.setState({ confirmPasswordInputVisible: true });
        this.setState({ error: null });
      }
    }
  }
spencercarli commented 8 years ago

This is very similar to #21 so I'm going to close this in favor of consolidating issues. I haven't thought through the issue yet but it's on my radar!