Crea un utente
Puoi creare un nuovo utente nel tuo progetto Firebase chiamando il createUserWithEmail:password:completion:
o accedendo a un utente per la prima volta utilizzando un provider di identità federato, come Google Sign-In o Facebook Login .
Puoi anche creare nuovi utenti autenticati con password dalla sezione Autenticazione della console Firebase , nella pagina Utenti.
Ottieni l'utente attualmente connesso
Il modo consigliato per ottenere l'utente corrente è impostare un listener sull'oggetto Auth:
Veloce
handle = Auth.auth().addStateDidChangeListener { auth, user in // ... }
Obiettivo-C
self.handle = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) { // ... }];
Usando un listener, ti assicuri che l'oggetto Auth non sia in uno stato intermedio, come l'inizializzazione, quando ottieni l'utente corrente.
Puoi anche ottenere l'utente attualmente connesso usando la proprietà currentUser
. Se un utente non ha effettuato l'accesso, currentUser
è nullo:
Veloce
if Auth.auth().currentUser != nil { // User is signed in. // ... } else { // No user is signed in. // ... }
Obiettivo-C
if ([FIRAuth auth].currentUser) { // User is signed in. // ... } else { // No user is signed in. // ... }
Ottieni il profilo di un utente
Per ottenere le informazioni sul profilo di un utente, utilizzare le proprietà di un'istanza di FIRUser
. Per esempio:
Veloce
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 += " " } // ... }
Obiettivo-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; // ... }
Ottieni le informazioni sul profilo specifico del provider di un utente
Per ottenere le informazioni sul profilo recuperate dai provider di accesso collegati a un utente, utilizzare la proprietà providerData
. Per esempio:
Veloce
let userInfo = Auth.auth().currentUser?.providerData[indexPath.row] cell?.textLabel?.text = userInfo?.providerID // Provider-specific UID cell?.detailTextLabel?.text = userInfo?.uid
Obiettivo-C
id<FIRUserInfo> userInfo = [FIRAuth auth].currentUser.providerData[indexPath.row]; cell.textLabel.text = [userInfo providerID]; // Provider-specific UID cell.detailTextLabel.text = [userInfo uid];
Aggiorna il profilo di un utente
È possibile aggiornare le informazioni di base del profilo di un utente, il nome visualizzato dell'utente e l'URL della foto del profilo, con la classe FIRUserProfileChangeRequest
. Per esempio:
Veloce
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest() changeRequest?.displayName = displayName changeRequest?.commitChanges { error in // ... }
Obiettivo-C
FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest]; changeRequest.displayName = userInput; [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) { // ... }];
Imposta l'indirizzo email di un utente
Puoi impostare l'indirizzo email di un utente con il updateEmail:completion:
Per esempio:
Veloce
Auth.auth().currentUser?.updateEmail(to: email) { error in // ... }
Obiettivo-C
[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
Invia a un utente un'e-mail di verifica
Puoi inviare un'e-mail di verifica dell'indirizzo a un utente con il metodo sendEmailVerificationWithCompletion:
Per esempio:
Veloce
Auth.auth().currentUser?.sendEmailVerification { error in // ... }
Obiettivo-C
[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) { // ... }];
Puoi personalizzare il modello e-mail utilizzato nella sezione Autenticazione della console Firebase , nella pagina Modelli e-mail. Vedi Modelli e-mail nel Centro assistenza di Firebase.
È anche possibile passare lo stato tramite un URL continua per reindirizzare all'app quando si invia un'e-mail di verifica.
Inoltre, puoi localizzare l'e-mail di verifica aggiornando il codice della lingua sull'istanza Auth prima di inviare l'e-mail. Per esempio:
Veloce
Auth.auth().languageCode = "fr" // To apply the default app language instead of explicitly setting it. // Auth.auth().useAppLanguage()
Obiettivo-C
[FIRAuth auth].languageCode = @"fr"; // To apply the default app language instead of explicitly setting it. // [[FIRAuth auth] useAppLanguage];
Imposta la password di un utente
È possibile impostare la password di un utente con il updatePassword:completion:
Per esempio:
Veloce
Auth.auth().currentUser?.updatePassword(to: password) { error in // ... }
Obiettivo-C
[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) { // ... }];
Invia un'e-mail di reimpostazione della password
Puoi inviare un'e-mail di reimpostazione della password a un utente con il sendPasswordResetWithEmail:completion:
Per esempio:
Veloce
Auth.auth().sendPasswordReset(withEmail: email) { error in // ... }
Obiettivo-C
[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
Puoi personalizzare il modello e-mail utilizzato nella sezione Autenticazione della console Firebase , nella pagina Modelli e-mail. Vedi Modelli e-mail nel Centro assistenza di Firebase.
È anche possibile passare lo stato tramite un URL continua per reindirizzare all'app quando si invia un'e-mail di reimpostazione della password.
Inoltre, puoi localizzare l'e-mail di reimpostazione della password aggiornando il codice della lingua sull'istanza Auth prima di inviare l'e-mail. Per esempio:
Veloce
Auth.auth().languageCode = "fr" // To apply the default app language instead of explicitly setting it. // Auth.auth().useAppLanguage()
Obiettivo-C
[FIRAuth auth].languageCode = @"fr"; // To apply the default app language instead of explicitly setting it. // [[FIRAuth auth] useAppLanguage];
Puoi anche inviare e-mail di reimpostazione della password dalla console Firebase.
Elimina un utente
È possibile eliminare un account utente con il metodo deleteWithCompletion
. Per esempio:
Veloce
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
// An error happened.
} else {
// Account deleted.
}
}
Obiettivo-C
FIRUser *user = [FIRAuth auth].currentUser;
[user deleteWithCompletion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Account deleted.
}
}];
Puoi anche eliminare utenti dalla sezione Autenticazione della console Firebase , nella pagina Utenti.
Riautenticare un utente
Alcune azioni sensibili alla sicurezza, come l' eliminazione di un account , l'impostazione di un indirizzo e-mail principale e la modifica di una password , richiedono che l'utente abbia eseguito l'accesso di recente. Se esegui una di queste azioni e l'utente ha eseguito l'accesso troppo tempo fa, il l'azione non riesce con l'errore FIRAuthErrorCodeCredentialTooOld
. Quando ciò si verifica, autentica nuovamente l'utente ottenendo nuove credenziali di accesso dall'utente e passando le credenziali per reauthenticate
. Per esempio:
Veloce
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.
}
}
Obiettivo-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.
}
}];
Importa account utente
Puoi importare account utente da un file nel tuo progetto Firebase utilizzando il comando auth:import
della CLI Firebase. Per esempio:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14