-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthControl.cs
173 lines (139 loc) · 4.81 KB
/
AuthControl.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Auth;
using UnityEngine.UI;
using System;
public class AuthControl : MonoBehaviour
{
// public InputField LoginPass;
public string RegisterEmail;
public string RegisterPass;
public string Name;
public bool isLogin;
public bool isRegister;
private static AuthControl _instance;
public static AuthControl instance { get { return _instance; } }
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(this.gameObject);
}
else
{
_instance = this;
}
}
private void Start()
{
Register();
Login();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
LogOut();
}
DontDestroyOnLoad(gameObject);
}
public void Login()
{
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInWithEmailAndPasswordAsync(RegisterEmail, RegisterPass).ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
// Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
Firebase.FirebaseException e = task.Exception.Flatten().InnerExceptions[0] as Firebase.FirebaseException;
GetErrorMessage((AuthError)e.ErrorCode);
return;
}
if (task.IsCompleted)
{
// DBManager.instance.Createuser();
Debug.Log("Login Successfull Congrats ");
isLogin = true;
// LoadScene.Instance.CheckUser();
//Score.Instance.wr;
// Score.Instance.LoadData();
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
public void Register()
{
if (RegisterEmail.Equals("") && RegisterPass.Equals("") && Name.Equals(""))
{
print("Please Enter Email And Password");
return;
}
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.CreateUserWithEmailAndPasswordAsync(RegisterEmail, RegisterPass).ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Firebase.FirebaseException e = task.Exception.Flatten().InnerExceptions[0] as Firebase.FirebaseException;
GetErrorMessage((AuthError)e.ErrorCode);
return;
}
if (task.IsCompleted)
{
isRegister = true;
Debug.Log("Registration is completed");
Login();
//Score.Instance.SavaData();
// LoadScene.Instance.CheckUser();
// Score.Instance.LoadData();
}
// Firebase user has been created.
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("Firebase user created successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
public void AnonymousLogin()
{
FirebaseAuth.DefaultInstance.SignInAnonymouslyAsync().ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Firebase.FirebaseException e = task.Exception.Flatten().InnerExceptions[0] as Firebase.FirebaseException;
GetErrorMessage((AuthError)e.ErrorCode);
return;
}
if (task.IsCompleted)
{
Debug.Log(" Anonymous Login Success");
}
});
}
private void GetErrorMessage(AuthError errorCode)
{
string msg = "";
msg = errorCode.ToString();
print(msg);
}
public void LogOut()
{
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignOut();
// LoadScene.Instance.LoginPanel.SetActive(true);
}
}