Các SDK Xác thực Firebase cung cấp một cách đơn giản để nắm bắt nhiều lỗi có thể xảy ra khi sử dụng các phương thức xác thực. Các SDK cho Flutter hiển thị những lỗi này thông qua lớp FirebaseAuthException.
Ít nhất, bạn phải cung cấp code và message, tuy nhiên, trong một số trường hợp, bạn cũng phải cung cấp các thuộc tính bổ sung như địa chỉ email và thông tin đăng nhậ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 phát hiện 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 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ề 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ể xảy ra 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 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 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), thì 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 nhận dạng Google).
Để hoàn tất quy trình đăng nhập vào nhà cung cấp dự kiế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 nhà cung cấp trước đó AuthCredential (mã thông báo 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...
}
}