App crashes when email or password are wrong with firebase

问题: so I'm using the latest versions of react native, firebase + react-native-form-validator. when I try to log in with an existing user with the right email and password every...

问题:

so I'm using the latest versions of react native, firebase + react-native-form-validator. when I try to log in with an existing user with the right email and password everything is great, but, when I insert wrong email or password the app crashes and I get this error:

c.call is not a function

the relevant lines of code are:

    state = { email: '', password: '', error: '', loading: false }

    userLogin = () => {
            this.setState({ loading: true });

            this.validate({
                email: { required: true, email: true },
                password: { required: true, minlength: 5 }
            });

            if (this.isFormValid()) {
                firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
                .then(() => Actions.partyzmain())
                .catch(setTimeout(() => {
                    this.setState({ error: 'Email or password are inccorect!', password: '', loading: false });
                }, 5000));                 
            }
            else {
                if (this.isFieldInError('password')) {
                    this.setState({ error: 'Wrong password!', password: '', loading: false });
                }
                if (this.isFieldInError('email')) {
                    this.setState({ error: 'Invalid email adress!', loading: false });
                }
            }
    };

I set a .catch error and I really don't understand why isn't it working properly.

many thanks to the answers.


回答1:

Your code seems correct.

Only possible issue may be setTimeout that runs this.setState in different scope as I see in error screen which shows backtrace with timers.

I recommend You to use async/await and handle all throwables using try...catch construction.

For timers You can use such method:

const wait = (timer) => 
  new Promise((resolve) => {
    setTimeout(resolve, timer);
  })

and overall code is:

state = { email: '', password: '', error: '', loading: false }


userLogin = async () => {
  try {
        this.setState({ loading: true });

        this.validate({
            email: { required: true, email: true },
            password: { required: true, minlength: 5 }
        });

        if (!this.isFormValid()) {
            if (this.isFieldInError('password')) {
                this.setState({ 
                  error: 'Wrong password!', 
                  password: '', loading: false 
                });
            }

            if (this.isFieldInError('email')) {
                this.setState({ 
                  error: 'Invalid email address!', 
                  loading: false 
                });
            }
            return;
        }

        await firebase
                .auth()
                .signInWithEmailAndPassword(
                  this.state.email, 
                  this.state.password
                );
        Actions.partyzmain();
  }
  catch (error) {
      console.debug(error);

      if (error.code.startsWith('auth')) {
        await wait(1000);
        this.setState({ 
          error: 'Email or password are inccorect!', 
          password: '', 
          loading: false 
        });
      }
  }
};
  • 发表于 2019-02-18 16:39
  • 阅读 ( 231 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除