创建用户
要在 Firebase 项目中创建新用户,您可以调用 createUserWithEmail:password:completion:
方法,或让用户利用联合身份提供方(例如 Google 登录服务或 Facebook 登录服务)完成首次登录。
您还可以转到 Firebase 控制台的“身份验证”部分,在“用户”页面中创建以密码验证身份的新用户。
获取当前登录的用户
要获取当前用户,建议在 Auth 对象上设置一个侦听器:
Swift
handle = Auth.auth().addStateDidChangeListener { (auth, user) in // ... }
Objective-C
self.handle = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) { // ... }];
使用侦听器可确保在您获取当前用户时,Auth 对象不会处于中间状态(例如初始化)。
您还可以使用 currentUser
属性来获取当前登录的用户。如果用户尚未登录,则 currentUser
为 nil:
Swift
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
实例的各个属性。例如:
Swift
let user = Auth.auth().currentUserif 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 // ... }
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 *uid = user.uid; NSString *email = user.email; NSURL *photoURL = user.photoURL; // ... }
获取用户在特定提供方处的个人资料信息
要获取从用户已关联的登录服务提供商处检索到的个人资料信息,可使用 providerData
属性。例如:
Swift
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
类来更新用户的个人资料基本信息(即用户的显示名和个人资料照片网址)。例如:
Swift
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:
方法来设置用户的电子邮件地址。例如:
Swift
Auth.auth().currentUser?.updateEmail(to: email) { (error) in // ... }
Objective-C
[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
向用户发送验证邮件
您可以使用 sendEmailVerificationWithCompletion:
方法向用户发送邮箱地址验证邮件。例如:
Swift
Auth.auth().currentUser?.sendEmailVerification { (error) in // ... }
Objective-C
[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) { // ... }];
您可以在 Firebase 控制台的“身份验证”(Authentication) 部分的“模板”页面中自定义使用的电子邮件模板。请参阅 Firebase 帮助中心的电子邮件模板。
在发送验证电子邮件时,还可以通过一个接续网址传递状态以重定向回应用。
此外,在发送验证电子邮件之前,您可以通过更新 Auth 实例中的语言代码来对该电子邮件进行本地化。例如:
Swift
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:
方法来设置用户的密码。例如:
Swift
Auth.auth().currentUser?.updatePassword(to: password) { (error) in // ... }
Objective-C
[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) { // ... }];
发送重设密码电子邮件
您可以使用 sendPasswordResetWithEmail:completion:
方法向用户发送重设密码电子邮件。例如:
Swift
Auth.auth().sendPasswordReset(withEmail: email) { error in // ... }
Objective-C
[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) { // ... }];
您可以在 Firebase 控制台的“身份验证”(Authentication) 部分的“模板”页面中自定义使用的电子邮件模板。请参阅 Firebase 帮助中心内的电子邮件模板。
在发送重设密码电子邮件时,还可以通过一个接续网址传递状态以重定向回应用。
此外,在发送重设密码电子邮件之前,您可以通过更新 Auth 实例中的语言代码来对该电子邮件进行本地化。例如:
Swift
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
方法来删除用户帐号。例如:
Swift
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 控制台的“身份验证”(Authentication) 部分,在“用户”页面中删除用户。
对用户重新进行身份验证
某些涉及安全的敏感操作(例如删除帐号、设置主电子邮件地址和更改密码)只能针对最近登录过的用户执行。如果您执行这类操作,但该用户上次登录时间距离现在已过去太久,则操作将失败,并产生 FIRAuthErrorCodeCredentialTooOld
错误。发生这种情况时,请向用户索取新的登录凭据并将这些凭据传递给 reauthenticate
,以便对该用户重新进行身份验证。例如:
Swift
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