React Native Firebase身份验证错误

时间:2017-07-19 18:35:00

标签: reactjs firebase react-native firebase-authentication

我试图创建登录用户帐户系统,由于某种原因,导航到主屏幕而不是之前识别出错误的密码。 (我所指的代码是login()函数)。我把控制台日志语句确定为和' console.log(errorCode);'是输出的最后一件事。有人可以解释我写的代码的逻辑,以及为什么不正确的密码最终只能被识别? 控制台输出的顺序是 "登录" "导航到Home" "登录结束" " AUTH /错密码? 非常感谢。

import React, {Component} from 'react';
import {
  View,
  Text,
  TouchableHighlight,
  TouchableOpacity,
  TextInput,
  KeyboardAvoidingView
} from 'react-native';
import Input from './Input';
import Icon from 'react-native-vector-icons/MaterialIcons';
import {firebaseApp} from './App';
import {Tabs} from './Router';
import {StackNavigator, TabNavigator} from 'react-navigation';
import { Root } from './Router';

export default class LoginScreen extends Component {
  constructor(props) {
    super(props)

    this.state = {
      email: '',
      password: '',
      status: '',
      success: ''
    }

    this.login = this.login.bind(this);

  }

  login(){
    console.log("Logging in");
    var errorCode;
    var errorMessage;
    firebaseApp.auth().signInWithEmailAndPassword(this.state.email, this.state.password).catch(function(error) {
        errorCode = error.code;
        console.log(errorCode);
        errorMessage = error.message;
    });

      if (errorCode === 'auth/wrong-password') {
        console.log("Wrong password");
        alert('Wrong password.');
      } else {
        console.log("Navigate to Home");
        this.props.navigation.navigate('Home');
      }

      //console.log(error);
      console.log("Login finish");


    /*firebaseApp.auth().signInWithEmailAndPassword(this.state.email, this.state.password).catch(function(error) {
      console.log(error.code);
      console.log(error.message);
    })

    this.props.navigation.navigate('Home');

    console.log("Navigate to Home");*/
  }

  render() {
    var styles = require('./Styles');
    const {navigate} = this.props.navigation;

    return(
      <KeyboardAvoidingView behavior='padding' style={styles.loginContainer}>
        <Text style={styles.loginHeader}>PRINCETON EVENTS</Text>
        <TextInput
          style={styles.loginInput}
          placeholder="Email"
          autoCapitalize='none'
          onChangeText={(text) => this.setState({email: text})}
          value={this.state.email}
          returnKeyType='next'/>
        <TextInput
          secureTextEntry
          style={styles.loginInput} placeholder="Password"
          onChangeText={(text) => this.setState({password: text})}
          value={this.state.password}
          autoCapitalize='none'
          returnKeyType='go'/>
        <TouchableOpacity style={styles.loginButton}>
          <Text style={styles.loginText} onPress={this.login}>LOGIN</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={styles.loginButton}
          onPress = {() => navigate('CreateAccount')}>
          <Text style={styles.loginText}> CREATE ACCOUNT </Text>
        </TouchableOpacity>
      </KeyboardAvoidingView>
    );

}
}

2 个答案:

答案 0 :(得分:0)

您需要处理成功/错误案例然后捕获块。

firebaseApp.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
.then(function() { //Auth is successful
   this.props.navigation.navigate('Home');
}
.catch(function(error) {
    errorCode = error.code;
    errorMessage = error.message;
  if (errorCode === 'auth/wrong-password') {
    console.log("Wrong password");
    alert('Wrong password.');
  } else {
    console.log("Navigate to Home");
    this.props.navigation.navigate('Home');
  }
});

答案 1 :(得分:0)

有点晚了,但对于下一个,请检查此答案 https://stackoverflow.com/a/63958584/9300663

仍在工作:13/01/2021。

对于您的情况,您可以使用 substr() 从您的错误代码中删除“auth/”。

类似的东西:

switch (errorCode.substr(5)) {
    case 'ERROR_EMAIL_ALREADY_IN_USE':
    case 'account-exists-with-different-credential':
    case 'email-already-in-use':
      return 'Email already used. Go to login page.';
    case 'ERROR_WRONG_PASSWORD':
    case 'wrong-password':
      return 'Wrong email/password combination.';
    case 'ERROR_USER_NOT_FOUND':
    case 'user-not-found':
      return 'No user found with this email.';
    case 'ERROR_USER_DISABLED':
    case 'user-disabled':
      return 'User disabled.';
    case 'ERROR_TOO_MANY_REQUESTS':
    case 'operation-not-allowed':
      return 'Too many requests to log into this account.';
    case 'ERROR_OPERATION_NOT_ALLOWED':
    case 'operation-not-allowed':
      return 'Server error, please try again later.';
    case 'ERROR_INVALID_EMAIL':
    case 'invalid-email':
      return 'Email address is invalid.';
    default:
      return 'Login failed. Please try again.';
  }