गड़बड़ी ठीक करना

Firebase से पुष्टि करने वाले 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);
}

हर तरीका, पुष्टि करने के अनुरोध के टाइप के आधार पर अलग-अलग गड़बड़ी के कोड और मैसेज देता है. Reference API, हर तरीके की गड़बड़ियों के बारे में अप-टू-डेट जानकारी देता है.

अगर Firebase से पुष्टि करने का कोटा पूरा हो जाता है या आपने पुष्टि करने वाली किसी खास कंपनी को चालू नहीं किया है, तो too-many-requests या operation-not-allowed जैसी दूसरी गड़बड़ियां हो सकती हैं.

account-exists-with-different-credential गड़बड़ियां मैनेज की जा रही हैं

अगर आपने Firebase कंसोल में हर ईमेल पते के लिए एक खाता सेटिंग चालू की है, तो जब कोई उपयोगकर्ता सेवा देने वाली किसी कंपनी (जैसे कि Google) में साइन इन करने के लिए किसी ऐसे ईमेल पते का इस्तेमाल करता है जो Firebase का इस्तेमाल करने वाले दूसरे व्यक्ति (जैसे कि Facebook) के लिए पहले से मौजूद है, तो AuthCredential क्लास (Google आईडी टोकन) के साथ गड़बड़ी auth/account-exists-with-different-credential होती है. सेवा देने वाली किसी मौजूदा कंपनी का साइन इन फ़्लो पूरा करने के लिए, उपयोगकर्ता को सबसे पहले मौजूदा कंपनी (जैसे, 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...
  }
}