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

kullanıcı oluştur

CreateUserWithEmailAndPassword yöntemini çağırarak veya Google Sign-In veya Facebook Login gibi bir birleşik kimlik sağlayıcı kullanarak bir kullanıcıda ilk kez oturum açarak Firebase projenizde yeni bir kullanıcı oluşturursunuz.

Ayrıca, Firebase konsolunun Kullanıcılar sayfasındaki Kimlik Doğrulama bölümünden parola doğrulaması yapılmış yeni kullanıcılar da oluşturabilirsiniz.

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

Geçerli kullanıcıyı almanın önerilen yolu, Auth nesnesine 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, geçerli kullanıcıyı aldığınızda Auth nesnesinin bir ara durumda (başlatma gibi) olmadığından emin olursunuz.

Şu anda oturum açmış olan kullanıcıyı current_user öğesini çağırarak da alabilirsiniz. Bir kullanıcı oturum açmamışsa, kullanıcının is_valid yöntemi false döndürür.

Bir kullanıcının kimlik bilgilerini kalıcı hale getirme

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

Bir kullanıcının profilini alın

Bir kullanıcının profil bilgilerini almak için, bir 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();
}

Bir kullanıcının sağlayıcıya özel profil bilgilerini alın

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

Bir kullanıcının profilini güncelleme

Bir kullanıcının temel profil bilgilerini (kullanıcının görünen adı ve profil fotoğrafı URL'si) 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.
}

Bir kullanıcının e-posta adresini ayarlayın

Bir kullanıcının e-posta adresini UpdateEmail yöntemiyle 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);
}

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

SendEmailVerification yöntemiyle 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ından özelleştirebilirsiniz. Firebase Yardım Merkezi'ndeki E-posta Şablonlarına bakın.

Kullanıcı parolası belirleyin

Bir kullanıcının parolasını UpdatePassword yöntemiyle belirleyebilirsiniz. Ö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);
}

Parola sıfırlama e-postası gönder

SendPasswordResetEmail yöntemiyle bir kullanıcıya parola 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 Kimlik Doğrulama bölümünde kullanılan e-posta şablonunu E-posta Şablonları sayfasından özelleştirebilirsiniz. Firebase Yardım Merkezi'ndeki E-posta Şablonlarına bakın.

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

Bir kullanıcıyı sil

Bir kullanıcı hesabını Delete yöntemiyle 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);
}

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

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

Bir hesabı silmek , birincil e-posta adresini ayarlamak ve parolayı değiştirmek gibi güvenlik açısından hassas bazı işlemler, kullanıcının yakın zamanda oturum açmış olmasını gerektirir. Bu işlemlerden birini gerçekleştirirseniz ve kullanıcı çok uzun zaman önce oturum açmışsa, eylem başarısız olur.

Bu olduğunda, kullanıcıdan yeni oturum açma kimlik bilgilerini alarak ve kimlik bilgilerini Reauthenticate öğesine geçirerek 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 aktar

Firebase CLI'nin 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