फायरबेस प्रमाणीकरण एसडीके प्रमाणीकरण विधियों का उपयोग करके होने वाली विभिन्न त्रुटियों को पकड़ने का एक आसान तरीका प्रदान करता है। फ़्लटर के लिए SDKs 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);
}
प्रत्येक विधि प्रमाणीकरण आमंत्रण प्रकार के आधार पर विभिन्न त्रुटि कोड और संदेश प्रदान करती है। संदर्भ एपीआई प्रत्येक विधि के लिए त्रुटियों पर नवीनतम विवरण प्रदान करता है।
यदि आप फायरबेस प्रमाणीकरण कोटा तक पहुंचते हैं, या किसी विशिष्ट प्रमाणीकरण प्रदाता को सक्षम नहीं किया है, तो अन्य त्रुटियां जैसे too-many-requests
या operation-not-allowed
दी जा सकती हैं।
account-exists-with-different-credential
यदि आपने फ़ायरबेस कंसोल में प्रति ईमेल पता एक खाता सेटिंग सक्षम की है, तो जब कोई उपयोगकर्ता किसी प्रदाता (जैसे कि Google) में किसी अन्य फ़ायरबेस उपयोगकर्ता के प्रदाता (जैसे फेसबुक) के लिए पहले से मौजूद ईमेल के साथ साइन इन करने का प्रयास करता है, तो त्रुटि होती है auth/account-exists-with-different-credential
AuthCredential
वर्ग (Google ID टोकन) के साथ फेंक दिया जाता है। इच्छित प्रदाता के लिए साइन-इन प्रवाह को पूरा करने के लिए, उपयोगकर्ता को पहले मौजूदा प्रदाता (जैसे फेसबुक) में साइन इन करना होगा और फिर पूर्व 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...
}
}