Les SDK d'authentification Firebase fournissent un moyen simple de détecter les différentes erreurs pouvant survenir à l'aide de méthodes d'authentification. Les SDK pour Flutter exposent ces erreurs via la classe FirebaseAuthException
.
Au minimum, un code
et message
sont fournis, mais dans certains cas, des propriétés supplémentaires telles qu'une adresse e-mail et des informations d'identification sont également fournies. Par exemple, si l'utilisateur tente de se connecter avec un e-mail et un mot de passe, toutes les erreurs générées peuvent être explicitement détectées :
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);
}
Chaque méthode fournit divers codes d'erreur et messages en fonction du type d'appel d'authentification. L' API de référence fournit des détails à jour sur les erreurs pour chaque méthode.
D'autres erreurs telles qu'un too-many-requests
ou operation-not-allowed
peuvent être générées si vous atteignez le quota d'authentification Firebase ou si vous n'avez pas activé un fournisseur d'authentification spécifique.
Gestion des erreurs account-exists-with-different-credential
Si vous avez activé le paramètre Un compte par adresse e-mail dans la console Firebase , lorsqu'un utilisateur tente de se connecter à un fournisseur (tel que Google) avec une adresse e-mail qui existe déjà pour le fournisseur d'un autre utilisateur Firebase (tel que Facebook), l'erreur auth/account-exists-with-different-credential
est lancé avec une classe AuthCredential
(jeton d'identification Google). Pour terminer le processus de connexion au fournisseur prévu, l'utilisateur doit d'abord se connecter au fournisseur existant (par exemple Facebook), puis se connecter à l'ancien AuthCredential
(jeton d'identification 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...
}
}