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



如何在react native中使用firebase检测登录用户是现有用户还是新用户。我已经使用谷歌身份验证创建了身份验证,但不幸的是,我没有得到任何名为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": "*******"}

我现在的问题是,每当用户成功验证谷歌签名方法时,它都会将数据添加到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) {
...
}
}
}

另请参阅:

  • AdditionalUserInfo的文档
  • UserMetadata的文档

相关内容

最新更新