Firebase 错误:错误(身份验证/网络请求失败

问题描述 投票:0回答:5

我尝试使用 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="[email protected]"
              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>

所以我需要帮助。

javascript firebase react-native firebase-authentication expo
5个回答
12
投票

万一它对某人有帮助。 我遇到了同样的问题并浪费了很多时间进行调查。 事实证明,就我而言,这只是 firebase 层中的 DNS 问题。 模拟器 URL 中的

localhost
未解析。 我用
127.0.0.1
替换了它,并且工作正常。


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 错误和设备级错误日志)。


2
投票
这有助于解决使用 iOS 模拟器时出现的错误:

auth/network-request-failed

如果您遵循文档并在调试模式下实现了 FirebaseAuth 的模拟。

https://firebase.google.com/docs/emulator-suite/

设置模拟器然后运行:

firebase emulators:start
我应该在那之后工作,你可以在 http://localhost:4000/auth 管理用户。


2
投票
我在 ios 模拟器上遇到此错误,我的解决方案是通过运行来刷新我的 mac 上的 dns

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder


希望这对某人有帮助


0
投票
就我而言,问题在于指定模拟器地址和端口的方式。

我有

connectAuthEmulator(auth, "http://127.0.0.1", "9099");
现在我有了

connectAuthEmulator(auth, "http://127.0.0.1:9099");
并且有效。

© www.soinside.com 2019 - 2024. All rights reserved.