Firebase'de Kullanıcıları Yönetin

Kullanıcı oluşturma

Firebase projenizde CreateUserWithEmailAndPassword yöntemini çağırarak veya Google ile Oturum Açma ya da Facebook Girişi gibi bir birleşik kimlik sağlayıcı kullanarak bir kullanıcının ilk kez oturum açmasını sağlayarak yeni bir kullanıcı oluşturursunuz.

Kullanıcılar sayfasındaki Firebase konsolunun Kimlik Doğrulama bölümünden de şifreyle kimliği doğrulanmış yeni kullanıcılar oluşturabilirsiniz.

Oturum açmış durumdaki kullanıcıyı getir

Geçerli kullanıcıyı edinmenin önerilen yolu, Auth nesnesinde bir işleyici ayarlamaktır:

class MyAuthStateListener : public firebase::auth::AuthStateListener {
 public:
  void OnAuthStateChanged(firebase::auth::Auth* auth) override {
    firebase::auth::User user = auth->current_user();
    if (user.is_valid()) {
      // User is signed in
      printf("OnAuthStateChanged: signed_in %s\n", user.uid().c_str());
    } else {
      // User is signed out
      printf("OnAuthStateChanged: signed_out\n");
    }
    // ...
  }
};
// ... initialization code
// Test notification on registration.
MyAuthStateListener state_change_listener;
auth->AddAuthStateListener(&state_change_listener);

Bir işleyici kullanarak, geçerli kullanıcıyı aldığınızda Auth nesnesinin başlatma gibi bir ara durumda kalmamasını sağlarsınız.

Şu anda oturum açmış olan kullanıcıyı current_user numaralı telefonu arayarak da öğrenebilirsiniz. Kullanıcı oturum açmamışsa kullanıcının is_valid yöntemi false (yanlış) değerini döndürür.

Kullanıcının kimlik bilgilerini korumak

Kullanıcının kimlik bilgileri, oturum açtıktan sonra yerel anahtar deposunda depolanır. Kullanıcı kimlik bilgilerinin yerel önbelleği, kullanıcının oturumu kapatılarak silinebilir. Anahtar deposu platforma özgüdür:

Kullanıcının profilini alma

Kullanıcının profil bilgilerini almak için firebase::auth::User örneğindeki erişim yöntemlerini kullanın. Örnek:

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  std::string name = user.display_name();
  std::string email = user.email();
  std::string photo_url = user.photo_url();
  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use firebase::auth::User::Token() instead.
  std::string uid = user.uid();
}

Kullanıcının sağlayıcıya özel profil bilgilerini alma

Bir kullanıcıya bağlı oturum açma sağlayıcılarından alınan profil bilgilerini almak için ProviderData yöntemini kullanın. Örnek:

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  for (auto it = user.provider_data().begin();
       it != user.provider_data().end(); ++it) {
    firebase::auth::UserInfoInterface profile = *it;
    // Id of the provider (ex: google.com)
    std::string providerId = profile.provider_id();

    // UID specific to the provider
    std::string uid = profile.uid();

    // Name, email address, and profile photo Url
    std::string name = profile.display_name();
    std::string email = profile.email();
    std::string photoUrl = profile.photo_url();
  }
}

Kullanıcı profilini güncelleme

UpdateUserProfile yöntemini kullanarak kullanıcının temel profil bilgilerini (kullanıcının görünen adı ve profil fotoğrafı URL'si) güncelleyebilirsiniz. Örnek:

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  firebase::auth::User::UserProfile profile;
  profile.display_name = "Jane Q. User";
  profile.photo_url = "https://example.com/jane-q-user/profile.jpg";
  user.UpdateUserProfile(profile).OnCompletion(
      [](const firebase::Future<void>& completed_future, void* user_data) {
        // We are probably in a different thread right now.
        if (completed_future.error() == 0) {
          printf("User profile updated.");
        }
      },
      nullptr);  // pass user_data here.
}

Kullanıcının e-posta adresini ayarlama

Kullanıcının e-posta adresini UpdateEmail yöntemiyle ayarlayabilirsiniz. Örnek:

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  user.UpdateEmail("user@example.com")
      .OnCompletion(
          [](const firebase::Future<void>& completed_future,
             void* user_data) {
            // We are probably in a different thread right now.
            if (completed_future.error() == 0) {
              printf("User email address updated.");
            }
          },
          nullptr);
}

