創建用戶
您可以通過調用createUser
方法或使用聯合身份提供商(例如Google Sign-In或Facebook Login )首次登錄用戶來在 Firebase 項目中創建新用戶。
您還可以在用戶頁面上的Firebase 控制台的身份驗證部分創建新的密碼驗證用戶。
獲取當前登錄的用戶
獲取當前用戶的推薦方法是在 Auth 對像上設置偵聽器:
迅速
handle = Auth.auth().addStateDidChangeListener { auth, user in // ... }
目標-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. // ... }
目標-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 += " " } // ... }
目標-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
目標-C
id<FIRUserInfo> userInfo = [FIRAuth auth].currentUser.providerData[indexPath.row]; cell.textLabel.text = [userInfo providerID]; // Provider-specific UID cell.detailTextLabel.text = [userInfo uid];
更新用戶的個人資料
您可以使用UserProfileChangeRequest
類更新用戶的基本個人資料信息(用戶的顯示名稱和個人資料照片 URL)。例如:
迅速
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest() changeRequest?.displayName = displayName changeRequest?.commitChanges { error in // ... }
目標-C
FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest]; changeRequest.displayName = userInput; [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) { // ... }];
設置用戶的電子郵件地址
您可以使用updateEmail
方法設置用戶的電子郵件地址。例如:
迅速
Auth.auth().currentUser?.updateEmail(to: email) { error in // ... }
目標-C
[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
向用戶發送驗證郵件
您可以使用sendEmailVerificationWithCompletion:
方法向用戶發送地址驗證電子郵件。例如:
迅速
Auth.auth().currentUser?.sendEmailVerification { error in // ... }
目標-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()
目標-C
[FIRAuth auth].languageCode = @"fr"; // To apply the default app language instead of explicitly setting it. // [[FIRAuth auth] useAppLanguage];
設置用戶密碼
您可以使用updatePassword
方法設置用戶密碼。例如:
迅速
Auth.auth().currentUser?.updatePassword(to: password) { error in // ... }
目標-C
[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) { // ... }];
發送密碼重置郵件
您可以使用sendPasswordReset
方法向用戶發送密碼重置電子郵件。例如:
迅速
Auth.auth().sendPasswordReset(withEmail: email) { error in // ... }
目標-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()
目標-C
[FIRAuth auth].languageCode = @"fr"; // To apply the default app language instead of explicitly setting it. // [[FIRAuth auth] useAppLanguage];
您還可以從 Firebase 控制台發送密碼重置電子郵件。
刪除用戶
您可以使用delete
方法刪除用戶帳戶。例如:
迅速
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
// An error happened.
} else {
// Account deleted.
}
}
目標-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.
}
}
目標-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