![](/img/trans.png)
[英][FirebaseError: Firebase: Error (auth/network-request-failed)
[英]Firebase error : Error (auth/network-request-failed
我嘗試使用 Expo 構建一個電子商務應用程序並做出原生反應,但我遇到了網絡錯誤,谷歌服務在設備上處於活動狀態(android 的模擬器和 IOS 的真實 iPhone)並且我已連接到我的 Google 帳戶。
我也嘗試阻止頁面重新加載但沒有任何改變。 經過一些研究,我所看到的是我不是唯一一個面臨這個錯誤的人,我沒有找到任何有價值的答案,所以我就在這里結束了。
這是我的代碼:
const LoginScreen = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [isSignedIn, setIsSignedIn] = useState(false);
const handleCreateAccount = (e) => {
e.preventDefault();
createUserWithEmailAndPassword(authentification, email, password)
.then((userCredential) => {
console.log(userCredential);
})
.catch((error) => {
console.log(error.message);
});
};
const handleSignIn = (e) => {
e.preventDefault();
signWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
console.log("Signed in!");
const user = userCredential.user;
console.log(user);
})
.catch((error) => {
console.log(error);
Alert.alert(error.message);
});
};
<ScrollView
contentContainerStyle={{
flex: 1,
width: "100%",
height: "100%",
alignItems: "center",
justifyContent: "center",
}}
>
<BlurView intensity={100}>
<View style={styles.login}>
<Image
source={{ uri: profilePicture }}
style={styles.profilePicture}
/>
<Text style={{ fontSize: 17, fontWeight: "400", color: "white" }}>
email
</Text>
<TextInput
onChangeText={(text) => setEmail(text)}
style={styles.input}
placeholder="your@email.com"
keyboardType="email-address"
textContentType="emailAddress"
/>
<Text style={{ fontSize: 17, fontWeight: "400", color: "white" }}>
mot de passe
</Text>
<TextInput
onChange={(text) => setPassword(text)}
style={styles.input}
placeholder="your password"
secureTextEntry={true}
/>
<Button
onPress={handleSignIn}
title="Connexion"
buttonStyle={{
width: 250,
height: 40,
borderRadius: 10,
backgroundColor: "#00CFEB90",
alignItems: "center",
justifyContent: "center",
marginVertical: 10,
borderColor: "#fff",
borderWidth: 2,
}}
type="outline"
titleStyle={{ color: "white", fontSize: 17 }}
/>
<Button
onPress={handleCreateAccount}
title="Créer un compte"
buttonStyle={{
width: 250,
height: 40,
borderRadius: 10,
backgroundColor: "#6792F090",
alignItems: "center",
justifyContent: "center",
marginVertical: 10,
borderColor: "#fff",
borderWidth: 2,
}}
type="outline"
titleStyle={{ color: "white", fontSize: 17 }}
/>
</View>
</BlurView>
</ScrollView>
所以我需要幫助。
auth/network-request-failed
錯誤可能特別具有誤導性,因為它似乎是由許多不同的原因引起的。 我已經看到了以下任何一種情況:
如果上述方法都不起作用,請提供您得到的完整錯誤(檢查 React 錯誤和設備級錯誤日志)。
該問題是由為 firebase 身份驗證實現的代碼引起的。
對於 Password InputText,這里使用了onChange屬性,它將 TextInput-Event 設置為密碼 state值。
這導致錯誤為“Firebase 錯誤:錯誤(auth/network-request-failed)”
因為它將事件 object 作為不可接受的密碼發送到 firebase API。
希望你明白了。
進行如下更改以解決您的問題。
onChange={(text) => setPassword(text)} // will set input event as state value
至
onChangeText={(text) => setPassword(text)} // will set entered text as state value
以防它幫助某人。 我遇到了同樣的問題,並且浪費了很多時間進行調查。 事實證明,就我而言,這只是 firebase 層中的 DNS 問題。 模擬器的 URL 中的localhost
未解析。 我用127.0.0.1
替換了它,它工作正常。
這有助於解決錯誤: auth.network-request-failed
while using the iOS Simulator。
如果您遵循文檔並在調試模式下為 FirebaseAuth 實現了仿真。 https://firebase.google.com/docs/emulator-suite/
設置模擬器然后運行:
firebase emulators:start
在那之后我應該工作,你可以在 http://localhost:4000/auth 管理用戶。
我在 ios 模擬器上遇到了這個錯誤,我的解決方案是通過運行sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
希望這對某人有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.