處理錯誤

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 使用者供應商的電子郵件地址登入供應商 (例如 Google) 時 (例如 Facebook),系統會擲回 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...
  }
}