暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FirebaseManager.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using UnityEngine;
  2. using Firebase;
  3. using UnityEngine.Events;
  4. using System.Threading.Tasks;
  5. using System;
  6. public class FirebaseManager : MonoBehaviour
  7. {
  8. public Firebase.Auth.FirebaseAuth auth;
  9. public Firebase.Auth.FirebaseUser user;
  10. void Start()
  11. {
  12. auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
  13. auth.StateChanged += AuthStateChanged;
  14. }
  15. public async Task<string> Register(string email,string password){
  16. try{
  17. var RegisterTask = auth.CreateUserWithEmailAndPasswordAsync(email, password);
  18. await RegisterTask;
  19. if(RegisterTask.IsCanceled){
  20. return "2-取消";
  21. }else if(RegisterTask.IsFaulted){
  22. Debug.Log(RegisterTask.Exception.InnerException.Message);
  23. return "1-"+RegisterTask.Exception.InnerException.Message;
  24. }else if (RegisterTask.IsCompletedSuccessfully){
  25. Debug.Log("註冊成功");
  26. string userID = RegisterTask.Result.User.UserId;
  27. ulong creationDate = RegisterTask.Result.User.Metadata.CreationTimestamp;
  28. SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString() ,"遊客", password,"0","","1","0");
  29. return "0-註冊成功";
  30. }else{
  31. Debug.Log("註冊失敗,未知錯誤");
  32. return "1-未知错误";
  33. }
  34. }catch(Exception ex){
  35. Debug.Log("註冊失敗:" + ex.Message);
  36. return "1-" + ex.Message;
  37. }
  38. }
  39. public async Task<string> 登入(string email,string password){
  40. try{
  41. var signInTask = auth.SignInWithEmailAndPasswordAsync(email, password);
  42. await signInTask;
  43. if (signInTask.IsFaulted){
  44. Debug.Log(signInTask.Exception.InnerException.Message);
  45. return "1-" + signInTask.Exception.InnerException.Message;
  46. }else if (signInTask.IsCompletedSuccessfully){
  47. Debug.Log("登录成功!");
  48. Main.Global.登入條件=0;
  49. return "0-登录成功!";
  50. }else{
  51. Debug.Log("登录失败:未知错误");
  52. return "1-未知错误";
  53. }
  54. }catch (Exception ex){
  55. Debug.Log("登录失败:" + ex.Message);
  56. return "1-" + ex.Message;
  57. }
  58. }
  59. public async Task<string> Google登入(string googleIdToken,string googleAccessToken,string passowrd){
  60. try{
  61. Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(googleIdToken, googleAccessToken);
  62. var googlesignintask=auth.SignInAndRetrieveDataWithCredentialAsync(credential);
  63. await googlesignintask;
  64. if (googlesignintask.IsFaulted) {
  65. return "1-1."+googlesignintask.Exception.InnerException.Message;
  66. }else if(googlesignintask.IsCompletedSuccessfully){
  67. Main.Global.登入條件=1;
  68. string userID = googlesignintask.Result.User.UserId;
  69. ulong creationDate = googlesignintask.Result.User.Metadata.CreationTimestamp;
  70. SQL_game_sys.SQL_使用者_查詢暱稱(userID);
  71. if(SQL_Module.dr.Read()){}else{
  72. SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString(),"GOOGLE遊客", passowrd,"0","","0","1");
  73. }
  74. return "0";
  75. }else {
  76. return "1-2.未知错误";
  77. }
  78. }catch (Exception ex){
  79. return "1-3." + ex.Message;
  80. }
  81. }
  82. public async Task<string> 遊客登入(){
  83. try{
  84. var signInTask = auth.SignInAnonymouslyAsync();
  85. await signInTask;
  86. if (signInTask.IsCompleted && !signInTask.IsCanceled && !signInTask.IsFaulted){
  87. Debug.Log("遊客登入成功!");
  88. string userID = signInTask.Result.User.UserId;
  89. ulong creationDate = signInTask.Result.User.Metadata.CreationTimestamp;
  90. Debug.Log(userID);
  91. SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString() ,"遊客","", "0", "", "0", "0");
  92. return "0";
  93. }else{
  94. Debug.LogError("2-1." + signInTask.Exception);
  95. return "2-1." + signInTask.Exception;
  96. }
  97. }catch (Exception ex){
  98. Debug.LogError("2-2." + ex.Message);
  99. return "2-2." + ex.Message;
  100. }
  101. }
  102. public async Task<string> 刪除使用者()
  103. {
  104. try
  105. {
  106. Firebase.Auth.FirebaseUser currentUser = auth.CurrentUser;
  107. if (currentUser != null)
  108. {
  109. await currentUser.DeleteAsync();
  110. return "0-使用者資料刪除成功";
  111. }
  112. else
  113. {
  114. return "1-沒有使用者登入";
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. return "1-" + ex.Message;
  120. }
  121. }
  122. public async void 重設密碼(string password){
  123. Firebase.Auth.FirebaseUser user = auth.CurrentUser;
  124. var usertask = user.UpdatePasswordAsync(password);
  125. await usertask;
  126. }
  127. public async Task<string> 重設EMAIL(string password)
  128. {
  129. try
  130. {
  131. Firebase.Auth.FirebaseUser user = auth.CurrentUser;
  132. var usertask = user.UpdateEmailAsync(password);
  133. await usertask;
  134. return "邮箱重设成功";
  135. }
  136. catch (Exception ex)
  137. {
  138. return "重设邮箱时出错:" + ex.Message;
  139. }
  140. }
  141. public void 登出(){
  142. auth.SignOut();
  143. }
  144. void AuthStateChanged(object sender, System.EventArgs eventArgs)
  145. {
  146. if (auth.CurrentUser != user)
  147. {
  148. user = auth.CurrentUser;
  149. if (user != null)
  150. {
  151. Debug.Log($"登入帳號 - {user.Email}");
  152. }
  153. }
  154. }
  155. void OnDestroy()
  156. {
  157. auth.StateChanged -= AuthStateChanged;
  158. }
  159. }