خطأ أثناء المعالجة

توفر حزم تطوير البرامج (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);
}

توفر كل طريقة رموز خطأ ورسائل متنوعة بناءً على نوع استدعاء المصادقة. توفّر 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). لإكمال عملية تسجيل الدخول إلى موفِّر الخدمة المقصود، على المستخدم أولاً تسجيل الدخول إلى مزوّد الخدمة الحالي (مثل 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...
  }
}