管理 Firebase 中的用戶

創建用戶

您可以透過呼叫createUser方法或使用聯合身分識別提供者(例如Google Sign-InFacebook 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為零:

迅速

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];

更新用戶的個人資料

您可以使用UserProfileChangeRequest類別更新使用者的基本個人資料資訊(使用者的顯示名稱和個人資料照片 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方法設定使用者的電子郵件地址。例如:

迅速

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方法設定使用者密碼。例如:

迅速

Auth.auth().currentUser?.updatePassword(to: password) { error in
  // ...
}

Objective-C

[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) {
  // ...
}];

發送密碼重設電子郵件

您可以使用sendPasswordReset方法向使用者發送密碼重設電子郵件。例如:

迅速

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 控制台傳送密碼重設電子郵件。

刪除用戶

您可以使用delete方法刪除使用者帳戶。例如:

迅速

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