您可以使用Firebase身份驗證來讓您的用戶使用其電子郵件地址和密碼向Firebase進行身份驗證,並管理您應用程序基於密碼的帳戶。
在你開始之前
在使用Firebase身份驗證之前,您需要:
註冊您的Unity項目並將其配置為使用Firebase。
如果您的Unity項目已經使用Firebase,則說明它已經為Firebase註冊並配置了。
如果您沒有Unity項目,則可以下載示例應用程序。
將Firebase Unity SDK (特別是
FirebaseAuth.unitypackage
)添加到您的Unity項目中。
請注意,將Firebase添加到Unity項目中會涉及Firebase控制台和打開的Unity項目中的任務(例如,您從控制台下載Firebase配置文件,然後將它們移到Unity項目中)。
訪問Firebase.Auth.FirebaseAuth
類
FirebaseAuth
類是所有API調用的網關。可通過FirebaseAuth.DefaultInstance訪問它。 Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
創建一個基於密碼的帳戶
要使用密碼創建新的用戶帳戶,請在您應用的登錄代碼中完成以下步驟:
- 當新用戶使用您的應用程序的註冊表單進行註冊時,請完成您的應用程序所需的所有新帳戶驗證步驟,例如,驗證新帳戶的密碼是否正確鍵入並滿足您的複雜性要求。
- 通過將新用戶的電子郵件地址和密碼傳遞給
FirebaseAuth.CreateUserWithEmailAndPassword
來創建新帳戶:auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception); return; } // Firebase user has been created. Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("Firebase user created successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); });
使用電子郵件地址和密碼登錄用戶
使用密碼登錄用戶的步驟與創建新帳戶的步驟相似。在應用程序的登錄功能中,執行以下操作:
- 用戶登錄到您的應用時,請將用戶的電子郵件地址和密碼傳遞給
FirebaseAuth.SignInWithEmailAndPassword
:auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SignInWithEmailAndPasswordAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); });
- 您也可以像其他工作流程一樣創建證書並登錄:
Firebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential(email, password); auth.SignInWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SignInWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); });
下一步
用戶首次登錄後,將創建一個新的用戶帳戶並將其鏈接到該用戶登錄的憑據(即用戶名和密碼,電話號碼或身份驗證提供者信息)。這個新帳戶存儲為Firebase項目的一部分,可用於在項目中的每個應用程序中識別用戶,而無論用戶如何登錄。
在您的應用中,您可以從
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; }
在Firebase實時數據庫和雲存儲安全規則中,您可以從
auth
變量獲取登錄用戶的唯一用戶ID,並使用它來控制用戶可以訪問哪些數據。
通過將身份驗證提供程序憑據鏈接到現有用戶帳戶,可以允許用戶使用多個身份驗證提供程序登錄您的應用程序。
要註銷用戶,請調用SignOut()
:
auth.SignOut();