Hata İşleme

Firebase Authentication SDK'ları, kimlik doğrulama yöntemleri kullanılırken oluşabilecek çeşitli hataları yakalamak için basit bir yol sağlar. Flutter için SDK'lar bu hataları FirebaseAuthException sınıfı aracılığıyla gösterir.

En azından code ve message sağlanır. Ancak bazı durumlarda e-posta adresi ve kimlik bilgisi gibi ek özellikler de sağlanır. Örneğin, kullanıcı e-posta ve şifreyle oturum açmaya çalışıyorsa oluşturulan hatalar açıkça yakalanabilir:

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

Her yöntem, kimlik doğrulama çağırma türüne bağlı olarak çeşitli hata kodları ve mesajlar sağlar. Referans API, her yöntemle ilgili hatalar hakkında güncel ayrıntılar sağlar.

Firebase Authentication kotasına ulaşırsanız veya belirli bir kimlik doğrulama sağlayıcısını etkinleştirmediyseniz too-many-requests ya da operation-not-allowed gibi başka hatalar da oluşabilir.

account-exists-with-different-credential hatalarını işleme

Firebase konsolunda "E-posta adresi başına bir hesap" ayarını etkinleştirdiyseniz bir kullanıcı, başka bir Firebase kullanıcısının sağlayıcısı (ör. Facebook) için zaten mevcut olan bir e-posta adresiyle bir sağlayıcıda (ör. Google) oturum açmaya çalıştığında auth/account-exists-with-different-credential hatası AuthCredential sınıfıyla (Google kimlik jetonu) birlikte oluşturulur. Kullanıcının, oturum açma akışını amaçlanan sağlayıcıda tamamlaması için önce mevcut sağlayıcıda (ör. Facebook) oturum açması ve ardından eski sağlayıcıya AuthCredential (Google kimlik jetonu) bağlanması gerekir.

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...
  }
}