【问题标题】:Firebase error : Error (auth/network-request-failedFirebase 错误:错误(身份验证/网络请求失败
【发布时间】:2022-08-15 02:12:42
【问题描述】:

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

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

这是我的代码:

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>

所以我需要帮助。

  • 网页版也有同样的问题,但是,只在 iOS 设备上。它适用于其他设备。
  • 我认为是表单/文本输入的代码,因为我在另一个具有不同类型表单的项目中尝试了相同的功能,并且它在 android 模拟器和 Iphone 上工作。

标签: javascript firebase react-native firebase-authentication expo


【解决方案1】:

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 服务。 See here for details
  4. Firebase 设置不正确- 确保您已按照 ExpoReact Native Firebase 的入门说明中的每一项操作,尤其是凭据部分,并确保包名称在 Android 上匹配,Bundle ID 在 iOS 上匹配。

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

【讨论】:

【解决方案2】:

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

如果错误消息没有隐藏堆栈跟踪并且提供了必要的堆栈跟踪,那将会非常容易。

【讨论】:

    【解决方案3】:

    该问题是由为 firebase auth 实现的代码引起的。

    对于密码输入文本, 这里改变使用道具将 TextInput-Event 设置为密码状态价值。

    这导致错误为“Firebase 错误:错误(auth/network-request-failed)”

    因为它将事件对象作为不可接受的密码发送到 firebase API。

    希望你明白了。

    进行如下更改以解决您的问题。

    onChange={(text) => setPassword(text)} // will set input event as state value
    

    onChangeText={(text) => setPassword(text)} // will set entered text as state value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 2017-12-27
      • 2022-11-01
      • 2018-11-14
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多