創建用戶
您可以通過調用createUserWithEmail:password:completion:
方法或通過使用聯合身份提供商(例如Google Sign-In或Facebook Login )首次登錄用戶來在 Firebase 項目中創建新用戶。
您還可以在Firebase 控制台的“用戶”頁面上的“身份驗證”部分創建新的經過密碼身份驗證的用戶。
獲取當前登錄的用戶
獲取當前用戶的推薦方法是在 Auth 對像上設置監聽器:
迅速
handle = Auth.auth().addStateDidChangeListener { auth, user in // ... }
Objective-C
self.handle = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) { // ... }];
通過使用偵聽器,您可以確保 Auth 對像在獲取當前用戶時不處於中間狀態(例如初始化)。
您還可以使用currentUser
屬性獲取當前登錄的用戶。如果用戶未登錄,則currentUser
為 nil:
迅速
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. // ... }
獲取用戶的個人資料
要獲取用戶的個人資料信息,請使用FIRUser
實例的屬性。例如:
迅速
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; // ... }
獲取用戶的提供商特定的個人資料信息
要獲取從鏈接到用戶的登錄提供程序檢索的配置文件信息,請使用providerData
屬性。例如:
迅速
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];
更新用戶的個人資料
您可以使用FIRUserProfileChangeRequest
類更新用戶的基本個人資料信息(用戶的顯示名稱和個人資料照片 URL)。例如:
迅速
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) { // ... }];
設置用戶的電子郵件地址
您可以使用updateEmail:completion:
方法設置用戶的電子郵件地址。例如:
迅速
Auth.auth().currentUser?.updateEmail(to: email) { error in // ... }
Objective-C
[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
向用戶發送驗證電子郵件
您可以使用sendEmailVerificationWithCompletion:
方法向用戶發送地址驗證電子郵件。例如:
迅速
Auth.auth().currentUser?.sendEmailVerification { error in // ... }
Objective-C
[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) { // ... }];
您可以在電子郵件模板頁面上自定義Firebase 控制台的身份驗證部分中使用的電子郵件模板。請參閱 Firebase 幫助中心的電子郵件模板。
發送驗證電子郵件時,也可以通過繼續 URL傳遞狀態以重定向回應用程序。
此外,您可以通過在發送電子郵件之前更新 Auth 實例上的語言代碼來本地化驗證電子郵件。例如:
迅速
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];
設置用戶密碼
您可以使用updatePassword:completion:
方法設置用戶密碼。例如:
迅速
Auth.auth().currentUser?.updatePassword(to: password) { error in // ... }
Objective-C
[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) { // ... }];
發送密碼重置電子郵件
您可以使用sendPasswordResetWithEmail:completion:
方法向用戶發送密碼重置電子郵件。例如:
迅速
Auth.auth().sendPasswordReset(withEmail: email) { error in // ... }
Objective-C
[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
您可以在電子郵件模板頁面上自定義Firebase 控制台的身份驗證部分中使用的電子郵件模板。請參閱 Firebase 幫助中心的電子郵件模板。
發送密碼重置電子郵件時,還可以通過繼續 URL傳遞狀態以重定向回應用程序。
此外,您可以通過在發送電子郵件之前更新 Auth 實例上的語言代碼來本地化密碼重置電子郵件。例如:
迅速
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];
您還可以從 Firebase 控制台發送密碼重置電子郵件。
刪除用戶
您可以使用deleteWithCompletion
方法刪除用戶帳戶。例如:
迅速
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.
}
}];
您還可以在Firebase 控制台的“用戶”頁面上的“身份驗證”部分刪除用戶。
重新驗證用戶
某些安全敏感操作(例如刪除帳戶、設置主電子郵件地址和更改密碼)要求用戶最近登錄。如果您執行這些操作之一,而用戶登錄時間過長,則操作失敗並出現FIRAuthErrorCodeCredentialTooOld
錯誤。發生這種情況時,通過從用戶獲取新的登錄憑據並將憑據傳遞給reauthenticate
來重新驗證用戶。例如:
迅速
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.
}
}];
導入用戶帳戶
您可以使用 Firebase CLI 的auth:import
命令將用戶帳戶從文件導入您的 Firebase 項目。例如:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14