ערכות האימות של Firebase מספקות דרך פשוטה לתפוס את השגיאות השונות שעלולות להתרחש באמצעות שיטות אימות. ערכות ה-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, או אם לא הפעלת ספק אימות ספציפי.
טיפול account-exists-with-different-credential
אם הפעלת את ההגדרה חשבון אחד לכל כתובת אימייל במסוף Firebase , כאשר משתמש מנסה להיכנס לספק (כגון Google) עם אימייל שכבר קיים עבור ספק אחר של משתמש Firebase (כגון Facebook), השגיאה auth/account-exists-with-different-credential
נזרק יחד עם מחלקה AuthCredential
(אסימון Google ID). כדי להשלים את זרימת הכניסה לספק המיועד, על המשתמש להיכנס תחילה לספק הקיים (למשל פייסבוק) ולאחר מכן לקשר ל- AuthCredential
לשעבר (אסימון Google ID).
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...
}
}