Tworzenie konta użytkownika
Nowego użytkownika możesz utworzyć w projekcie Firebase, wywołując metodę CreateUserWithEmailAndPassword
lub logując użytkownika po raz pierwszy przy użyciu dostawcy tożsamości sfederowanego, takiego jak Logowanie przez Google lub Logowanie przez Facebooka.
Nowych użytkowników z uwierzytelnianiem za pomocą hasła możesz też utworzyć w sekcji Uwierzytelnianie w konsoli Firebase na stronie Użytkownicy.
Pobierz informacje o zalogowanym użytkowniku
Aby uzyskać bieżącego użytkownika, najlepiej ustawić detektor 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);
Dzięki detektorowi masz pewność, że po otrzymaniu bieżącego użytkownika obiekt Auth nie znajduje się w stanie pośrednim, np. jako inicjalizacja.
Możesz też skontaktować się z zalogowanym użytkownikiem, dzwoniąc pod numer current_user
. Jeśli użytkownik nie jest zalogowany, jego metoda is_valid
zwróci wartość „false”.
Zachowywanie danych logowania użytkownika
Gdy użytkownik się zaloguje, dane logowania użytkownika zostaną zapisane w lokalnym magazynie kluczy. Lokalną pamięć podręczną danych logowania użytkownika można usunąć przez wylogowanie użytkownika. Magazyn kluczy jest związany z konkretną platformą:
- Platformy Apple: usługi pęku kluczy.
- Android: magazyn kluczy Androida.
- Windows: Credential Management API.
- OS X: usługi pęku kluczy.
- Linux: libsecret, który musi zainstalować użytkownik.
Pobieranie profilu użytkownika
Aby uzyskać informacje o profilu użytkownika, użyj metod akcesorów instancji 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(); }
Uzyskiwanie informacji o profilu użytkownika specyficznych dla dostawcy
Aby pobrać informacje profilowe 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
Za pomocą metody UpdateUserProfile
możesz aktualizować podstawowe informacje profilowe użytkownika – wyświetlaną nazwę i adres URL zdjęcia profilowego. 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 skonfigurować 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 e-maila weryfikacyjnego do użytkownika
Aby wysłać do użytkownika e-maila na potrzeby weryfikacji adresu, użyj 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); }
Na stronie Szablony e-maili możesz dostosować szablon e-maila w sekcji Uwierzytelnianie w konsoli Firebase. Przeczytaj artykuł Szablony e-maili w Centrum pomocy Firebase.
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); }
Wyślij e-maila do resetowania hasła
Za pomocą metody SendPasswordResetEmail
możesz wysłać do użytkownika e-maila z prośbą o zresetowanie hasła. 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);
Na stronie Szablony e-maili możesz dostosować szablon e-maila w sekcji Uwierzytelnianie w konsoli Firebase. Przeczytaj artykuł Szablony e-maili w Centrum pomocy Firebase.
Możesz też wysyłać e-maile dotyczące resetowania hasła z konsoli Firebase.
Usuwanie konta 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 sekcji Uwierzytelnianie w konsoli Firebase na stronie Użytkownicy.
Ponowne uwierzytelnianie użytkownika
Niektóre działania związane z bezpieczeństwem (takie jak usunięcie konta, ustawienie podstawowego adresu e-mail lub zmiana hasła) wymagają ostatniego zalogowania się. Jeśli wykonasz jedną z tych czynności, a użytkownik zaloguje się zbyt dawno temu, nie powiedzie się.
W takim przypadku ponownie uwierzytelnij użytkownika, uzyskując od niego nowe dane logowania i przekazując je do usługi 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
Aby zaimportować konta użytkowników z pliku do projektu Firebase, użyj polecenia auth:import
interfejsu wiersza poleceń Firebase. Przykład:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14