反应本机 Firebase 身份验证错误
Posted
技术标签:
【中文标题】反应本机 Firebase 身份验证错误【英文标题】:React Native Firebase Authentication Error 【发布时间】:2017-12-25 04:41:20 【问题描述】:我正在尝试创建登录用户帐户系统,但由于某种原因,它在导航到主屏幕后而不是之前识别出不正确的密码。 (我指的代码是 login() 函数)。我放置了控制台日志语句来确定这一点和“console.log(errorCode);”是最后输出的东西。有人可以解释我编写的代码的逻辑以及为什么只在最后识别不正确的密码吗? 控制台输出的顺序是 “在登录” “导航到主页” “登录完成” “授权/密码错误? 非常感谢。
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>
);
【问题讨论】:
【参考方案1】:您需要在 then 和 catch 块中处理成功/错误情况。
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');
);
【讨论】:
【参考方案2】:有点晚了,但是对于下一个,请查看此答案https://***.com/a/63958584/9300663
仍在工作:2021 年 1 月 13 日。
对于您的情况,您可以使用 substr() 从您的 errorCode 中删除 '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.';
【讨论】:
以上是关于反应本机 Firebase 身份验证错误的主要内容,如果未能解决你的问题,请参考以下文章