ত্রুটি পরিচালনা

ফায়ারবেস প্রমাণীকরণ SDKগুলি প্রমাণীকরণ পদ্ধতি ব্যবহার করে ঘটতে পারে এমন বিভিন্ন ত্রুটি ধরার জন্য একটি সহজ উপায় প্রদান করে। Flutter-এর জন্য SDK গুলি 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 এর মতো অন্যান্য ত্রুটিগুলি ফেলে দেওয়া হতে পারে।

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...
  }
}