问题
I use a Register/Login on my Website which also supports Google Login, the problem is i make a new Data Document in the Firestore with the uid on register, but i cannot do that for the Google Login otherwise it would wipe the existing entry, because i can only SignIn with Google but i need a way to check if the User already exists.
googlelogin: function()
{
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
db.collection("user").doc(firebase.auth().currentUser.uid).set({
name: firebase.auth().currentUser.email,
favorites: []
})
$('#modal1').modal('close');
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
},
回答1:
You can check result.additionalUserInfo.isNewUser
to see if the user is signing in the first time. The result returned by signInWithPopup
is an UserCredential
object: https://firebase.google.com/docs/reference/js/firebase.auth#.UserCredential
来源:https://stackoverflow.com/questions/51273940/firebase-google-authentication-check-if-user-logs-in-for-first-time