Bir kullanıcıya doğrulama e-postası gönderme

Kullanıcılara SendEmailVerification yöntemini kullanarak adres doğrulama e-postası gönderebilirsiniz. Örnek:

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  user.SendEmailVerification().OnCompletion(
      [](const firebase::Future<void>& completed_future, void* user_data) {
        // We are probably in a different thread right now.
        if (completed_future.error() == 0) {
          printf("Email sent.");
        }
      },
      nullptr);
}

Firebase konsolunun Kimlik Doğrulama bölümünde kullanılan e-posta şablonunu E-posta Şablonları sayfasından özelleştirebilirsiniz. Firebase Yardım Merkezi'nde E-posta Şablonları'na göz atın.

Kullanıcı şifresi ayarlayın

Kullanıcı şifresini UpdatePassword yöntemiyle ayarlayabilirsiniz. Örnek:

firebase::auth::User user = auth->current_user();
std::string newPassword = "SOME-SECURE-PASSWORD";

if (user.is_valid()) {
  user.UpdatePassword(newPassword.c_str())
      .OnCompletion(
          [](const firebase::Future<void>& completed_future,
             void* user_data) {
            // We are probably in a different thread right now.
            if (completed_future.error() == 0) {
              printf("password updated.");
            }
          },
          nullptr);
}

Şifre sıfırlama e-postası gönderin

SendPasswordResetEmail yöntemini kullanarak bir kullanıcıya şifre sıfırlama e-postası gönderebilirsiniz. Örnek:

std::string emailAddress = "user@example.com";

auth->SendPasswordResetEmail(emailAddress.c_str())
    .OnCompletion(
        [](const firebase::Future<void>& completed_future,
           void* user_data) {
          // We are probably in a different thread right now.
          if (completed_future.error() == 0) {
            // Email sent.
          } else {
            // An error happened.
            printf("Error %d: %s", completed_future.error(),
                   completed_future.error_message());
          }
        },
        nullptr);

Firebase konsolunun Kimlik Doğrulama bölümünde kullanılan e-posta şablonunu E-posta Şablonları sayfasından özelleştirebilirsiniz. Firebase Yardım Merkezi'nde E-posta Şablonları'na göz atın.

Firebase konsolundan şifre sıfırlama e-postaları da gönderebilirsiniz.

Kullanıcı silme

Bir kullanıcı hesabını Delete yöntemini kullanarak silebilirsiniz. Örnek:

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  user.Delete().OnCompletion(
      [](const firebase::Future<void>& completed_future, void* user_data) {
        if (completed_future.error() == 0) {
          // User deleted.
        } else {
          // An error happened.
          printf("Error %d: %s", completed_future.error(),
                 completed_future.error_message());
        }
      },
      nullptr);
}

Ayrıca, Kullanıcılar sayfasındaki Firebase konsolunun Kimlik Doğrulama bölümünden de kullanıcıları silebilirsiniz.

Kullanıcının kimliğini yeniden doğrulama

Hesap silme, birincil e-posta adresi ayarlama ve şifre değiştirme gibi güvenlik açısından hassas işlemler için kullanıcının kısa süre önce oturum açmış olması gerekir. Bu işlemlerden birini gerçekleştirirseniz ve kullanıcı çok uzun süre önce oturum açmışsa işlem başarısız olur.

Bu durumda, kullanıcıdan yeni oturum açma kimlik bilgilerini alıp kimlik bilgilerini Reauthenticate hizmetine ileterek kullanıcının kimliğini yeniden doğrulayın. Örnek:

firebase::auth::User user = auth->current_user();

// Get auth credentials from the user for re-authentication. The example
// below shows email and password credentials but there are multiple
// possible providers, such as GoogleAuthProvider or FacebookAuthProvider.
firebase::auth::Credential credential =
    firebase::auth::EmailAuthProvider::GetCredential("user@example.com",
                                                     "password1234");

if (user.is_valid()) {
  user.Reauthenticate(credential)
      .OnCompletion(
          [](const firebase::Future<void>& completed_future,
             void* user_data) {
            if (completed_future.error() == 0) {
              printf("User re-authenticated.");
            }
          },
          nullptr);
}

Kullanıcı hesaplarını içe aktarma

Firebase CLI'ın auth:import komutunu kullanarak kullanıcı hesaplarını bir dosyadan Firebase projenize aktarabilirsiniz. Örnek:

firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14