포톤, Auth 관련 강의 영상을 보는 중에 메일 회원가입, 인증하는 방법은 없어서 코드를 조금 수정했다.

 

 

이메일 인증을 받지 않은 상태라면 메일을 보내고, 받았다면 로비씬으로 이동하는 코드다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Firebase;
using Firebase.Auth;
using Firebase.Extensions;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
public class AuthManager : MonoBehaviour
{
    public bool IsFirebaseReady { get; private set; }
    public bool IsSignInOnProgress { get; private set; }
 
    public InputField emailField;
    public InputField passwordField;
    public Button signInButton;
 
    public static FirebaseApp firebaseApp;
    public static FirebaseAuth firebaseAuth;
 
    public static FirebaseUser User;
 
    public void Start()
    {
        signInButton.interactable = false;
 
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                var result = task.Result;
 
                if (result != DependencyStatus.Available)
                {
                    Debug.Log(message: result.ToString());
                    IsFirebaseReady = false;
                }
                else
                {
                    IsFirebaseReady = true;
 
                    firebaseApp = FirebaseApp.DefaultInstance;
                    firebaseAuth = FirebaseAuth.DefaultInstance;
                }
 
                signInButton.interactable = IsFirebaseReady;
            }
        );
    }
 
    public void SignIn()
    {
        if (!IsFirebaseReady || IsSignInOnProgress || User != null)
        {
            return;
        }
 
        IsSignInOnProgress = true;
        signInButton.interactable = false;
 
        firebaseAuth.SignInWithEmailAndPasswordAsync(emailField.text, passwordField.text).ContinueWithOnMainThread(
            (task) =>
                {
                    Debug.Log(message: $"Sign in status : {task.Status}");
 
                    IsSignInOnProgress = false;
                    signInButton.interactable = true;
 
                    if (task.IsFaulted)
                    {
                        Debug.LogError(task.Exception);
                    }
                    else if (task.IsCanceled)
                    {
                        Debug.LogError(message: "Sign-in canceled");
                    }
                    else
                    {
                        User = task.Result;
                        if(User.IsEmailVerified)
                        {
                            Debug.Log(User.Email);
                            SceneManager.LoadScene("Lobby");
                        }
                        else
                        {
                            Firebase.Auth.FirebaseUser user = firebaseAuth.CurrentUser;
                            if (user != null)
                            {
                                user.SendEmailVerificationAsync().ContinueWith(stat => {
                                    if (stat.IsCanceled)
                                    {
                                        Debug.LogError("SendEmailVerificationAsync was canceled.");
                                        return;
                                    }
                                    if (stat.IsFaulted)
                                    {
                                        Debug.LogError("SendEmailVerificationAsync encountered an error: " + stat.Exception);
                                        return;
                                    }
 
                                    Debug.Log("Email sent successfully.");
                                });
                            }
 
                            User = null;
                        }
                    }
                }
            );
    }
 
    public void SignUp()
    {
        firebaseAuth.CreateUserWithEmailAndPasswordAsync(emailField.text, passwordField.text).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);
        });
    }
}
cs

 

참고

레트로님 유튜브

www.youtube.com/watch?v=mP6IscbNxOA&list=PLctzObGsrjfwF7kkoraWb235U8Z602gx1&index=5

 

구글 파이어베이스 공식 문서

firebase.google.com/docs/auth/unity/manage-users?hl=ko

Posted by 꿀풍
,