Firebase signIn issue in React Native Expo production build – Crashes after selecting Google account in SigninScreen

huangapple go评论52阅读模式
英文:

Firebase signIn issue in React Native Expo production build - Crashes after selecting Google account in SigninScreen

问题

Firebase在React Native Expo生产版本中的登录问题 - 选择Google帐号后崩溃

我在我的React Native应用中遇到了一个严重问题,即在登录过程中选择Google帐号后,应用会在生产版本中突然崩溃,但在开发版本(Expo)中,应用却可以正常工作,没有崩溃。下次应用打开时,checkedSignIn函数正常工作,这意味着登录成功,但在Firestore中未创建文档。

概述:

  • 问题特别发生在我的React Native应用的生产版本中。
  • 崩溃发生在选择Google帐号后的登录过程中。
  • 预期行为是完成登录过程并将用户设置为已登录。
  • 在开发版本(Expo)中,应用可以正常工作,没有崩溃或问题。

编辑 [经过进一步记录,似乎代码在这一行停止执行,之后没有显示更多的日志并且应用程序崩溃:]

 const { user } = await auth().signInWithCredential(googleCredential);

相关代码:
以下是来自我的useAuth钩子的相关代码片段:

// ...

const signInWithGoogle = async () => {
  try {
    setLoading(true);
    await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
    const { idToken } = await GoogleSignin.signIn();
    const googleCredential = auth.GoogleAuthProvider.credential(idToken);
    const { user } = await auth().signInWithCredential(googleCredential);
    await setUserInFirestore(user);
    

  } catch (error) {
    setError(error);
    console.log("Error signing in with Google:", error);
  }
};

// ...
const setUserInFirestore = async (user) => {
    const { uid, displayName, email, photoURL } = user;

    try {
      const userRef = doc(db, "users", uid);
      const snapshot = await getDoc(userRef);

      if (!snapshot.exists()) {
        await setDoc(userRef, {
          displayName,
          email,
          photoURL,
          uid,
          subscription: null,
        });
      }

      setUser(user);
      setUserContext(user);
    } catch (error) {
      console.log("Error adding user to Firestore:", error.message);
      navigation.navigate(routes.WELCOME);
    } finally {
      setLoading(false);
    }
  };

版本:

  • React Native: 0.71.8
  • Expo: ~48.0.15
  • @react-native-firebase/app: ^17.4.3
  • @react-native-firebase/auth: ^17.4.3
  • @react-native-google-signin/google-signin: ^9.0.2
  • Firebase: ^9.21.0
英文:

Firebase signIn issue in React Native Expo production build - Crashes after selecting Google account in SigninScreen

I am facing a critical issue in my React Native app where the app crashes abruptly after selecting a Google account during the signin process, but only in the production build. In the development build (Expo), the app works fine without any crashes. The next time the app opens the checkedSignIn function works fine which means sign in was successful but the doc is not created in the firestore.

Overview:

  • The issue occurs specifically in the production build of my React Native app.
  • The crash happens after selecting a Google account during the signin process.
  • The expected behavior is to complete the signin process and setUser() to the logged in user.
  • The app works fine in the development build (Expo) without any crashes or issues.

Edit [ after further logging it looks like the code stops at this line as no further logs are shown after this and app crashes: ]

 const { user } = await auth().signInWithCredential(googleCredential);

Relevant Code:
Here is the relevant code snippet from my useAuth hook:


// ...

const signInWithGoogle = async () => {
  try {
    setLoading(true);
    await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
    const { idToken } = await GoogleSignin.signIn();
    const googleCredential = auth.GoogleAuthProvider.credential(idToken);
    const { user } = await auth().signInWithCredential(googleCredential);
    await setUserInFirestore(user);
    

  } catch (error) {
    setError(error);
    console.log("Error signing in with Google:", error);
  }
};

// ...
const setUserInFirestore = async (user) => {
    const { uid, displayName, email, photoURL } = user;

    try {
      const userRef = doc(db, "users", uid);
      const snapshot = await getDoc(userRef);

      if (!snapshot.exists()) {
        await setDoc(userRef, {
          displayName,
          email,
          photoURL,
          uid,
          subscription: null,
        });
      }

      setUser(user);
      setUserContext(user);
    } catch (error) {
      console.log("Error adding user to Firestore:", error.message);
      navigation.navigate(routes.WELCOME);
    } finally {
      setLoading(false);
    }
  };

Versions:

  • React Native: 0.71.8
  • Expo: ~48.0.15
  • @react-native-firebase/app: ^17.4.3
  • @react-native-firebase/auth: ^17.4.3
  • @react-native-google-signin/google-signin: ^9.0.2
  • Firebase: ^9.21.0

答案1

得分: 1

对我来说问题在于我需要打开Xcode并且:

  1. 进入我的GoogleService-Info.plist文件
  2. 复制REVERSED_CLIENT_ID的值
  3. 导航到项目的TARGETS并选择我的应用程序
  4. 选择Info选项卡,然后导航到URL Types部分
  5. 通过点击加号(+)图标向URL Types部分添加一个新项目
  6. REVERSED_CLIENT_ID的值粘贴到URL Schemes字段中。

如果您认为这些说明不够清晰,您可以观看此视频,视频在5:20处演示了这些步骤。

英文:

For me the issue was that I needed to open Xcode and:

  1. Go to my GoogleService-Info.plist file
  2. Copy the value of the REVERSED_CLIENT_ID
  3. Navigate to the project's TARGETS and select my app
  4. Select the Info tab and navigate to the URL Types section
  5. Add a new item to the URL Types section by tapping on the plus (+) icon
  6. Paste the value of the REVERSED_CLIENT_ID in the URL Schemes field.

In case you feel these instructions are not clear enough, you can watch this video that goes through these steps at 5:20

huangapple
  • 本文由 发表于 2023年5月24日 18:01:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定