處理錯誤

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);
}

每個方法根據身份驗證呼叫類型的類型提供各種錯誤代碼和訊息。參考 API提供了有關每種方法的錯誤的最新詳細資訊。

如果您達到 Firebase 身份驗證配額,或​​尚未啟用特定身份驗證提供程序,則可能會引發其他錯誤,例如too-many-requestsoperation-not-allowed

處理account-exists-with-different-credential錯誤

如果您在Firebase 控制台中啟用了「每個電子郵件地址一個帳戶」設置,則當用戶嘗試使用另一Firebase 用戶的提供者(例如Facebook)已存在的電子郵件登入提供者(例如Google)時,會出現以下錯誤auth/account-exists-with-different-credentialAuthCredential類別(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...
  }
}