کیتهای توسعه نرمافزار (SDK) احراز هویت فایربیس (Firebase Authentication SDK) روشی ساده برای شناسایی خطاهای مختلفی که ممکن است در هنگام استفاده از روشهای احراز هویت رخ دهند، ارائه میدهند. کیتهای توسعه نرمافزار (SDK) فلاتر (Flutter) این خطاها را از طریق کلاس FirebaseAuthException افشا میکنند.
حداقل، یک code و message ارائه میشود، با این حال در برخی موارد، ویژگیهای اضافی مانند آدرس ایمیل و اعتبارنامه نیز ارائه میشوند. به عنوان مثال، اگر کاربر در تلاش برای ورود به سیستم با ایمیل و رمز عبور باشد، هرگونه خطایی که رخ میدهد را میتوان به صراحت دریافت کرد:
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: "barry.allen@example.com",
password: "SuperSecretPassword!"
);
} on FirebaseAuthException catch (e) {
print('Failed with error code: ${e.code}');
print(e.message);
}
هر روش بسته به نوع فراخوانی احراز هویت، کدهای خطا و پیامهای مختلفی ارائه میدهد. API مرجع، جزئیات بهروزی در مورد خطاها برای هر روش ارائه میدهد.
اگر به سهمیه احراز هویت Firebase برسید یا یک ارائه دهنده احراز هویت خاص را فعال نکرده باشید، ممکن است خطاهای دیگری مانند too-many-requests یا operation-not-allowed رخ دهد.
مدیریت خطاهای account-exists-with-different-credential
اگر تنظیمات «یک حساب کاربری به ازای هر آدرس ایمیل» را در کنسول Firebase فعال کرده باشید، وقتی کاربری سعی میکند با ایمیلی که از قبل برای ارائهدهندهی دیگری از Firebase (مانند فیسبوک) وجود دارد، وارد یک ارائهدهنده (مانند گوگل) شود، خطای auth/account-exists-with-different-credential به همراه کلاس AuthCredential (توکن شناسهی گوگل) نمایش داده میشود. برای تکمیل فرآیند ورود به ارائهدهندهی مورد نظر، کاربر ابتدا باید وارد ارائهدهندهی موجود (مثلاً فیسبوک) شود و سپس به AuthCredential قبلی (توکن شناسهی گوگل) پیوند دهد.
FirebaseAuth auth = FirebaseAuth.instance;
// Create a credential from a Google Sign-in Request
var googleAuthCredential = GoogleAuthProvider.credential(accessToken: 'xxxx');
try {
// Attempt to sign in the user in with Google
await auth.signInWithCredential(googleAuthCredential);
} on FirebaseAuthException catch (e) {
if (e.code == 'account-exists-with-different-credential') {
// The account already exists with a different credential
String email = e.email;
AuthCredential pendingCredential = e.credential;
// Fetch a list of what sign-in methods exist for the conflicting user
List<String> userSignInMethods = await auth.fetchSignInMethodsForEmail(email);
// If the user has several sign-in methods,
// the first method in the list will be the "recommended" method to use.
if (userSignInMethods.first == 'password') {
// Prompt the user to enter their password
String password = '...';
// Sign the user in to their account with the password
UserCredential userCredential = await auth.signInWithEmailAndPassword(
email: email,
password: password,
);
// Link the pending credential with the existing account
await userCredential.user.linkWithCredential(pendingCredential);
// Success! Go back to your application flow
return goToApplication();
}
// Since other providers are now external, you must now sign the user in with another
// auth provider, such as Facebook.
if (userSignInMethods.first == 'facebook.com') {
// Create a new Facebook credential
String accessToken = await triggerFacebookAuthentication();
var facebookAuthCredential = FacebookAuthProvider.credential(accessToken);
// Sign the user in with the credential
UserCredential userCredential = await auth.signInWithCredential(facebookAuthCredential);
// Link the pending credential with the existing account
await userCredential.user.linkWithCredential(pendingCredential);
// Success! Go back to your application flow
return goToApplication();
}
// Handle other OAuth providers...
}
}