ফায়ারবেস অথেনটিকেশন এসডিকে-গুলো অথেনটিকেশন পদ্ধতি ব্যবহারের সময় ঘটতে পারে এমন বিভিন্ন ত্রুটি ধরার একটি সহজ উপায় প্রদান করে। ফ্লাটারের এসডিকে-গুলো 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 ত্রুটিগুলি পরিচালনা করা
আপনি যদি Firebase কনসোলে ‘One account per email address’ সেটিংটি চালু করে থাকেন, তাহলে যখন কোনো ব্যবহারকারী এমন একটি ইমেল দিয়ে কোনো প্রোভাইডারে (যেমন 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...
}
}