Zarządzaj użytkownikami w Firebase (original) (raw)
Tworzenie konta użytkownika
Tworzysz nowego użytkownika w projekcie Firebase, wywołując metodęCreateUserWithEmailAndPasswordlub przez zalogowanie użytkownika po raz pierwszy przy użyciu tożsamości sfederowanej dostawcy, takiego jak Logowanie przez Google,Logowanie do Facebooka.
Nowych użytkowników z uwierzytelnianiem za pomocą hasła możesz też utworzyć na stronie w konsoli Firebase, na stronie Użytkownicy.
Pobierz informacje o zalogowanym użytkowniku
Zalecanym sposobem na uzyskanie bieżącego użytkownika jest ustawienie detektora Obiekt 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 użyciu detektora masz pewność, że obiekt Auth nie jest w trybie pośrednim (np. inicjalizację) po otrzymaniu bieżącego użytkownika.
Możesz też skontaktować się z zalogowanym użytkownikiem, dzwoniąc pod numer current_user. Jeśli nie jest zalogowany, metoda is_valid użytkownika zwróci wartość „false”.
Zachowywanie danych logowania użytkownika
Dane logowania użytkownika zostaną zapisane w lokalnym magazynie kluczy, gdy użytkownik Użytkownik jest zalogowany. Lokalną pamięć podręczną danych logowania użytkownika można usunąć, podpisując 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 akcesora instancjifirebase::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żytkownika, należy użyć 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
Możesz aktualizować podstawowe informacje o profilu użytkownika – jego wyświetlaną nazwę. i adresu URL zdjęcia profilowego, używając 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& 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& 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
Możesz wysłać e-maila weryfikacyjnego do użytkownika z Metoda SendEmailVerification. Przykład:
firebase::auth::User user = auth->current_user(); if (user.is_valid()) { user.SendEmailVerification().OnCompletion( [](const firebase::Future& completed_future, void* user_data) { // We are probably in a different thread right now. if (completed_future.error() == 0) { printf("Email sent."); } }, nullptr); }
Możesz dostosować szablon e-maila używany w sekcji Uwierzytelnianie w konsoli Firebase, na stronie Szablony e-maili. Zobacz 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& 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
Możesz wysłać e-maila do resetowania hasła do użytkownika, który korzysta z: SendPasswordResetEmail. Przykład:
std::string emailAddress = "user@example.com";
auth->SendPasswordResetEmail(emailAddress.c_str()) .OnCompletion( [](const firebase::Future& 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 w sekcji Uwierzytelnianie w konsoli Firebase, na stronie Szablony e-maili. Zobacz Szablony e-maili w Centrum pomocy Firebase.
Możesz też wysyłać e-maile dotyczące resetowania hasła za pomocą 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& 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ć z sekcji UwierzytelnianieFirebase na stronie Użytkownicy.
Ponowne uwierzytelnianie użytkownika
Niektóre działania związane z bezpieczeństwem, takie jak:usunięciu konta,ustawienie głównego adresu e-mail orazzmianę hasła – użytkownik musi mieć niedawno się zalogowałeś(-aś). Jeśli wykonasz jedną z tych czynności, a użytkownik się zaloguje dawno temu, działanie nie powiedzie się.
W takim przypadku ponownie uwierzytelnij użytkownika, uzyskując nowe dane logowania. od użytkownika i przekazując dane logowania 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& completed_future, void* user_data) { if (completed_future.error() == 0) { printf("User re-authenticated."); } }, nullptr); }
Importowanie kont użytkowników
Konta użytkowników możesz importować z pliku do projektu Firebase za pomocą Polecenie auth:import w interfejsie wiersza poleceń Firebase. Przykład:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14