Kullanıcı oluştur
CreateUserWithEmailAndPassword
yöntemini çağırarak veya Google Sign-In veya Facebook Login gibi birleştirilmiş kimlik sağlayıcıyı kullanarak bir kullanıcıda ilk kez oturum açarak Firebase projenizde yeni bir kullanıcı oluşturursunuz.
Firebase konsolunun Kullanıcılar sayfasındaki Kimlik Doğrulama bölümünden de şifreyle kimliği doğrulanmış yeni kullanıcılar oluşturabilirsiniz.
Şu anda oturum açmış olan kullanıcıyı alın
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 başlatma gibi bir ara durumda 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çmadıysa kullanıcının is_valid
yöntemi false değerini döndürür.
Kullanıcının kimlik bilgilerini kalıcı hale getirin
Kullanıcının kimlik bilgileri, kullanıcı oturum açtıktan sonra yerel anahtar deposunda saklanacaktır. Kullanıcı kimlik bilgilerinin yerel önbelleği, kullanıcının oturumu kapatılarak silinebilir. Anahtar deposu platforma özeldir:
- Apple platformları: Anahtar Zinciri Hizmetleri .
- Android: Android Anahtar Deposu .
- Windows: Kimlik Bilgisi Yönetimi API'si .
- OS X: Anahtarlık Hizmetleri .
- Linux: Kullanıcının kurmuş olması gereken libsecret .
Bir kullanıcının profilini alın
Bir 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(); } }
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. }
Kullanıcının e-posta adresini ayarlayın
UpdateEmail
yöntemiyle bir 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); }
Bir kullanıcıya doğrulama e-postası gönderin
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ı'na bakın.
Bir kullanıcının şifresini ayarlayın
UpdatePassword
yöntemiyle bir kullanıcının şifresini 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önder
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 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ı'na bakın.
Firebase konsolundan da şifre sıfırlama e-postaları gönderebilirsiniz.
Kullanıcıyı silme
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); }
Ayrıca Firebase konsolunun Kullanıcılar sayfasındaki Kimlik Doğrulama bölümünden de kullanıcıları silebilirsiniz.
Bir kullanıcının kimliğini yeniden doğrulama
Hesabı silmek , birincil e-posta adresi ayarlamak ve şifreyi 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 durumda, kullanıcıdan yeni oturum açma kimlik bilgilerini alıp kimlik bilgilerini Reauthenticate
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 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