Firebase Authentication SDK 提供簡單的方式,可擷取使用驗證方法時可能發生的各種錯誤。Flutter 的 SDK 會透過 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 使用者供應商 (例如 Facebook) 的電子郵件地址登入供應商 (例如 Google) 時,系統會擲回 auth/account-exists-with-different-credential 錯誤和 AuthCredential 類別 (Google ID 權杖)。如要完成登入流程並連線至預期供應商,使用者必須先登入現有供應商 (例如 Facebook),然後連結至前者 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...
}
}