Crea un utente
Puoi creare un nuovo utente nel tuo progetto Firebase chiamando il metodo createUser
o effettuando l'accesso di un utente per la prima volta utilizzando un provider di identità federato, ad esempio 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:
Rapido
handle = Auth.auth().addStateDidChangeListener { auth, user in // ... }
Obiettivo-C
self.handle = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) { // ... }];
Utilizzando un listener, ti assicuri che l'oggetto Auth non si trovi in uno stato intermedio, come l'inizializzazione, quando ottieni l'utente corrente.
Puoi anche ottenere l'utente attualmente connesso utilizzando la proprietà currentUser
. Se un utente non ha effettuato l'accesso, currentUser
è nil:
Rapido
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:
Rapido
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 informazioni sul profilo specifico del provider di un utente
Per ottenere le informazioni sul profilo recuperate dai provider di accesso collegati a un utente, usa la proprietà providerData
. Per esempio:
Rapido
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
Puoi aggiornare le informazioni di base del profilo di un utente, ovvero il nome visualizzato e l'URL della foto del profilo dell'utente, con la classe UserProfileChangeRequest
. Per esempio:
Rapido
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 metodo updateEmail
. Per esempio:
Rapido
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
È possibile inviare un'e-mail di verifica dell'indirizzo a un utente con il metodo sendEmailVerificationWithCompletion:
Per esempio:
Rapido
Auth.auth().currentUser?.sendEmailVerification { error in // ... }
Obiettivo-C
[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) { // ... }];
Puoi personalizzare il modello di email utilizzato nella sezione Autenticazione della console Firebase , nella pagina Modelli email. Consulta Modelli email nel Centro assistenza Firebase.
È anche possibile passare lo stato tramite un URL continuo 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:
Rapido
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 metodo updatePassword
. Per esempio:
Rapido
Auth.auth().currentUser?.updatePassword(to: password) { error in // ... }
Obiettivo-C
[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) { // ... }];
Invia un'e-mail per reimpostare la password
È possibile inviare un messaggio di posta elettronica per la reimpostazione della password a un utente con il metodo sendPasswordReset
. Per esempio:
Rapido
Auth.auth().sendPasswordReset(withEmail: email) { error in // ... }
Obiettivo-C
[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
Puoi personalizzare il modello di email utilizzato nella sezione Autenticazione della console Firebase , nella pagina Modelli email. Consulta Modelli email nel Centro assistenza Firebase.
È anche possibile passare lo stato tramite un URL continuo 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:
Rapido
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 delete
. Per esempio:
Rapido
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 gli 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 email principale e la modifica di una password , richiedono che l'utente abbia effettuato 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ò accade, autentica nuovamente l'utente ottenendo nuove credenziali di accesso dall'utente e passando le credenziali a reauthenticate
. Per esempio:
Rapido
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 gli account utente da un file nel tuo progetto Firebase utilizzando il comando auth:import
dell'interfaccia a riga di comando di Firebase. Per esempio:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14