Firebase Authentication 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 প্রতিটি পদ্ধতির জন্য ত্রুটি সম্পর্কে হালনাগাদ বিবরণ প্রদান করে।
যদি আপনি Firebase প্রমাণীকরণ কোটায় পৌঁছান, অথবা নির্দিষ্ট প্রমাণীকরণ প্রদানকারী সক্ষম না করেন, তাহলে অন্যান্য ত্রুটি যেমন too-many-requests বা operation-not-allowed দেখা দিতে পারে।
account-exists-with-different-credential পরিচালনার ত্রুটি
যদি আপনি Firebase কনসোলে " এক অ্যাকাউন্ট প্রতি ইমেল ঠিকানা" সেটিং সক্ষম করে থাকেন, তাহলে যখন একজন ব্যবহারকারী অন্য Firebase ব্যবহারকারীর প্রদানকারীর (যেমন Facebook) জন্য ইতিমধ্যেই বিদ্যমান ইমেল দিয়ে একটি প্রদানকারীতে (যেমন Google) সাইন ইন করার চেষ্টা করেন, তখন 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...
}
}