การจัดการข้อผิดพลาด
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
SDK การตรวจสอบสิทธิ์ Firebase มอบวิธีง่ายๆ ในการค้นหาข้อผิดพลาดต่างๆ ที่อาจเกิดขึ้น
วิธีการตรวจสอบสิทธิ์ 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);
}
แต่ละวิธีจะมีรหัสและข้อความแสดงข้อผิดพลาดที่แตกต่างกัน โดยขึ้นอยู่กับประเภทคำขอการตรวจสอบสิทธิ์
API ข้อมูลอ้างอิงให้รายละเอียดล่าสุดเกี่ยวกับข้อผิดพลาดสำหรับแต่ละวิธี
อาจมีข้อผิดพลาดอื่นๆ เช่น too-many-requests
หรือ operation-not-allowed
ถ้าคุณถึงโควต้าการตรวจสอบสิทธิ์ของ Firebase
หรือไม่ได้เปิดใช้ผู้ให้บริการตรวจสอบสิทธิ์รายใดรายหนึ่ง
การจัดการข้อผิดพลาด account-exists-with-different-credential
รายการ
หากคุณเปิดใช้การตั้งค่า 1 บัญชีต่ออีเมลในคอนโซล Firebase
เมื่อผู้ใช้พยายามลงชื่อเข้าใช้ระบบของผู้ให้บริการ (เช่น Google) ด้วยอีเมลที่มีอยู่แล้วสำหรับผู้ให้บริการ Firebase ของผู้ใช้รายอื่น
(เช่น Facebook) ข้อผิดพลาด auth/account-exists-with-different-credential
จะแสดงพร้อมกับคลาส AuthCredential
(โทเค็นรหัส Google)
เพื่อให้ขั้นตอนการลงชื่อเข้าใช้กับผู้ให้บริการที่ต้องการนั้น ผู้ใช้ต้องลงชื่อเข้าใช้ผู้ให้บริการที่มีอยู่ (เช่น Facebook) ก่อน จากนั้นจึงลิงก์กับผู้ให้บริการรายเดิม
AuthCredential
(โทเค็นรหัส 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...
}
}
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 2025-07-25 UTC"],[],[],null,["\u003cbr /\u003e\n\nThe Firebase Authentication SDKs provide a simple way for catching the various errors which may occur which using\nauthentication methods. The SDKs for Flutter expose these errors via the `FirebaseAuthException`\nclass.\n\nAt a minimum, a `code` and `message` are provided, however in some cases additional properties such as an email address\nand credential are also provided. For example, if the user is attempting to sign in with an email and password,\nany errors thrown can be explicitly caught: \n\n try {\n await FirebaseAuth.instance.signInWithEmailAndPassword(\n email: \"barry.allen@example.com\",\n password: \"SuperSecretPassword!\"\n );\n } on FirebaseAuthException catch (e) {\n print('Failed with error code: ${e.code}');\n print(e.message);\n }\n\nEach method provides various error codes and messages depending on the type of authentication invocation type. The\n[Reference API](https://pub.dev/documentation/firebase_auth/latest/) provides up-to-date details on the errors for each method.\n\nOther errors such as `too-many-requests` or `operation-not-allowed` may be thrown if you reach the Firebase Authentication quota,\nor have not enabled a specific auth provider.\n\nHandling `account-exists-with-different-credential` Errors\n\nIf you enabled the One account per email address setting in the [Firebase console](https://console.firebase.google.com/project/_/authentication/providers),\nwhen a user tries to sign in a to a provider (such as Google) with an email that already exists for another Firebase user's provider\n(such as Facebook), the error `auth/account-exists-with-different-credential` is thrown along with an `AuthCredential` class (Google ID token).\nTo complete the sign-in flow to the intended provider, the user has to first sign in to the existing provider (e.g. Facebook) and then link to the former\n`AuthCredential` (Google ID token). \n\n FirebaseAuth auth = FirebaseAuth.instance;\n\n // Create a credential from a Google Sign-in Request\n var googleAuthCredential = GoogleAuthProvider.credential(accessToken: 'xxxx');\n\n try {\n // Attempt to sign in the user in with Google\n await auth.signInWithCredential(googleAuthCredential);\n } on FirebaseAuthException catch (e) {\n if (e.code == 'account-exists-with-different-credential') {\n // The account already exists with a different credential\n String email = e.email;\n AuthCredential pendingCredential = e.credential;\n\n // Fetch a list of what sign-in methods exist for the conflicting user\n List\u003cString\u003e userSignInMethods = await auth.fetchSignInMethodsForEmail(email);\n\n // If the user has several sign-in methods,\n // the first method in the list will be the \"recommended\" method to use.\n if (userSignInMethods.first == 'password') {\n // Prompt the user to enter their password\n String password = '...';\n\n // Sign the user in to their account with the password\n UserCredential userCredential = await auth.signInWithEmailAndPassword(\n email: email,\n password: password,\n );\n\n // Link the pending credential with the existing account\n await userCredential.user.linkWithCredential(pendingCredential);\n\n // Success! Go back to your application flow\n return goToApplication();\n }\n\n // Since other providers are now external, you must now sign the user in with another\n // auth provider, such as Facebook.\n if (userSignInMethods.first == 'facebook.com') {\n // Create a new Facebook credential\n String accessToken = await triggerFacebookAuthentication();\n var facebookAuthCredential = FacebookAuthProvider.credential(accessToken);\n\n // Sign the user in with the credential\n UserCredential userCredential = await auth.signInWithCredential(facebookAuthCredential);\n\n // Link the pending credential with the existing account\n await userCredential.user.linkWithCredential(pendingCredential);\n\n // Success! Go back to your application flow\n return goToApplication();\n }\n\n // Handle other OAuth providers...\n }\n }"]]