Firebase Google Authentication - Check if User Logs in for first time

南楼画角 提交于 2019-12-13 10:24:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!