Firebase 错误:错误(auth/网络请求失败

Gan*_*nzo 4 javascript firebase react-native firebase-authentication expo

我尝试使用 Expo 构建一个电子商务应用程序并做出本机反应,但我遇到网络错误,谷歌服务在设备上处于活动状态(Android 模拟器和 IOS 真实 iPhone),并且我已连接到我的 Google 帐户。

\n

我也尝试阻止页面重新加载,但没有任何改变。经过一番研究后,我发现我并不是唯一一个面临此错误的人,我没有找到任何有价值的答案,所以我最终来到这里。

\n

这是我的代码:

\n
const LoginScreen = () => {\n  const [email, setEmail] = useState("");\n  const [password, setPassword] = useState("");\n  const [isSignedIn, setIsSignedIn] = useState(false);\n\n  const handleCreateAccount = (e) => {\n    e.preventDefault();\n    createUserWithEmailAndPassword(authentification, email, password)\n      .then((userCredential) => {\n        console.log(userCredential);\n      })\n      .catch((error) => {\n        console.log(error.message);\n      });\n  };\n\n  const handleSignIn = (e) => {\n    e.preventDefault();\n    signWithEmailAndPassword(auth, email, password)\n      .then((userCredential) => {\n        console.log("Signed in!");\n        const user = userCredential.user;\n        console.log(user);\n      })\n      .catch((error) => {\n        console.log(error);\n        Alert.alert(error.message);\n      });\n  };\n\n      <ScrollView\n        contentContainerStyle={{\n          flex: 1,\n          width: "100%",\n          height: "100%",\n          alignItems: "center",\n          justifyContent: "center",\n        }}\n      >\n        <BlurView intensity={100}>\n          <View style={styles.login}>\n            <Image\n              source={{ uri: profilePicture }}\n              style={styles.profilePicture}\n            />\n            <Text style={{ fontSize: 17, fontWeight: "400", color: "white" }}>\n              email\n            </Text>\n            <TextInput\n              onChangeText={(text) => setEmail(text)}\n              style={styles.input}\n              placeholder="your@email.com"\n              keyboardType="email-address"\n              textContentType="emailAddress"\n            />\n            <Text style={{ fontSize: 17, fontWeight: "400", color: "white" }}>\n              mot de passe\n            </Text>\n            <TextInput\n              onChange={(text) => setPassword(text)}\n              style={styles.input}\n              placeholder="your password"\n              secureTextEntry={true}\n            />\n            <Button\n              onPress={handleSignIn}\n              title="Connexion"\n              buttonStyle={{\n                width: 250,\n                height: 40,\n                borderRadius: 10,\n                backgroundColor: "#00CFEB90",\n                alignItems: "center",\n                justifyContent: "center",\n                marginVertical: 10,\n                borderColor: "#fff",\n                borderWidth: 2,\n              }}\n              type="outline"\n              titleStyle={{ color: "white", fontSize: 17 }}\n            />\n            <Button\n              onPress={handleCreateAccount}\n              title="Cr\xc3\xa9er un compte"\n              buttonStyle={{\n                width: 250,\n                height: 40,\n                borderRadius: 10,\n                backgroundColor: "#6792F090",\n                alignItems: "center",\n                justifyContent: "center",\n                marginVertical: 10,\n                borderColor: "#fff",\n                borderWidth: 2,\n              }}\n              type="outline"\n              titleStyle={{ color: "white", fontSize: 17 }}\n            />\n          </View>\n        </BlurView>\n      </ScrollView>\n
Run Code Online (Sandbox Code Playgroud)\n

所以我需要帮助。

\n

Ren*_*aud 12

万一它对某人有帮助。 我遇到了同样的问题并浪费了很多时间进行调查。事实证明,就我而言,这只是 firebase 层中的 DNS 问题。 localhost模拟器的 URL 中的内容未解析。我用它替换了它127.0.0.1并且工作正常。


Tim*_*Tim 5

auth/network-request-failed错误可能特别具有误导性,因为它似乎是由于多种不同的原因而出现的。我见过它有以下任一情况:

  1. SDK 版本错误- 要在本机设备上使用身份验证,您需要使用本机 SDK,而不是JavaScript SDK。其他一些 Firebase 功能可能通过 JS SDK 工作,但身份验证则不能,因为它有额外的安全层。确保您使用的是正确的版本。
  2. 模拟器问题- 您的模拟器无法访问互联网、DNS 过时或系统时间不正确。检查并确保您的设备可以访问互联网,设备上的时钟设置正确,并且项目的 DNS 可以解析。对于 DNS,您可以尝试切换到 Google 的 DNS (8.8.8.8) 或 Cloudflare 的 (1.1.1.1),看看这些是否有帮助。
  3. 无 Google Play 服务- 在 Android 上,身份验证要求在设备模拟器上安装 Google Play 服务。详细信息请参见此处
  4. Firebase 设置不正确- 确保您已遵循ExpoReact Native Firebase的入门说明中的每一项操作,特别是凭据部分,并确保包名称在 Android 上匹配,捆绑包 ID 在 iOS 上匹配。

如果上述方法均不起作用,请提供您收到的完整错误(检查 React 错误和设备级错误日志)。