什么时候使用this.afAuth.auth.signInWithCredential在我的Ionic应用程序中一切都很好用。我可以使用Google身份验证登录,并将用户个人资料推送到firebase。但是,我在控制台中收到一个错误,即不推荐使用signInWithCredential并使用signInAndRetrieveDataWithCredential。问题是signInAndRetrieveDataWithCredential不提供任何用户数据(uid,用户电子邮件,显示名称)......它全部为空。
那我该怎么办?控制台错误说即使它正在工作也不要使用signInWithCredential。 signInAndRetrieveDataWithCredential为用户返回null。
async googleLogin(): Promise<void> {
try {
const gplusUser = await this.gplus.login({
'webClientId': environment.googleWebClientId,
'offline': true,
'scopes': 'profile email'
});
return await this.afAuth.auth.signInWithCredential(
firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken)
).then((credential) => {
console.log('creds', credential);
this.updateUserData(credential);
});
} catch (err) {
console.log(err);
}
}
private updateUserData(user) {
const userRef: firebase.firestore.DocumentReference = firebase.firestore().doc(`users/${user.uid}`);
const data: User = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
};
console.log('user data', data);
return userRef.set(data);
}
如果我正确理解了您的问题,我认为您的问题来自两个方法返回不同对象的事实:
正如here所解释的那样,signInWithCredential()
返回一个User
对象,
而
正如here所解释的那样,signInAndRetrieveDataWithCredential()
返回一个UserCredential
对象,其中包含一个User
对象。
所以你应该修改你的代码如下
....
return await this.afAuth.auth.signInAndRetrieveDataWithCredential(
firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken)
).then((credential) => {
console.log('creds', credential.user);
this.updateUserData(credential.user);
});
我只想澄清signInAndRetrieveDataWithCredential
,而不是signInWithCredential
,已经在Firebase JS SDK的version 6中弃用了。
此外,signInWithCredential
函数现已更新为返回UserCredential
,因此您应该能够访问UserCredential
上的用户对象下的uid,email等。