Obsługa błędów

Pakiety SDK Uwierzytelniania Firebase w prosty sposób pozwalają wychwytywać różne błędy, które mogą wystąpić podczas uwierzytelniania. Pakiety SDK Flutter udostępniają te błędy za pomocą interfejsu FirebaseAuthException zajęcia.

Wymagane są co najmniej właściwości code i message, ale w niektórych przypadkach mogą się pojawić dodatkowe właściwości, takie jak adres e-mail. i dane logowania. Jeśli na przykład użytkownik próbuje zalogować się za pomocą adresu e-mail i hasła, wszystkie zgłoszone błędy można wykryć:

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);
}

Każda metoda dostarcza różne kody błędów i komunikaty w zależności od typu wywołania uwierzytelniania. Interfejs Reference API zawiera aktualne informacje o błędach w poszczególnych metodach.

Jeśli osiągniesz limit uwierzytelniania Firebase, mogą pojawić się inne błędy, takie jak too-many-requests lub operation-not-allowed. lub nie włączyli konkretnego dostawcy uwierzytelniania.

Obsługa account-exists-with-different-credential błędu

Jeśli w konsoli Firebase włączone jest ustawienie Jedno konto na adres e-mail, gdy użytkownik próbuje zalogować się w usłudze dostawcy (np. Google) przy użyciu adresu e-mail, który już istnieje w przypadku dostawcy innego użytkownika Firebase. (np. Facebook), błąd auth/account-exists-with-different-credential jest zgłaszany wraz z klasą AuthCredential (token identyfikatora Google). Aby dokończyć proces logowania do odpowiedniego dostawcy, użytkownik musi najpierw zalogować się w dotychczasowym systemie (np.na Facebooku), a potem połączyć z poprzednim dostawcą. AuthCredential (token identyfikatora 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...
 
}
}