Xử lý lỗi

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

Tối thiểu, hệ thống sẽ cung cấp codemessage. 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 đăng nhập 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, thì hệ thống có thể phát hiện rõ mọi lỗi được gửi:

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 nhiều mã lỗi và thông báo tuỳ thuộc vào loại lệnh gọi xác thực. Reference API cung cấp thông tin chi tiết mới nhất về lỗi của mỗi phương thức.

Các lỗi khác như too-many-requests hoặc operation-not-allowed có thể được gửi nếu bạn đạt đến hạn mức Xác thực Firebase hoặc chưa bật nhà cung cấp dịch vụ xác thực cụ thể.

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

Nếu bạn bật chế độ cài đặt Một tài khoản cho mỗi địa chỉ email trong bảng điều khiển của Firebase, 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 một email đã tồn tại của nhà cung cấp khác của người dùng Firebase (chẳng hạn như Facebook), lỗi auth/account-exists-with-different-credential sẽ được gửi cùng với một lớp AuthCredential (mã thông báo mã nhận dạng của 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 có (ví dụ: Facebook), sau đó liên kết với AuthCredential cũ (mã thông báo mã nhận dạng của 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...
  }
}