ה-SDK של Firebase Authentication מספק דרך פשוטה לזיהוי השגיאות השונות שעלולות להתרחש במהלך השימוש בשיטות אימות. ערכות ה-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);
}
כל שיטה מספקת קודי שגיאה והודעות שונים, בהתאם לסוג של הפעלת האימות. בReference API אפשר למצוא פרטים עדכניים על השגיאות בכל שיטה.
יכול להיות שיוצגו שגיאות אחרות כמו too-many-requests או operation-not-allowed אם הגעתם למכסת השימוש ב-Firebase Authentication או שלא הפעלתם ספק אימות ספציפי.
טיפול בשגיאות account-exists-with-different-credential
אם הפעלתם את ההגדרה 'חשבון אחד לכל כתובת אימייל' במסוף Firebase, כשמשתמש מנסה להיכנס לספק (למשל, Google) עם כתובת אימייל שכבר קיימת אצל ספק אחר של משתמש Firebase (למשל, Facebook), מוצגת השגיאה auth/account-exists-with-different-credential יחד עם מחלקה AuthCredential (אסימון מזהה Google).
כדי להשלים את תהליך הכניסה לספק הרצוי, המשתמש צריך קודם להיכנס לספק הקיים (למשל, פייסבוק) ואז לקשר אותו לטוקן הקודם של AuthCredential (מזהה Google).
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...
}
}