目前,此代码生成一个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;
}
}
}
你必须使用signInSuccessWithAuthResult
中引入的v2.7.0回调
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;
},
...
},
...
};