【问题标题】:Google Firebase authentication in ReactNative AppReact Native App 中的 Google Firebase 身份验证
【发布时间】:2021-10-31 16:57:46
【问题描述】:

我正在开发一个我喜欢将 Firebase 实现为身份验证系统的应用。

当我尝试修改保存用户的 firestore 集合时,我尝试使用 Google 提供程序设置身份验证时出现问题。我的代码如下:

export const loginWithGoogle = () => {
  const navigation = useNavigation();
  useEffect(() => {
    setTimeout(() => {
      navigation.navigate('/RegisterScreen');
    }, 10000);
  }, []);

  return () => {
    return firebase
      .auth()
      .signInWithPopup(Providers.google)
      .then(async result => {
        //console.log(result.credential.accessToken);
        const user = result.user;
        console.log(user);

        //This 2 lines below doesn't work to get the colletion.
        db.('users').setItem('userid', user!.uid);
        collection.(db,'users').setItem('photoURL', user!.photoURL);

        //TODO if userid exists IN USERS db then use update IF NULL use set
        await db.collection('users').doc(user!.uid).update({
          // id: user.uid,
          name: user!.displayName,
          email: user!.email,
          phone: user!.phoneNumber,
          photoURL: user!.photoURL,
        });
      })
      .then(() => {
        navigation.navigate('ProtectedScreen');
      })
      .catch(err => {
        console.log(err);
      });
  };
};

所以我猜我的错误来自于不知道如何管理保存在 firestore 上的数据。

如果你能帮忙提前谢谢!

【问题讨论】:

    标签: javascript react-native google-cloud-firestore firebase-authentication


    【解决方案1】:

    这里有一些事情我们需要明确:

    您可以只合并数据。无需从Firestore 读取/获取它以检查它是否存在,如果不存在则保存它。您需要为readswrites 付费。最后,总是只写而不检查是否存在会更便宜。

    这里还有这段代码:

    db.('users').setItem('userid', user!.uid);
    collection.(db,'users').setItem('photoURL', user!.photoURL);
    

    尤其是 db.(collection.( 看起来不太好。即使是不是为了获取数据,而是为了保存它。

    您能否澄清一下您使用的 Firebase SDK:版本 8 或 9。还请检查一下文档 here

    【讨论】:

    • 检查数据是否存在的主要原因是检查是否存在用户的某些uid,如果存在则以不同的方式进行管理。但我的主要问题是,你真的对 sighInWithGoogle 方法做了什么?
    • 我还建议使用firebase.auth().onAuthStateChanged 来获取用户数据。这样你就不需要处理sighInWithGoogle。用户登录后(无论是哪个提供商),您都会获得他的数据,然后您就可以用它做任何您想做的事情。
    • Okey,这是一种最实用的方式,以便您不关心身份验证的类型。我知道我试试!
    • 检查这个答案。稍微详细一点:stackoverflow.com/a/69034201/5519300
    猜你喜欢
    • 1970-01-01
    • 2018-03-10
    • 2017-07-12
    • 2016-12-25
    • 2017-06-13
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多