創建用戶
您可以透過呼叫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。如果用戶登出,用戶的IsValid()
將傳回 false。
保留使用者的憑證
使用者登入後,使用者的憑證將儲存在本機金鑰庫中。可以透過登出使用者來刪除使用者憑證的本機快取。密鑰庫是特定於平台的:
- Apple 平台:鑰匙圈服務。
- Android: Android 金鑰庫。
- Windows:憑證管理 API 。
- OS X:鑰匙圈服務。
- Linux: libsecret ,使用者必須已安裝。
取得用戶的個人資料
若要取得使用者的個人資料信息,請使用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