處理錯誤

Firebase 驗證 SDK 可讓您輕鬆找出使用驗證方法可能發生的各種錯誤。Flutter 適用的 SDK 會透過 FirebaseAuthException 類別顯示這些錯誤。

至少需要提供 codemessage,但在某些情況下,也會提供電子郵件地址和憑證等其他屬性。例如,如果使用者嘗試使用電子郵件地址和密碼登入,系統會明確找出系統擲回的所有錯誤:

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 提供每種方法的最新錯誤詳細資訊。

如果您達到 Firebase 驗證配額,或未啟用特定驗證提供者,可能會擲回 too-many-requestsoperation-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...
  }
}