D.H*_*ges 5 firebase firebase-authentication angularfire2 angular ionic4
什么时候使用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);
}Run Code Online (Sandbox Code Playgroud)
如果我正确理解了您的问题,则认为您的问题出自以下两个事实:
正如解释在这里,signInWithCredential()返回一个User对象,
而
正如解释在这里,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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2321 次 |
| 最近记录: |