Firebase Authentication SDK มอบวิธีง่ายๆ ในการตรวจจับข้อผิดพลาดต่างๆ ที่อาจเกิดขึ้นโดยใช้วิธีการรับรองความถูกต้อง SDK สำหรับ Flutter เปิดเผยข้อผิดพลาดเหล่านี้ผ่านคลาส FirebaseAuthException
อย่างน้อยที่สุด จะมีการระบุ code
และ message
อย่างไรก็ตาม ในบางกรณี ยังมีการจัดเตรียมคุณสมบัติเพิ่มเติม เช่น ที่อยู่อีเมลและข้อมูลรับรองด้วย ตัวอย่างเช่น หากผู้ใช้พยายามลงชื่อเข้าใช้ด้วยอีเมลและรหัสผ่าน ข้อผิดพลาดใดๆ ที่เกิดขึ้นจะถูกตรวจจับได้อย่างชัดเจน:
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);
}
แต่ละวิธีจะมีรหัสข้อผิดพลาดและข้อความต่างๆ ขึ้นอยู่กับประเภทของประเภทการเรียกใช้การตรวจสอบสิทธิ์ Reference API ให้รายละเอียดล่าสุดเกี่ยวกับข้อผิดพลาดของแต่ละวิธี
ข้อผิดพลาดอื่นๆ เช่น too-many-requests
หรือ operation-not-allowed
อาจเกิดขึ้นได้หากคุณใช้โควต้าการตรวจสอบสิทธิ์ Firebase ถึงที่กำหนด หรือไม่ได้เปิดใช้งานผู้ให้บริการรับรองความถูกต้องรายใดรายหนึ่ง
การจัดการข้อผิดพลาด account-exists-with-different-credential
หากคุณเปิดใช้งานการตั้งค่าหนึ่งบัญชีต่อที่อยู่อีเมลใน คอนโซล Firebase เมื่อผู้ใช้พยายามลงชื่อเข้าใช้ผู้ให้บริการ (เช่น Google) ด้วยอีเมลที่มีอยู่แล้วสำหรับผู้ให้บริการผู้ใช้ Firebase รายอื่น (เช่น Facebook) ข้อผิดพลาด auth/account-exists-with-different-credential
ถูกโยนพร้อมกับคลาส AuthCredential
(โทเค็น 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...
}
}