-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirebaseAuth.cs
More file actions
308 lines (261 loc) · 9.88 KB
/
FirebaseAuth.cs
File metadata and controls
308 lines (261 loc) · 9.88 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public static class FirebaseAuth
{
const string FirebaseAPIkey = "AIzaSyDysXaUC9yev74AxQ9hooZ2abjCv-6fYVE";
const string SignUpURL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=" + FirebaseAPIkey;
const string SignInURL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + FirebaseAPIkey;
const string RefreshURL = "https://securetoken.googleapis.com/v1/token?key=" + FirebaseAPIkey;
const string ConfirmationURL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=" + FirebaseAPIkey;
const string SetAccountURL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=" + FirebaseAPIkey;
public static IEnumerator SignUpWithEmailAndPassword(string email, string password, Action<string, string> onSignUp)
{
var request = new SignUpWithEmailAndPasswordRequest
{
email = email,
password = password,
};
string payload = JsonConvert.SerializeObject(request);
using (var www = PostRequest(SignUpURL, payload))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(www.downloadHandler.text);
Debug.LogErrorFormat("Error: {0} Code: {1}", errorResponse.error.message, errorResponse.error.code);
}
else
{
var signInResponse = JsonConvert.DeserializeObject<SignUpWithEmailAndPasswordResponse>(www.downloadHandler.text);
onSignUp(signInResponse.refreshToken, signInResponse.idToken);
}
}
}
public static IEnumerator SignInWithEmailAndPassword(string email, string password, Action<string, string> onSignIn)
{
var request = new SignInWithEmailAndPasswordRequest
{
email = email,
password = password,
};
string payload = JsonConvert.SerializeObject(request);
using (var www = PostRequest(SignInURL, payload))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(www.downloadHandler.text);
Debug.LogFormat("Error: {0} Code: {1}", errorResponse.error.message, errorResponse.error.code);
}
else
{
var signInResponse = JsonConvert.DeserializeObject<SignInWithEmailAndPasswordResponse>(www.downloadHandler.text);
onSignIn(signInResponse.refreshToken, signInResponse.idToken);
}
}
}
public static IEnumerator SignInWithRefreshToken(string refreshToken, Action<string, string> onSignIn)
{
Dictionary<string, string> payload = new Dictionary<string, string>()
{
{ "grant_type", "refresh_token" },
{ "refresh_token", refreshToken },
};
using (var www = UnityWebRequest.Post(RefreshURL, payload))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(www.downloadHandler.text);
Debug.LogFormat("Error: {0} Code: {1}", errorResponse.error.message, errorResponse.error.code);
}
else
{
var signInResponse = JsonConvert.DeserializeObject<SignInWithRefreshTokenResponse>(www.downloadHandler.text);
onSignIn(signInResponse.refresh_token, signInResponse.id_token);
}
}
}
public static IEnumerator SignInAnonymously(Action<string, string> onSignIn)
{
var payload = JsonConvert.SerializeObject(new SignInAnonymouslyRequest());
using (var www = PostRequest(SignUpURL, payload))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
var errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(www.downloadHandler.text);
Debug.LogFormat("Error: {0} Code: {1}", errorResponse.error.message, errorResponse.error.code);
}
else
{
var signInResponse = JsonConvert.DeserializeObject<SignInAnonymouslyResponse>(www.downloadHandler.text);
onSignIn(signInResponse.refreshToken, signInResponse.idToken);
}
}
}
public static IEnumerator SendEmailVerification(string idToken, Action<string> OnEmailVerificationSent)
{
var request = new SendEmailVerificationRequest
{
idToken = idToken,
};
var payload = JsonConvert.SerializeObject(request);
using (var www = PostRequest(ConfirmationURL, payload))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
var response = JsonConvert.DeserializeObject<ErrorResponse>(www.downloadHandler.text);
Debug.LogErrorFormat("Error: {0} Code: {1}", response.error.message, response.error.code);
}
else
{
var response = JsonConvert.DeserializeObject<SendEmailVerificationResponse>(www.downloadHandler.text);
OnEmailVerificationSent(response.email);
}
}
}
public static IEnumerator ConfirmEmailVerification(string verificationCode, Action<bool, string> OnEmailVerificationConfirmed)
{
var request = new ConfirmEmailVerificationRequest
{
oobCode = verificationCode,
};
var payload = JsonConvert.SerializeObject(request);
using (var www = PostRequest(ConfirmationURL, payload))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
var response = JsonConvert.DeserializeObject<ErrorResponse>(www.downloadHandler.text);
Debug.LogErrorFormat("Error: {0} Code: {1}", response.error.message, response.error.code);
}
else
{
var response = JsonConvert.DeserializeObject<ConfirmEmailVerificationResponse>(www.downloadHandler.text);
OnEmailVerificationConfirmed(response.emailVerified, response.email);
}
}
}
static UnityWebRequest PostRequest(string URL, string payload)
{
var www = new UnityWebRequest
{
url = URL,
method = UnityWebRequest.kHttpVerbPOST
};
byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(payload);
www.uploadHandler = new UploadHandlerRaw(bodyRaw)
{
contentType = "application/json"
};
www.downloadHandler = new DownloadHandlerBuffer();
return www;
}
}
#pragma warning disable IDE1006 // Naming Styles
class Error2
{
public string domain { get; set; }
public string reason { get; set; }
public string message { get; set; }
}
class Error
{
public List<Error2> errors { get; set; }
public int code { get; set; }
public string message { get; set; }
}
class ErrorResponse
{
public Error error { get; set; }
}
class SignInWithEmailAndPasswordRequest
{
public string email { get; set; }
public string password { get; set; }
bool returnSecureToken = true;
}
class SignInWithEmailAndPasswordResponse
{
public string kind { get; set; }
public string localId { get; set; }
public string email { get; set; }
public string displayName { get; set; }
public string idToken { get; set; }
public bool registered { get; set; }
public string refreshToken { get; set; }
public string expiresIn { get; set; }
}
class SignInWithRefreshTokenResponse
{
public string expires_in { get; set; }
public string token_type { get; set; }
public string refresh_token { get; set; }
public string id_token { get; set; }
public string user_id { get; set; }
public string project_id { get; set; }
}
class SignUpWithEmailAndPasswordRequest
{
public string email { get; set; }
public string password { get; set; }
public bool returnSecureToken { get; set; }
}
class SignUpWithEmailAndPasswordResponse
{
public string kind { get; set; }
public string idToken { get; set; }
public string email { get; set; }
public string refreshToken { get; set; }
public string expiresIn { get; set; }
public string localId { get; set; }
}
class SignInAnonymouslyRequest
{
bool returnSecureToken = true;
}
class SignInAnonymouslyResponse
{
public string kind { get; set; }
public string idToken { get; set; }
public string email { get; set; }
public string refreshToken { get; set; }
public string expiresIn { get; set; }
public string localId { get; set; }
}
class SendEmailVerificationRequest
{
string requestType = "VERIFY_EMAIL";
public string idToken { get; set; }
}
class SendEmailVerificationResponse
{
public string kind { get; set; }
public string email { get; set; }
}
class ConfirmEmailVerificationRequest
{
public string oobCode { get; set; }
}
class ProviderUserInfo
{
public string providerId { get; set; }
public string federatedId { get; set; }
}
class ConfirmEmailVerificationResponse
{
public string kind { get; set; }
public string email { get; set; }
public string displayName { get; set; }
public string photoUrl { get; set; }
public string passwordHash { get; set; }
public List<ProviderUserInfo> providerUserInfo { get; set; }
public bool emailVerified { get; set; }
}
#pragma warning restore IDE1006 // Naming Styles