Javascript 如何检测google auth登录用户是否为新用户,是否在react native中使用firebase

Javascript 如何检测google auth登录用户是否为新用户,是否在react native中使用firebase,javascript,firebase,react-native,google-cloud-firestore,firebase-authentication,Javascript,Firebase,React Native,Google Cloud Firestore,Firebase Authentication,如何在react native中使用firebase检测登录用户是现有用户还是新用户。我已经使用GoogleAuth创建了身份验证,但不幸的是,我没有得到任何名为isNewUser的字段作为回报 下面是我的代码 async function onGoogleButtonPress() { // Get the users ID token const {idToken} = await GoogleSignin.signIn(); // Create a Google

如何在react native中使用firebase检测登录用户是现有用户还是新用户。我已经使用GoogleAuth创建了身份验证,但不幸的是,我没有得到任何名为isNewUser的字段作为回报

下面是我的代码

async function onGoogleButtonPress() {
    // Get the users ID token
    const {idToken} = await GoogleSignin.signIn();

    // Create a Google credential with the token
    const googleCredential = auth.GoogleAuthProvider.credential(idToken);

    // Sign-in the user with the credential
    return auth().signInWithCredential(googleCredential);
  }

  function onAuthStateChanged(user) {
    if (user) {
      firestore()
        .collection('Users')
        .doc(user.uid)
        .set({
          user: user.displayName,
        email: user.email,
        photo: user.photoURL,
        })
        .then(() => {
          console.log('User added!');
        });
    }
    if (initializing) setInitializing(false);
  }


 useEffect(() => {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
    return subscriber; // unsubscribe on unmount
  });
这是我得到的回应

 {"displayName": "***", "email": "**@gmail.com", "emailVerified": true, "isAnonymous": false, "metadata": {"creationTime": 15960**412290, "lastSignInTime": 15960**65185}, "phoneNumber": null, "photoURL": "**", "providerData": [[Object]], "providerId": "firebase", "uid": "*******"}
我现在的问题是,每次用户成功验证google signin方法后,都会向firebase数据库添加数据。有什么方法可以检测到用户是新用户还是现有用户


帮助将非常有用:)

isNewUser属性位于
UserCredential
对象中,该属性仅在调用
signInWithCredential
后才可用

const credentialPromise = auth().signInWithCredential(googleCredential);
credentialPromise.then((credential) => {
  console.log(credential.additionalUserInfo.isNewUser);
})

通过将用户的创建时间戳与其上次登录进行比较,您可以确定该用户是否是来自身份验证状态侦听器的新用户:

function onAuthStateChanged(user) {
  if (user) {
    if (user.metadata.creationTime <> user.metadata.lastSignInTime) {
      ...
    }
  }
}
AuthStateChanged上的函数(用户){ 如果(用户){ if(user.metadata.creationTime user.metadata.lastsignentime){ ... } } }
另见:

  • 有关的文档
  • 有关的文档

为什么不在编写前检查每个用户的文档是否存在?如果它不存在,那么您知道它是一个新用户。