Tworzenie konta użytkownika
Nowego użytkownika możesz utworzyć na 2 sposoby:
W aplikacji: utwórz nowego użytkownika w projekcie Firebase, wywołując metodę
CreateUserWithEmailAndPasswordlub logując użytkownika po raz pierwszy za pomocą dostawcy tożsamości federacyjnej, takiego jak Logowanie przez Google lub Logowanie przez Facebooka.W Firebasekonsoli: utwórz nowego użytkownika uwierzytelnianego hasłem na karcie Zabezpieczenia > Uwierzytelnianie > Użytkownicy.
Pobieranie obecnie zalogowanego użytkownika
Zalecany sposób uzyskania informacji o bieżącym użytkowniku to ustawienie odbiornika w obiekcie Auth:
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);
Użycie detektora gwarantuje, że obiekt Auth nie będzie w stanie pośrednim, np. w trakcie inicjowania, gdy uzyskasz bieżącego użytkownika.
Możesz też uzyskać informacje o obecnie zalogowanym użytkowniku, wywołując funkcję current_user. Jeśli użytkownik nie jest zalogowany, metoda is_valid zwróci wartość false.
Przechowywanie danych logowania użytkownika
Po zalogowaniu się użytkownika jego dane logowania są przechowywane w lokalnym magazynie kluczy. Lokalną pamięć podręczną danych logowania użytkownika można usunąć, wylogowując go. Magazyn kluczy jest specyficzny dla platformy:
- Platformy Apple: Usługi pęku kluczy.
- Android: Android Keystore.
- Windows: Credential Management API.
- OS X: Usługi pęku kluczy.
- Linux: libsecret, które musi być zainstalowane przez użytkownika.
Pobieranie profilu użytkownika
Aby uzyskać informacje o profilu użytkownika, użyj metod dostępu instancji klasy firebase::auth::User. Przykład:
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(); }
Pobieranie informacji o profilu użytkownika specyficznych dla dostawcy
Aby uzyskać informacje o profilu pobrane od dostawców logowania połączonych z użytkownikiem, użyj metody ProviderData. Przykład:
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(); } }
Aktualizowanie profilu użytkownika
Podstawowe informacje o profilu użytkownika, czyli nazwę wyświetlaną i adres URL zdjęcia profilowego, możesz zaktualizować za pomocą metody UpdateUserProfile. Przykład:
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. }
Ustawianie adresu e-mail użytkownika
Adres e-mail użytkownika możesz ustawić za pomocą metody UpdateEmail. Przykład:
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); }
Wysyłanie użytkownikowi e-maila weryfikacyjnego
Możesz wysłać e-mail weryfikacyjny adresu do użytkownika za pomocą metody SendEmailVerification. Przykład:
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); }
Szablon e-maila używany w sekcji Uwierzytelnianie w Firebase konsoli możesz dostosować na stronie Szablony e-maili. Więcej informacji znajdziesz w Centrum pomocy Firebase w artykule Szablony e-maili.
Ustawianie hasła użytkownika
Hasło użytkownika możesz ustawić za pomocą metody UpdatePassword. Przykład:
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); }
Wysyłanie e-maila do zresetowania hasła
Możesz wysłać e-maila z prośbą o zresetowanie hasła do użytkownika za pomocą metody SendPasswordResetEmail. Przykład:
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);
Możesz dostosować szablon e-maila używany na karcie Zabezpieczenia > Uwierzytelnianie > Szablony w konsoli Firebase. Więcej informacji znajdziesz w Centrum pomocy Firebase w artykule Szablony e-maili.
E-maile do zresetowania hasła możesz też wysyłać z konsoli Firebase.
Usuwanie użytkownika
Konto użytkownika możesz usunąć za pomocą metody Delete. Przykład:
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); }
Użytkowników możesz też usuwać w Firebasekonsoli na karcie Bezpieczeństwo > Uwierzytelnianie > Użytkownicy.
Ponowne uwierzytelnianie użytkownika
Niektóre działania związane z bezpieczeństwem, takie jak usuwanie konta, ustawianie podstawowego adresu e-mail i zmiana hasła, wymagają, aby użytkownik niedawno się zalogował. Jeśli wykonasz jedno z tych działań, a użytkownik zalogował się zbyt dawno, działanie zakończy się niepowodzeniem.
W takim przypadku ponownie uwierzytelnij użytkownika, uzyskując od niego nowe dane logowania i przekazując je do funkcji Reauthenticate. Przykład:
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); }
Importowanie kont użytkowników
Możesz zaimportować konta użytkowników z pliku do projektu w Firebase, używając polecenia auth:import wiersza poleceń Firebase. Przykład:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14