การจัดการข้อผิดพลาด

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

หากคุณเปิดใช้การตั้งค่าบัญชีเดียวต่อที่อยู่อีเมลใน 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...
  }
}