Xử lý lỗi

SDK xác thực Firebase cung cấp một cách đơn giản để phát hiện các lỗi khác nhau có thể xảy ra khi sử dụng phương thức xác thực. SDK dành cho Flutter phát hiện những lỗi này thông qua lớp FirebaseAuthException .

Ở mức tối thiểu, một codemessage sẽ được cung cấp, tuy nhiên trong một số trường hợp, các thuộc tính bổ sung như địa chỉ email và thông tin xác thực cũng được cung cấp. Ví dụ: nếu người dùng đang cố đăng nhập bằng email và mật khẩu, mọi lỗi được đưa ra đều có thể được phát hiện rõ ràng:

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

Mỗi phương thức cung cấp các mã lỗi và thông báo khác nhau tùy thuộc vào loại lệnh gọi xác thực. API tham chiếu cung cấp thông tin chi tiết cập nhật về lỗi của từng phương pháp.

Các lỗi khác như too-many-requests hoặc operation-not-allowed có thể xảy ra nếu bạn đạt đến hạn ngạch Xác thực Firebase hoặc chưa bật nhà cung cấp xác thực cụ thể.

Xử lý lỗi account-exists-with-different-credential

Nếu bạn đã bật cài đặt Một tài khoản cho mỗi địa chỉ email trong bảng điều khiển Firebase , thì khi người dùng cố gắng đăng nhập vào một nhà cung cấp (chẳng hạn như Google) bằng email đã tồn tại cho nhà cung cấp của người dùng Firebase khác (chẳng hạn như Facebook), thì sẽ xảy ra lỗi auth/account-exists-with-different-credential được đưa vào cùng với lớp AuthCredential (mã thông báo ID Google). Để hoàn tất quy trình đăng nhập vào nhà cung cấp mong muốn, trước tiên người dùng phải đăng nhập vào nhà cung cấp hiện tại (ví dụ: Facebook) rồi liên kết với AuthCredential cũ (mã thông báo ID Google).

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...
  }
}