Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FirebaseManager.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. double num;
  29. SQL_game_sys.SQL_使用者_查詢編號();
  30. if(SQL_Module.dr.Read()){num = double.Parse(SQL_Module.dr[0].ToString());}else{num =0;}
  31. num += 1;
  32. SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString() ,"遊客",num.ToString().PadLeft(9,'0'), password,"0","","0");
  33. return "0-註冊成功";
  34. }else{
  35. Debug.Log("註冊失敗,未知錯誤");
  36. return "1-未知错误";
  37. }
  38. }catch(Exception ex){
  39. Debug.Log("註冊失敗:" + ex.Message);
  40. return "1-" + ex.Message;
  41. }
  42. }
  43. public async Task<string> 登入(string email,string password){
  44. try{
  45. var signInTask = auth.SignInWithEmailAndPasswordAsync(email, password);
  46. await signInTask;
  47. if (signInTask.IsFaulted){
  48. Debug.Log(signInTask.Exception.InnerException.Message);
  49. return "1-" + signInTask.Exception.InnerException.Message;
  50. }else if (signInTask.IsCompletedSuccessfully){
  51. Debug.Log("登录成功!");
  52. Main.Global.登入條件=0;
  53. return "0-登录成功!";
  54. }else{
  55. Debug.Log("登录失败:未知错误");
  56. return "1-未知错误";
  57. }
  58. }catch (Exception ex){
  59. Debug.Log("登录失败:" + ex.Message);
  60. return "1-" + ex.Message;
  61. }
  62. }
  63. /*public async Task<string> Google登入(string googleIdToken,string googleAccessToken){
  64. try{
  65. Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(googleIdToken, googleAccessToken);
  66. var googlesignintask=auth.SignInAndRetrieveDataWithCredentialAsync(credential);
  67. await googlesignintask;
  68. if (googlesignintask.IsFaulted) {
  69. return "1-1."+googlesignintask.Exception.InnerException.Message;
  70. }else if(googlesignintask.IsCompletedSuccessfully){
  71. Main.Global.登入條件=1;
  72. string userID = googlesignintask.Result.User.UserId;
  73. ulong creationDate = googlesignintask.Result.User.Metadata.CreationTimestamp;
  74. SQL_game_sys.SQL_使用者_查詢暱稱(userID);
  75. if(SQL_Module.dr.Read()){}else{
  76. double num;SQL_game_sys.SQL_使用者_查詢編號();
  77. if(SQL_Module.dr.Read()){num = double.Parse(SQL_Module.dr[0].ToString());}else{num =0;}
  78. num += 1;
  79. SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString(),"GOOGLE遊客",num.ToString().PadLeft(9,'0'));
  80. }
  81. return "0";
  82. }else {
  83. return "1-2.未知错误";
  84. }
  85. }catch (Exception ex){
  86. return "1-3." + ex.Message;
  87. }
  88. }*/
  89. public async Task<string> AppleSignIn(string appletoken,string row_nonce,string password){
  90. Firebase.Auth.Credential credential = Firebase.Auth.OAuthProvider.GetCredential("apple.com",appletoken,row_nonce);
  91. var applesignintask = auth.SignInWithCredentialAsync(credential);
  92. await applesignintask;
  93. if(applesignintask.IsCanceled){
  94. return "3-1.Cabceld";
  95. }else if(applesignintask.IsFaulted){
  96. return "3-2."+applesignintask.Exception.InnerException.Message;
  97. }else if(applesignintask.IsCompletedSuccessfully){
  98. string userID = applesignintask.Result.UserId;
  99. ulong creationDate = applesignintask.Result.Metadata.CreationTimestamp;
  100. string email = applesignintask.Result.Email;
  101. SQL_game_sys.SQL_使用者_查詢暱稱(userID);
  102. if(SQL_Module.dr.Read()){}else{
  103. Main.Global.使用者ID=password;
  104. double num;SQL_game_sys.SQL_使用者_查詢編號();
  105. if(SQL_Module.dr.Read()){num = double.Parse(SQL_Module.dr[0].ToString());}else{num =0;}
  106. num += 1;
  107. SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString(),"APPLE遊客",num.ToString().PadLeft(9,'0'), password,"1",email,"1");
  108. }
  109. return "0";
  110. }else{
  111. return "3-3.未知錯誤";
  112. }
  113. }
  114. public async Task<string> 遊客登入(){
  115. try{
  116. var signInTask = auth.SignInAnonymouslyAsync();
  117. await signInTask;
  118. if (signInTask.IsCompleted && !signInTask.IsCanceled && !signInTask.IsFaulted){
  119. Debug.Log("遊客登入成功!");
  120. string userID = signInTask.Result.User.UserId;
  121. ulong creationDate = signInTask.Result.User.Metadata.CreationTimestamp;
  122. double num;SQL_game_sys.SQL_使用者_查詢編號();
  123. if(SQL_Module.dr.Read()){num = double.Parse(SQL_Module.dr[0].ToString());}else{num =0;}
  124. num += 1;
  125. SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString() ,"匿名遊客",num.ToString().PadLeft(9,'0'),"","0","","0");
  126. return "0";
  127. }else{
  128. Debug.LogError("2-1." + signInTask.Exception);
  129. return "2-1." + signInTask.Exception;
  130. }
  131. }catch (Exception ex){
  132. Debug.LogError("2-2." + ex.Message);
  133. return "2-2." + ex.Message;
  134. }
  135. }
  136. public void DeleteUserInfo(){
  137. Firebase.Auth.FirebaseUser user = auth.CurrentUser;
  138. user?.DeleteAsync().ContinueWith(task => {
  139. if (task.IsCanceled) {
  140. Debug.LogError("DeleteAsync was canceled.");
  141. return;
  142. }
  143. if (task.IsFaulted) {
  144. Debug.LogError("DeleteAsync encountered an error: " + task.Exception);
  145. return;
  146. }
  147. Debug.Log("User deleted successfully.");
  148. });
  149. }
  150. public void 登出(){
  151. auth.SignOut();
  152. }
  153. void AuthStateChanged(object sender,System.EventArgs eventArgs){
  154. if(auth.CurrentUser != user){
  155. user = auth.CurrentUser;
  156. if(user != null){
  157. Debug.Log($"登入帳號 - {user.Email}");
  158. }
  159. }
  160. }
  161. void OnDestroy() {
  162. auth.StateChanged -= AuthStateChanged;
  163. }
  164. }