Tworzenie konta użytkownika
Nowego użytkownika możesz utworzyć w projekcie Firebase, wywołując metodę createUser
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:
Swift
handle = Auth.auth().addStateDidChangeListener { auth, user in // ... }
Objective-C
self.handle = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) { // ... }];
Dzięki detektorowi masz pewność, że po otrzymaniu bieżącego użytkownika obiekt Auth nie znajduje się w stanie pośrednim, np. jako inicjalizacja.
Informacje o aktualnie zalogowanym użytkowniku możesz też poznać, korzystając z właściwości currentUser
. Jeśli użytkownik nie jest zalogowany, currentUser
ma wartość nil:
Swift
if Auth.auth().currentUser != nil { // User is signed in. // ... } else { // No user is signed in. // ... }
Objective-C
if ([FIRAuth auth].currentUser) { // User is signed in. // ... } else { // No user is signed in. // ... }
Pobieranie profilu użytkownika
Aby uzyskać informacje o profilu użytkownika, użyj właściwości instancji FIRUser
. Przykład:
Swift
let user = Auth.auth().currentUser if let user = user { // 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 getTokenWithCompletion:completion: instead. let uid = user.uid let email = user.email let photoURL = user.photoURL var multiFactorString = "MultiFactor: " for info in user.multiFactor.enrolledFactors { multiFactorString += info.displayName ?? "[DispayName]" multiFactorString += " " } // ... }
Objective-C
FIRUser *user = [FIRAuth auth].currentUser; if (user) { // 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 getTokenWithCompletion:completion: instead. NSString *email = user.email; NSString *uid = user.uid; NSMutableString *multiFactorString = [NSMutableString stringWithFormat:@"MultiFactor: "]; for (FIRMultiFactorInfo *info in user.multiFactor.enrolledFactors) { [multiFactorString appendString:info.displayName]; [multiFactorString appendString:@" "]; } NSURL *photoURL = user.photoURL; // ... }
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 właściwości providerData
. Przykład:
Swift
let userInfo = Auth.auth().currentUser?.providerData[indexPath.row] cell?.textLabel?.text = userInfo?.providerID // Provider-specific UID cell?.detailTextLabel?.text = userInfo?.uid
Objective-C
id<FIRUserInfo> userInfo = [FIRAuth auth].currentUser.providerData[indexPath.row]; cell.textLabel.text = [userInfo providerID]; // Provider-specific UID cell.detailTextLabel.text = [userInfo uid];
Aktualizowanie profilu użytkownika
Za pomocą klasy UserProfileChangeRequest
możesz aktualizować podstawowe informacje profilowe użytkownika – wyświetlaną nazwę i adres URL zdjęcia profilowego. Przykład:
Swift
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest() changeRequest?.displayName = displayName changeRequest?.commitChanges { error in // ... }
Objective-C
FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest]; changeRequest.displayName = userInput; [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) { // ... }];
Ustawianie adresu e-mail użytkownika
Adres e-mail użytkownika możesz skonfigurować za pomocą metody updateEmail
.
Przykład:
Swift
Auth.auth().currentUser?.updateEmail(to: email) { error in // ... }
Objective-C
[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
Wysyłanie e-maila weryfikacyjnego do użytkownika
Aby wysłać do użytkownika e-maila na potrzeby weryfikacji adresu, użyj metody sendEmailVerificationWithCompletion:
. Przykład:
Swift
Auth.auth().currentUser?.sendEmailVerification { error in // ... }
Objective-C
[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) { // ... }];
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żna też przekazać stan za pomocą adresu URL kontynuacji, aby przekierowywać użytkowników z powrotem do aplikacji podczas wysyłania e-maila weryfikacyjnego.
Możesz też zlokalizować e-maila weryfikacyjnego, aktualizując kod języka w instancji Auth przed wysłaniem wiadomości. Przykład:
Swift
Auth.auth().languageCode = "fr" // To apply the default app language instead of explicitly setting it. // Auth.auth().useAppLanguage()
Objective-C
[FIRAuth auth].languageCode = @"fr"; // To apply the default app language instead of explicitly setting it. // [[FIRAuth auth] useAppLanguage];
Ustawianie hasła użytkownika
Hasło użytkownika możesz ustawić za pomocą metody updatePassword
. Przykład:
Swift
Auth.auth().currentUser?.updatePassword(to: password) { error in // ... }
Objective-C
[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) { // ... }];
Wyślij e-maila do resetowania hasła
Aby wysłać do użytkownika e-maila do resetowania hasła, użyj metody sendPasswordReset
. Przykład:
Swift
Auth.auth().sendPasswordReset(withEmail: email) { error in // ... }
Objective-C
[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
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ż przekazywać stan za pomocą adresu URL kontynuacji, aby przekierowywać użytkowników z powrotem do aplikacji podczas wysyłania e-maila do resetowania hasła.
Możesz też zlokalizować e-maila do resetowania hasła, aktualizując kod języka w instancji Auth przed wysłaniem wiadomości. Przykład:
Swift
Auth.auth().languageCode = "fr" // To apply the default app language instead of explicitly setting it. // Auth.auth().useAppLanguage()
Objective-C
[FIRAuth auth].languageCode = @"fr"; // To apply the default app language instead of explicitly setting it. // [[FIRAuth auth] useAppLanguage];
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:
Swift
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
// An error happened.
} else {
// Account deleted.
}
}
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
[user deleteWithCompletion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Account deleted.
}
}];
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 zalogował się zbyt dawno temu, czynność ta zakończy się niepowodzeniem i wyświetli się błąd FIRAuthErrorCodeCredentialTooOld
. W takim przypadku musisz ponownie uwierzytelnić użytkownika, uzyskując od niego nowe dane logowania i przekazując je do usługi reauthenticate
. Przykład:
Swift
let user = Auth.auth().currentUser
var credential: AuthCredential
// Prompt the user to re-provide their sign-in credentials
user?.reauthenticate(with: credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
FIRAuthCredential *credential;
// Prompt the user to re-provide their sign-in credentials
[user reauthenticateWithCredential:credential completion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// User re-authenticated.
}
}];
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