iT邦幫忙

2024 iThome 鐵人賽

DAY 18
0
Software Development

Unity黑科技揭秘:30個專業遊戲開發者必知的開發技巧系列 第 18

Unity Firebase - 使用Firebase進行用戶註冊登入!

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20240830/20119470arfff8XqXb.png

在Unity中先寫好Interface的程式:

public interface IFirebaseAuth
{
    void Initialize();
    void Register(string email, string password, Action<bool, string> registerAction);
    void Login(string email, string password, Action<FirebaseUser, string> userAction);
    FirebaseUser GetUserInfo();
    Task<bool> SendResetPasswordEmail(string email);
    void SignOut();
    Task<bool>  SendVerificationEmail();
    void UpdateUserInformation(UserProfile userProfile, Action<bool> updateAction);
    void DeleteAccount();
    bool CheckIsValid();
    }

然後新增一個Class繼承IFirebaseAuth:

public class FirebaseAuth : IFirebaseAuth
   {
       public FirebaseAuth FireAuth;
       public FirebaseUser CurrentLoginUser;

       public void Initialize()
       {
           FireAuth = FirebaseAuth.DefaultInstance;
       }

       public async void Register(string email, string password, Action<bool, string> registerAction)
       {
           try
           {
               var authTask = await FireAuth.CreateUserWithEmailAndPasswordAsync(email, password);
               var newUser = authTask.User;

               Debug.LogFormat("Firebase Auth: Register successfully: {0}", newUser.Email);
               registerAction?.Invoke(true, "Firebase Auth: Register successfully");
           }
           catch (Exception ex)
           {
               Debug.LogError($"Firebase Auth: Register encountered an error: {ex.Message}");
               registerAction?.Invoke(false, $"Firebase Auth: Register encountered an error: {ex.Message}");
           }
       }

       public async void Login(string email, string password, Action<FirebaseUser,string > userAction)
       {
           try
           {
               var authResult = await FireAuth.SignInWithEmailAndPasswordAsync(email, password);
               Debug.LogFormat("Firebase Auth: Login successfully: {0}", authResult.User.Email);
               CurrentLoginUser = authResult.User;
               userAction?.Invoke(authResult.User, "Firebase Auth: Login successfully");
           }
           catch (Exception ex)
           {
               Debug.LogError($"Firebase Auth: Login encountered an error: {ex.Message}");
               userAction?.Invoke(null, $"Firebase Auth: Login encountered an error: {ex.Message}");
           }
       }

  

       public FirebaseUser GetUserInfo()
       {
           CurrentLoginUser = FireAuth.CurrentUser;
           return CurrentLoginUser;
       }

       public async Task<bool> SendResetPasswordEmail(string email)
       {
           try
           {
               await FireAuth.SendPasswordResetEmailAsync(email);
               Debug.Log("Firebase Auth: Password reset email sent successfully.");
               return true;
           }
           catch (Exception ex)
           {
               Debug.LogError($"Firebase Auth: SendPasswordResetEmailAsync encountered an error: {ex.Message}");
               return false;
           }
       }

       public void SignOut()
       {
           FireAuth.SignOut();
       }

       public async Task<bool> SendVerificationEmail()
       {
           CurrentLoginUser = FireAuth.CurrentUser;
           if (CurrentLoginUser != null)
           {
               try
               {
                   await CurrentLoginUser.SendEmailVerificationAsync();
                   Debug.Log("Email verification sent successfully.");
                   return true;
               }
               catch (Exception ex)
               {
                   Debug.LogError($"SendEmailVerificationAsync encountered an error: {ex.Message}");
                   return false; 
               }
           }

           Debug.LogError("Failed to send email verification: CurrentLoginUser is null.");
           return false;
       }

       public async void UpdateUserInformation(UserProfile userProfile, Action<bool> updateAction)
       {
           if (CurrentLoginUser != null)
           {
               try
               {
                   await CurrentLoginUser.UpdateUserProfileAsync(userProfile);
                   Debug.Log(CurrentLoginUser.DisplayName);
                   Debug.Log("User profile updated successfully.");
                   updateAction?.Invoke(true);
               }
               catch (Exception ex)
               {
                   Debug.LogError($"UpdateUserProfileAsync encountered an error: {ex.Message}");
                   updateAction?.Invoke(false);
               }
           }
           else
           {
               Debug.LogError("No user is currently signed in.");
               updateAction?.Invoke(false);
           }
       }

       public void DeleteAccount()
       {
           CurrentLoginUser.DeleteAsync();
           Debug.Log("User Deleted successfully.");
       }

       public bool CheckIsValid()
       {
           return CurrentLoginUser != null && CurrentLoginUser.IsValid();
       }
   }

使用方法:

public IFirebaseAuth FirebaseAuth = new FirebaseAuth();

然後再需要的地方調用FirebaseAuth的Function即可。


上一篇
Unity Config - 製作加密的遊戲存檔方法!
下一篇
Unity Firestore - 使用 Firestore進行資料儲存!
系列文
Unity黑科技揭秘:30個專業遊戲開發者必知的開發技巧25
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言