123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
-
- using UnityEngine;
- using Firebase;
- using UnityEngine.Events;
-
- using System.Threading.Tasks;
- using System;
-
-
- public class FirebaseManager : MonoBehaviour
- {
- public Firebase.Auth.FirebaseAuth auth;
- public Firebase.Auth.FirebaseUser user;
- void Start()
- {
- auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
- auth.StateChanged += AuthStateChanged;
- }
- public async Task<string> Register(string email,string password){
- try{
- var RegisterTask = auth.CreateUserWithEmailAndPasswordAsync(email, password);
- await RegisterTask;
- if(RegisterTask.IsCanceled){
- return "2-取消";
- }else if(RegisterTask.IsFaulted){
- Debug.Log(RegisterTask.Exception.InnerException.Message);
- return "1-"+RegisterTask.Exception.InnerException.Message;
- }else if (RegisterTask.IsCompletedSuccessfully){
- Debug.Log("註冊成功");
- string userID = RegisterTask.Result.User.UserId;
- ulong creationDate = RegisterTask.Result.User.Metadata.CreationTimestamp;
- SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString() ,"遊客", password,"0","","1","0");
- return "0-註冊成功";
- }else{
- Debug.Log("註冊失敗,未知錯誤");
- return "1-未知错误";
- }
- }catch(Exception ex){
- Debug.Log("註冊失敗:" + ex.Message);
- return "1-" + ex.Message;
- }
- }
- public async Task<string> 登入(string email,string password){
- try{
- var signInTask = auth.SignInWithEmailAndPasswordAsync(email, password);
- await signInTask;
- if (signInTask.IsFaulted){
- Debug.Log(signInTask.Exception.InnerException.Message);
- return "1-" + signInTask.Exception.InnerException.Message;
- }else if (signInTask.IsCompletedSuccessfully){
-
- Debug.Log("登录成功!");
- Main.Global.登入條件=0;
- return "0-登录成功!";
- }else{
- Debug.Log("登录失败:未知错误");
- return "1-未知错误";
- }
- }catch (Exception ex){
- Debug.Log("登录失败:" + ex.Message);
- return "1-" + ex.Message;
- }
- }
- public async Task<string> Google登入(string googleIdToken,string googleAccessToken,string passowrd){
- try{
- Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(googleIdToken, googleAccessToken);
- var googlesignintask=auth.SignInAndRetrieveDataWithCredentialAsync(credential);
- await googlesignintask;
- if (googlesignintask.IsFaulted) {
- return "1-1."+googlesignintask.Exception.InnerException.Message;
- }else if(googlesignintask.IsCompletedSuccessfully){
- Main.Global.登入條件=1;
- string userID = googlesignintask.Result.User.UserId;
- ulong creationDate = googlesignintask.Result.User.Metadata.CreationTimestamp;
-
- SQL_game_sys.SQL_使用者_查詢暱稱(userID);
- if(SQL_Module.dr.Read()){}else{
- SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString(),"GOOGLE遊客", passowrd,"0","","0","1");
- }
- return "0";
- }else {
- return "1-2.未知错误";
- }
- }catch (Exception ex){
- return "1-3." + ex.Message;
- }
- }
- public async Task<string> 遊客登入(){
- try{
- var signInTask = auth.SignInAnonymouslyAsync();
- await signInTask;
- if (signInTask.IsCompleted && !signInTask.IsCanceled && !signInTask.IsFaulted){
- Debug.Log("遊客登入成功!");
- string userID = signInTask.Result.User.UserId;
- ulong creationDate = signInTask.Result.User.Metadata.CreationTimestamp;
- Debug.Log(userID);
- SQL_game_sys.SQL_使用者_新增(userID,creationDate.ToString() ,"遊客","", "0", "", "0", "0");
- return "0";
- }else{
- Debug.LogError("2-1." + signInTask.Exception);
- return "2-1." + signInTask.Exception;
- }
- }catch (Exception ex){
- Debug.LogError("2-2." + ex.Message);
- return "2-2." + ex.Message;
- }
- }
- public async Task<string> 刪除使用者()
- {
- try
- {
- Firebase.Auth.FirebaseUser currentUser = auth.CurrentUser;
- if (currentUser != null)
- {
- await currentUser.DeleteAsync();
- return "0-使用者資料刪除成功";
- }
- else
- {
- return "1-沒有使用者登入";
- }
- }
- catch (Exception ex)
- {
- return "1-" + ex.Message;
- }
- }
- public async void 重設密碼(string password){
- Firebase.Auth.FirebaseUser user = auth.CurrentUser;
- var usertask = user.UpdatePasswordAsync(password);
- await usertask;
- }
- public async Task<string> 重設EMAIL(string password)
- {
- try
- {
- Firebase.Auth.FirebaseUser user = auth.CurrentUser;
- var usertask = user.UpdateEmailAsync(password);
- await usertask;
- return "邮箱重设成功";
- }
- catch (Exception ex)
- {
- return "重设邮箱时出错:" + ex.Message;
- }
- }
- public void 登出(){
- auth.SignOut();
- }
- void AuthStateChanged(object sender, System.EventArgs eventArgs)
- {
- if (auth.CurrentUser != user)
- {
- user = auth.CurrentUser;
- if (user != null)
- {
- Debug.Log($"登入帳號 - {user.Email}");
- }
- }
- }
- void OnDestroy()
- {
- auth.StateChanged -= AuthStateChanged;
- }
- }
|