新增使用者
如要在 Firebase 專案中建立新使用者,請呼叫
createUser
敬上
方法或透過聯合身分首次登入使用者
例如 Google 登入或
Facebook 登入。
您也可以從「驗證」頁面建立以密碼驗證的新使用者 Firebase 控制台,找到「使用者」頁面。
取得目前登入的使用者
如要取得目前的使用者,建議您設定 驗證物件:
handle = Auth.auth().addStateDidChangeListener { auth, user in
// ...
}
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.
// ...
}
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 += " "
}
// ...
}
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
id<FIRUserInfo> userInfo = [FIRAuth auth].currentUser.providerData[indexPath.row];
cell.textLabel.text = [userInfo providerID];
// Provider-specific UID
cell.detailTextLabel.text = [userInfo uid];
更新使用者的個人資料
您可以更新使用者的基本個人資料,也就是使用者的顯示名稱
和個人資料相片網址—使用 UserProfileChangeRequest
類別。例如:
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = displayName
changeRequest?.commitChanges { error in
// ...
}
FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest];
changeRequest.displayName = userInput;
[changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
// ...
}];
設定使用者的電子郵件地址
您可以使用 updateEmail
方法設定使用者的電子郵件地址。
例如:
Auth.auth().currentUser?.updateEmail(to: email) { error in
// ...
}
[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) {
// ...
}];
向使用者傳送驗證電子郵件
您可以使用
sendEmailVerificationWithCompletion:
方法。例如:
Auth.auth().currentUser?.sendEmailVerification { error in
// ...
}
[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) {
// ...
}];
您可以在 Firebase 控制台,前往「電子郵件範本」頁面。 請參閱「電子郵件範本」: Firebase 說明中心。
您也可以藉由 繼續網址:重新重新導向 傳送驗證電子郵件給應用程式。
此外,您可以更新 驗證碼才能傳送電子郵件。例如:
Auth.auth().languageCode = "fr"
// To apply the default app language instead of explicitly setting it.
// Auth.auth().useAppLanguage()
[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
// ...
}
[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) {
// ...
}];
傳送重設密碼電子郵件
您可以使用
sendPasswordReset
方法。例如:
Auth.auth().sendPasswordReset(withEmail: email) { error in
// ...
}
[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) {
// ...
}];
您可以在 Firebase 控制台,前往「電子郵件範本」頁面。 請參閱「電子郵件範本」: Firebase 說明中心。
您也可以藉由 繼續網址:重新重新導向 傳送密碼重設電子郵件給應用程式。
此外,您可以更新密碼重設電子郵件, 驗證碼才能傳送電子郵件。例如:
Auth.auth().languageCode = "fr"
// To apply the default app language instead of explicitly setting it.
// Auth.auth().useAppLanguage()
[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.
}
}
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.
}
}
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 auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14