创建用户
您可以通过调用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