Xử lý lỗi

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

Ít nhất thì 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 đăng nhập cũng được cung cấp. Ví dụ: nếu người dùng đang cố gắng đăng nhập bằng email và mật khẩu, thì mọi lỗi phát sinh đều có thể được nắm bắt một cách 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 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. API Tham chiếu cung cấp thông tin chi tiết mới nhất về các lỗi cho từng phương thức.

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

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

Nếu bạn bật chế độ 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 một email đã tồn tại cho nhà cung cấp của một người dùng Firebase khác (chẳng hạn như Facebook), lỗi auth/account-exists-with-different-credential sẽ phát sinh cùng với lớp AuthCredential (mã thông báo mã nhận dạng Google). Để hoàn tất quy trình đăng nhập vào nhà cung cấp dự định, 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 trước đó (mã thông báo mã nhận dạng 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...
  }
}