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

Kullanıcı oluşturma

Yeni kullanıcı oluşturmak için aşağıdaki seçeneklerden yararlanabilirsiniz:

Şu anda oturum açmış kullanıcıyı alma

Geçerli kullanıcıyı almanın önerilen yolu, Auth nesnesinde bir dinleyici 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 dinleyici kullanarak, mevcut kullanıcıyı aldığınızda Auth nesnesinin ara durumda (ör. başlatma) olmamasını sağlarsınız.

Ayrıca current_user işlevini çağırarak şu anda oturum açmış kullanıcıyı da alabilirsiniz. Kullanıcı oturum açmamışsa kullanıcının is_valid yöntemi false değerini döndürür.

Kullanıcı kimlik bilgilerini kalıcı hale getirme

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

Kullanıcı profili alma

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

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. Örneğin:

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

Kullanıcının görünen adı ve profil fotoğrafı URL'si gibi temel profil bilgilerini UpdateUserProfile yöntemiyle güncelleyebilirsiniz. Örneğin:

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

UpdateEmail yöntemini kullanarak kullanıcının e-posta adresini ayarlayabilirsiniz. Örneğin:

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

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

SendEmailVerification yöntemini kullanarak bir kullanıcıya adres doğrulama e-postası gönderebilirsiniz. Örneğin:

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ında özelleştirebilirsiniz. Firebase Yardım Merkezi'ndeki E-posta Şablonları başlıklı makaleyi inceleyin.

Kullanıcı şifresi belirleme

UpdatePassword yöntemini kullanarak kullanıcı şifresi ayarlayabilirsiniz. Örneğin:

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önderme

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

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 Güvenlik > Kimlik doğrulama > Şablonlar sekmesinde hangi e-posta şablonunun kullanılacağını özelleştirebilirsiniz. Firebase Yardım Merkezi'ndeki E-posta Şablonları başlıklı makaleyi inceleyin.

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

Kullanıcı silme

Delete yöntemini kullanarak kullanıcı hesabını silebilirsiniz. Örneğin:

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, Firebase konsolunda Güvenlik > Kimlik doğrulama > Kullanıcılar sekmesinden 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üvenlikle ilgili bazı hassas işlemler için kullanıcının yakın zamanda oturum açmış olması gerekir. Bu işlemlerden birini gerçekleştirirseniz ve kullanıcı çok uzun zaman önce oturum açtıysa işlem başarısız olur.

Bu durumda, kullanıcıdan yeni oturum açma kimlik bilgileri alıp bu kimlik bilgilerini Reauthenticate'ya ileterek kullanıcının kimliğini yeniden doğrulayın. Örneğin:

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 KSA'nın auth:import komutunu kullanarak kullanıcı hesaplarını bir dosyadan Firebase projenize aktarabilirsiniz. Örneğin:

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