创建用户
您可以通过调用CreateUserWithEmailAndPassword
方法或通过使用联合身份提供商(例如Google Sign-In或Facebook Login )首次登录用户来在 Firebase 项目中创建一个新用户。
您还可以在Firebase 控制台的“用户”页面上的“身份验证”部分创建新的经过密码身份验证的用户。
获取当前登录的用户
获取当前用户的推荐方法是在 Auth 对象上设置监听器:
Firebase.Auth.FirebaseAuth auth; Firebase.Auth.FirebaseUser user; // Handle initialization of the necessary firebase modules: void InitializeFirebase() { Debug.Log("Setting up Firebase Auth"); auth = Firebase.Auth.FirebaseAuth.DefaultInstance; auth.StateChanged += AuthStateChanged; AuthStateChanged(this, null); } // Track state changes of the auth object. void AuthStateChanged(object sender, System.EventArgs eventArgs) { if (auth.CurrentUser != user) { bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null; if (!signedIn && user != null) { Debug.Log("Signed out " + user.UserId); } user = auth.CurrentUser; if (signedIn) { Debug.Log("Signed in " + user.UserId); } } } // Handle removing subscription and reference to the Auth instance. // Automatically called by a Monobehaviour after Destroy is called on it. void OnDestroy() { auth.StateChanged -= AuthStateChanged; auth = null; }
通过使用侦听器,您可以确保 Auth 对象在获取当前用户时不处于中间状态(例如初始化)。
您还可以通过调用CurrentUser
来获取当前登录的用户。如果用户未登录, CurrentUser
将返回 null。
保留用户的凭据
用户登录后,用户的凭据将存储在本地密钥库中。用户凭据的本地缓存可以通过注销用户来删除。密钥库是特定于平台的:
获取用户的个人资料
要获取用户的个人资料信息,请使用Firebase.Auth.FirebaseUser
实例的访问器方法。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { string name = user.DisplayName; string email = user.Email; System.Uri photo_url = user.PhotoUrl; // 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 User.TokenAsync() instead. string uid = user.UserId; }
获取用户的提供商特定的个人资料信息
要获取从链接到用户的登录提供程序检索的配置文件信息,请使用ProviderData
方法。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { foreach (var profile in user.ProviderData) { // Id of the provider (ex: google.com) string providerId = profile.ProviderId; // UID specific to the provider string uid = profile.UserId; // Name, email address, and profile photo Url string name = profile.DisplayName; string email = profile.Email; System.Uri photoUrl = profile.PhotoUrl; } }
更新用户的个人资料
您可以使用UpdateUserProfile
方法更新用户的基本个人资料信息(用户的显示名称和个人资料照片 URL)。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { Firebase.Auth.UserProfile profile = new Firebase.Auth.UserProfile { DisplayName = "Jane Q. User", PhotoUrl = new System.Uri("https://example.com/jane-q-user/profile.jpg"), }; user.UpdateUserProfileAsync(profile).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdateUserProfileAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdateUserProfileAsync encountered an error: " + task.Exception); return; } Debug.Log("User profile updated successfully."); }); }
设置用户的电子邮件地址
您可以使用UpdateEmail
方法设置用户的电子邮件地址。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.UpdateEmailAsync("user@example.com").ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdateEmailAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdateEmailAsync encountered an error: " + task.Exception); return; } Debug.Log("User email updated successfully."); }); }
向用户发送验证电子邮件
您可以使用SendEmailVerification
方法向用户发送地址验证电子邮件。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.SendEmailVerificationAsync().ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SendEmailVerificationAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SendEmailVerificationAsync encountered an error: " + task.Exception); return; } Debug.Log("Email sent successfully."); }); }
您可以在电子邮件模板页面上自定义Firebase 控制台的身份验证部分中使用的电子邮件模板。请参阅 Firebase 帮助中心的电子邮件模板。
设置用户密码
您可以使用UpdatePassword
方法设置用户的密码。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; string newPassword = "SOME-SECURE-PASSWORD"; if (user != null) { user.UpdatePasswordAsync(newPassword).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdatePasswordAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdatePasswordAsync encountered an error: " + task.Exception); return; } Debug.Log("Password updated successfully."); }); }
发送密码重置电子邮件
您可以使用SendPasswordResetEmail
方法向用户发送密码重置电子邮件。例如:
string emailAddress = "user@example.com"; if (user != null) { auth.SendPasswordResetEmailAsync(emailAddress).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SendPasswordResetEmailAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SendPasswordResetEmailAsync encountered an error: " + task.Exception); return; } Debug.Log("Password reset email sent successfully."); }); }
您可以在电子邮件模板页面上自定义Firebase 控制台的身份验证部分中使用的电子邮件模板。请参阅 Firebase 帮助中心的电子邮件模板。
您还可以从 Firebase 控制台发送密码重置电子邮件。
删除用户
您可以使用Delete
方法删除用户帐户。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.DeleteAsync().ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("DeleteAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("DeleteAsync encountered an error: " + task.Exception); return; } Debug.Log("User deleted successfully."); }); }
您还可以在Firebase 控制台的“用户”页面上的“身份验证”部分删除用户。
重新验证用户
某些安全敏感操作(例如删除帐户、设置主电子邮件地址和更改密码)要求用户最近登录。如果您执行这些操作之一,而用户登录时间过长,则动作失败。
发生这种情况时,通过从用户获取新的登录凭据并将凭据传递给Reauthenticate
来重新验证用户。例如:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; // Get auth credentials from the user for re-authentication. The example below shows // email and password credentials but there are multiple possible providers, // such as GoogleAuthProvider or FacebookAuthProvider. Firebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential("user@example.com", "password1234"); if (user != null) { user.ReauthenticateAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("ReauthenticateAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("ReauthenticateAsync encountered an error: " + task.Exception); return; } Debug.Log("User reauthenticated successfully."); }); }
导入用户帐户
您可以使用 Firebase CLI 的auth:import
命令将用户帐户从文件导入您的 Firebase 项目。例如:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14