提问人:Arooba Masood 提问时间:11/9/2023 最后编辑:Frank van PuffelenArooba Masood 更新时间:11/9/2023 访问量:19
如何处理react native中的firebase错误电子邮件和密码错误?[复制]
How to handle firebase wrong email and password error in react native? [duplicate]
问:
import {
StyleSheet,
Text,
TextInput,
Image,
View,
TouchableOpacity,
Dimensions,
SafeAreaView,
} from "react-native";
import React, { useState } from "react";
import { StatusBar } from "expo-status-bar";
import { useNavigation } from "@react-navigation/native";
import Animated, {
FadeIn,
FadeInDown,
FadeInUp,
} from "react-native-reanimated";
import Icon from "react-native-vector-icons/FontAwesome";
import { signInWithEmailAndPassword } from "firebase/auth";
import { auth } from "../config/firebase";
import { setItem } from "../Utils/AsyncStorage";
const { width, height } = Dimensions.get("window");
export default function LoginScreen() {
const [passwordVisible, setPasswordVisible] = React.useState(false);
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [emailError, setEmailError] = useState("");
const [passwordError, setPasswordError] = useState("");
const navigation = useNavigation();
const handleLogin = async () => {
setEmailError("");
setPasswordError("");
if (!email || !password) {
if (!email) setEmailError("*Please enter email");
if (!password) setPasswordError("*Please enter password");
return;
}
try {
await signInWithEmailAndPassword(auth, email, password);
navigation.navigate("Home");
setItem("LoggedIn", "1");
} catch (error) {
console.log("Firebase error:", error);
if (error.code === "auth/invalid-login-credentials") {
if (error.message.includes("password")) {
setPasswordError("*Wrong password, please try again");
} else {
setEmailError("*User not found, please sign up");
}
} else {
console.log("Error occurred while login:", error.message);
}
}
};
return (
<SafeAreaView style={styles.topContainer}>
<StatusBar style="light" />
<View style={styles.fields}>
<View style={{ flexDirection: "row", alignItems: "center" }}>
<TextInput
placeholder="Email Address"
placeholderTextColor={"gray"}
value={email}
onChangeText={(text) => setEmail(text)}
style={{ flex: 1 }}
/>
<Icon
name="envelope"
size={17}
color="grey"
style={{ marginRight: 5 }}
/>
</View>
</Animated.View>
<Text
style={{
color: "red",
marginTop: -6,
marginRight: "auto",
marginLeft: 8,
marginBottom: 5,
}}
>
{emailError}
</Text>
<Animated.View
entering={FadeInDown.delay(400).duration(1000).springify()}
style={{
backgroundColor: "#f0f0f0",
padding: 13,
borderRadius: 15,
width: width * 0.87,
}}
>
<View style={{ flexDirection: "row", alignItems: "center" }}>
<TextInput
placeholder="Enter password"
placeholderTextColor={"gray"}
value={password}
onChangeText={(text) => setPassword(text)}
secureTextEntry={!passwordVisible}
style={{ flex: 1 }}
/>
<TouchableOpacity
onPress={() => setPasswordVisible(!passwordVisible)}
style={{ padding: 4 }}
>
<Icon
name={passwordVisible ? "eye" : "eye-slash"}
size={20}
color="grey"
/>
</TouchableOpacity>
</View>
</Animated.View>
<Text
style={{
color: "red",
marginTop: -6,
marginRight: "auto",
marginLeft: 8,
}}
>
{passwordError}
</Text>
<View className="ml-auto">
<TouchableOpacity>
<Animated.Text
entering={FadeIn.delay(600).duration(1000).springify()}
style={{
color: "#69ADC6",
fontSize: 13,
marginBottom: 35,
marginTop: -20,
}}
>
Forgot Password?
</Animated.Text>
</TouchableOpacity>
</View>
<Animated.View
entering={FadeInDown.delay(600).duration(1000).springify()}
style={{ width: width * 0.87 }}
>
<TouchableOpacity
onPress={handleLogin}
style={{
backgroundColor: "#23286A",
borderRadius: 15,
marginBottom: 15,
width: width * 0.87,
padding: 12,
}}
>
<Text
style={{
fontWeight: "bold",
color: "white",
fontSize: 20,
textAlign: "center",
}}
>
Login
</Text>
</TouchableOpacity>
</Animated.View>
</View>
</View>
</SafeAreaView>
);
}
我已经编写了此代码,问题是当输入不正确的电子邮件时,它会在电子邮件字段上显示错误,并显示“未找到用户,请注册”。但是,当输入错误的密码时,它不会显示“错误的密码”错误;相反,它仍会在电子邮件输入字段上显示“找不到用户”错误。Firebase 如何处理密码错误的情况?
答: 暂无答案
评论