簡體   English   中英

Firebase 錯誤:錯誤(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錯誤可能特別具有誤導性,因為它似乎是由許多不同的原因引起的。 我已經看到了以下任何一種情況:

  1. Wrong Version of the SDK - To use auth on native devices, you need to use the native SDK, not the JavaScript SDK. 其他一些 Firebase 功能可以通過 JS SDK 工作,但身份驗證不會,因為它有額外的安全層。 確保您使用的是正確的版本。
  2. 仿真器問題- 您的仿真器無法訪問 Internet 或具有陳舊的 DNS 或不正確的系統時間。 檢查並確保您的設備可以訪問互聯網,設備上的時鍾設置正確,並且您的項目的 DNS 已解決。 對於 DNS,您可以嘗試切換到 Google 的 DNS (8.8.8.8) 或 Cloudflare 的 (1.1.1.1),看看是否有任何幫助。
  3. 沒有 Google Play 服務- 在 Android 上,身份驗證需要在設備模擬器上安裝 Google Play 服務。 詳情請看這里
  4. Incorrect Firebase Setup - Make sure you've followed every item in the Getting Started instructions from both Expo and React Native Firebase , in particular the credentials portions and making sure that the package names match on Android and Bundle IDs match on iOS.

如果上述方法都不起作用,請提供您得到的完整錯誤(檢查 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM