Firebase Google身份验证-检查用户是否首次登录 [英] Firebase Google Authentication - Check if User Logs in for first time

查看:83
本文介绍了Firebase Google身份验证-检查用户是否首次登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上使用了也支持Google登录的注册/登录,问题是我在Firestore中创建了一个新的数据文档,并在其中注册了uid,但我无法对Google登录执行此操作,否则会擦除现有条目,因为我只能通过Google登录,但是我需要一种方法来检查用户是否已经存在.

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;
      // ...
      });
},

推荐答案

您可以检查result.additionalUserInfo.isNewUser来查看用户是否是第一次登录. signInWithPopup返回的结果是UserCredential对象: https ://firebase.google.com/docs/reference/js/firebase.auth#.UserCredential

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

这篇关于Firebase Google身份验证-检查用户是否首次登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