如何在flutter中链接firebase_auth 0.11.1中的firebase帐户? [英] How do you link firebase accounts in firebase_auth 0.11.1 in flutter?
问题描述
我已经使用firebase_auth 0.11.0成功地实现了Flutter的Google和Facebook登录.我需要为具有相同电子邮件地址的帐户实现链接帐户功能.
I've successfully implemented Google and Facebook sign ins for Flutter using firebase_auth 0.11.0. I need to implement the linking account feature for accounts that have the same email address.
Future<FirebaseUser> _signInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
您如何/在何处捕获Firebase错误(auth/account-exists-with-different-credential),该帐户是重复的电子邮件,因此需要linkwithcredential
功能?我试图在上面的signInWithCredential
和_signInWithGoogle()
上捕获错误,但无济于事.
How/where do you catch the Firebase error (auth/account-exists-with-different-credential) that the account is a duplicate email and therefore requires the linkwithcredential
function? I've tried to catch the error on signInWithCredential
and _signInWithGoogle()
above to no avail.
基于此处的示例:
FirebaseUser user = await auth.currentUser();
user = await user.linkWithCredential(credential);
FirebaseUser user = await auth.currentUser();
user = await user.linkWithCredential(credential);
这是链接帐户的方式,但是我的问题是如何确定需要运行此功能?
This is how you link the accounts, but my question is how do you determine that this function needs to be run?
为清楚起见,当只有一个凭据/会话时,我可以链接帐户吗?例如,该流应为,使用Facebook登录,捕获(ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL),然后链接到Google.
for clarity, can I link the accounts when only one credential/session exists? For example, the flow should be, sign in with Facebook, catch (ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL), and then link to Google.
推荐答案
要链接google和facebook,我正在使用:
To link google and facebook, I'm using:
FacebookLoginResult facebookLoginResult = await _handleFBSignIn();
final accessToken = facebookLoginResult.accessToken.token;
if (facebookLoginResult.status == FacebookLoginStatus.loggedIn) {
final facebookAuthCred =
FacebookAuthProvider.getCredential(accessToken: accessToken);
final user = await auth.signInWithCredential(facebookAuthCred).catchError((e) async {
if (e.code == 'ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL') {
var graphResponse = await http.get(
'https://graph.facebook.com/v2.12/me?fields=name,first_name,last_name,email,picture.width(400)&access_token=${facebookLoginResult
.accessToken.token}');
Map<String, dynamic> profile = json.decode(graphResponse.body);
profileData = profile;
var email = profile["email"];
final signInMethods = await FirebaseAuth.instance
.fetchSignInMethodsForEmail(email: email);
if (signInMethods.contains("google.com")) {
final authGoogleResult = await googleLogin();
if (authGoogleResult.user.email == e.email) {
await authGoogleResult.user.linkWithCredential(e.credential);
}
}
}
});
这篇关于如何在flutter中链接firebase_auth 0.11.1中的firebase帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!