如何使用Firebase UI在Firebase中访问电子邮件帐户的isNewUser?

时间:2018-08-31 22:44:18

标签: javascript firebase firebase-authentication

当前,此代码产生Firebase错误消息,内容为“无法获取未定义或空引用的属性'isNewUser'”。如何解决此问题,即如何确定用户是否是登录的新用户?

var uiConfig = {
        signInSuccessUrl: false,
        signInOptions: [
           firebase.auth.EmailAuthProvider.PROVIDER_ID
        ],
        tosUrl: '',
        'credentialHelper': firebaseui.auth.CredentialHelper.NONE,
        privacyPolicyUrl: function() {
          window.location.assign('');
        },
        'callbacks': { 'signInSuccess': function(user, credential, redirectUrl) {
                if (user) {
                  var displayName = user.displayName;
                  var email = user.email;
                  var emailVerified = user.emailVerified;
                  var photoURL = user.photoURL;
                  var uid = user.uid;
                  var phoneNumber = user.phoneNumber;
                  var providerData = user.providerData;
                  user.getIdToken().then(function(accessToken) {
                    $("#message").text(uid);
                  });
                  if(firebase.auth.AdditionalUserInfo.isNewUser) {

                    db.collection("users").doc(uid).set({
                        firstName: displayName
                    });
                  }
                } else {
                  $("#message").text('signed out');
                }
                return false; 
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您必须使用v2.7.0

中引入的signInSuccessWithAuthResult回调
var uiConfig = {
    callbacks: {
      signInSuccessWithAuthResult: function(authResult, redirectUrl) {
        var user = authResult.user;
        var credential = authResult.credential;
        // Determine if user is new or existing.
        var isNewUser = authResult.additionalUserInfo.isNewUser;
        var providerId = authResult.additionalUserInfo.providerId;
        var operationType = authResult.operationType;
        return false;
      },
      ...
    },
    ...
};