Tworzenie konta użytkownika
Nowego użytkownika w projekcie Firebase możesz utworzyć, wywołując metodę
createUser
lub logując użytkownika po raz pierwszy za pomocą dostawcy tożsamości federacyjnej, takiego jak Logowanie przez Google lub
Logowanie przez Facebooka.
Nowych użytkowników uwierzytelnianych hasłem możesz też tworzyć w sekcji Uwierzytelnianie w Firebase konsoli na stronie Użytkownicy.
Pobieranie obecnie zalogowanego użytkownika
Zalecanym sposobem uzyskania informacji o bieżącym użytkowniku jest ustawienie detektora w obiekcie Auth:
Swift
handle = Auth.auth().addStateDidChangeListener { auth, user in // ... }
Objective-C
self.handle = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) { // ... }];
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 za pomocą właściwości currentUser. Jeśli użytkownik nie jest zalogowany, wartość currentUser to null:
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; // ... }
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 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
Możesz zaktualizować podstawowe informacje o profilu użytkownika, czyli nazwę wyświetlaną i adres URL zdjęcia profilowego, za pomocą klasy UserProfileChangeRequest. 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 ustawić 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 użytkownikowi e-maila weryfikacyjnego
Możesz wysłać e-maila weryfikacyjnego na adres użytkownika za pomocą metody sendEmailVerificationWithCompletion:. Przykład:
Swift
Auth.auth().currentUser?.sendEmailVerification { error in // ... }
Objective-C
[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) { // ... }];
Szablon e-maila używany w sekcji Uwierzytelnianie w Firebasekonsoli możesz dostosować na stronie Szablony e-maili. Zobacz Szablony e-maili w Centrum pomocy Firebase.
Możesz też przekazać stan za pomocą URL-a dalszego działania, aby po wysłaniu e-maila weryfikacyjnego przekierować użytkownika z powrotem do aplikacji.
Możesz też dostosować język e-maila weryfikacyjnego, aktualizując kod języka w instancji Auth przed wysłaniem e-maila. 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) { // ... }];
Wysyłanie e-maila do resetowania hasła
Możesz wysłać e-maila z prośbą o zresetowanie hasła do użytkownika za pomocą metody sendPasswordReset. Przykład:
Swift
Auth.auth().sendPasswordReset(withEmail: email) { error in // ... }
Objective-C
[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
Szablon e-maila używany w sekcji Uwierzytelnianie w Firebasekonsoli możesz dostosować na stronie Szablony e-maili. Zobacz Szablony e-maili w Centrum pomocy Firebase.
Możesz też przekazać stan za pomocą URL-a dalszego działania, aby po wysłaniu e-maila do resetowania hasła przekierować użytkownika z powrotem do aplikacji.
Możesz też zlokalizować e-maila z prośbą o zresetowanie hasła, aktualizując kod języka w instancji Auth przed wysłaniem e-maila. 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];
E-maile z prośbą o zresetowanie 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:
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 Firebasekonsoli na stronie 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 i wyświetli się błąd FIRAuthErrorCodeCredentialTooOld. W takim przypadku ponownie uwierzytelnij użytkownika, uzyskując od niego nowe dane logowania i przekazując je do 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
Możesz zaimportować konta użytkowników z pliku do projektu w Firebase za pomocą polecenia auth:import wiersza poleceń Firebase. Przykład:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14