-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirebaseRestAPI.cs
More file actions
865 lines (700 loc) · 31.2 KB
/
Copy pathFirebaseRestAPI.cs
File metadata and controls
865 lines (700 loc) · 31.2 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using System.Threading;
using Proyecto26;
using Unity.VisualScripting.FullSerializer;
using UnityEditor;
[Serializable]
public class RestFirebaseUser {
// localId string The uid of the current user.
public string localId;
public string UserId;
// email string The email of the account.
public string email;
// emailVerified boolean Whether or not the account's email has been verified.
public bool emailVerified;
// displayName string The display name for the account.
public string displayName;
// providerUserInfo List of JSON objects List of all linked provider objects which contain "providerId" and "federatedId".
// public Dictionary<string, string> providerUserInfo;
// photoUrl string The photo Url for the account.
public string photoUrl;
// passwordHash string Hash version of password.
public string passwordHash;
// passwordUpdatedAt double The timestamp, in milliseconds, that the account password was last changed.
public double passwordUpdatedAt;
// validSince string The timestamp, in seconds, which marks a boundary, before which Firebase ID token are considered revoked.
public string validSince;
// disabled boolean Whether the account is disabled or not.
public bool disabled;
// lastLoginAt string The timestamp, in milliseconds, that the account last logged in at.
public string lastLoginAt;
// createdAt string The timestamp, in milliseconds, that the account was created at.
public string createdAt;
// customAuth boolean Whether the account is authenticated by the developer.
public bool customAuth;
public string refreshToken;
}
public class FirebaseRestAPI// : MonoBehaviour
{
public static RestFirebaseUser currentUser;
private static fsSerializer serializer = new fsSerializer();
/* Call backs for REST API functions */
public delegate void SignInCallback(RestFirebaseUser user);
public delegate void PostDocCallback(string user);
public delegate void GetDocCallback(JObject user);// where T : new();
public delegate void GetDocIDsCallback(List<string> documents);
private static string authToken = "";
private string userID = "";
private const string authBaseUrl = "https://identitytoolkit.googleapis.com/v1/accounts";
private const string projectId = "YOUR_PROJECT_ID"; // You can find this in your Firebase project settings
private const string baseUrl = "https://firestore.googleapis.com/v1/projects/" + projectId +"/databases/(default)/documents";
private const string dbUrl = "projects/" + projectId + "/databases/(default)/documents";
private const string apiKey = "YOUR_API_KEY;
public string email;
public string password;
private string collectionPath = "YOUR_COLLECTION";
private string documentId = "YOUR_DOCUMENT_ID";
public static JObject json = new JObject();
// using ValueObject = System.Collections.Generic.Dictionary<string, object>;
public struct FirestoreDocument
{
public string name;
public DateTime createTime;
public DateTime updateTime;
//The real content is here, but they are still in "___Value" x object format. Dict key is the field's name. Inner dict key is the ___Value text.
public Dictionary<string, object> fields;
}
// Method to create a stringValue JSON object
private JObject CreateStringValue(string key, string value) {
JObject stringValueJson = new JObject();
JObject json = new JObject();
stringValueJson["stringValue"] = value;
json[key] = stringValueJson;
return json;
}
private JObject ConvertDictionaryValueToJObject(object value) {
JObject jsonObject = new JObject();
// Check if the value is a dictionary
if (value is Dictionary<string, object>) {
Dictionary<string, object> dictionaryValue = (Dictionary<string, object>)value;
// Convert nested dictionary value to JObject recursively
foreach (var pair in dictionaryValue) {
jsonObject[pair.Key] = JToken.FromObject(pair.Value);
}
}
return jsonObject;
}
/* start of code borrowed from other project */
/// <summary>
/// Gets all users from the Firebase Database
/// </summary>
/// <param name="callback"> What to do after all users are downloaded successfully </param>
public void GetDocs(string path, GetDocIDsCallback callback)
{
JObject json = new();
RequestHelper req = new RequestHelper {
Uri = $"{baseUrl}/{path}/",
Method = "GET",
IgnoreHttpException = true, //Prevent to catch http exceptions
ParseResponseBody = true, //Don't encode and parse downloaded data as JSON
ContentType = "application/json",
EnableDebug = false,
DefaultContentType = false,
Headers = new Dictionary<string, string> {
{ "Authorization", $"Bearer {authToken}" },
},
Params = new Dictionary<string, string>
{
{"key", (string)apiKey},// FirebaseConfig.api}
},
};
RestClient.Get(req).Then(response => {
// Waiting(500);
JObject obj = JObject.Parse(response.Text);
JObject json = new();
var docIds = ParseCollectionIds(obj);
callback(docIds);
}).Catch(err => {
EditorUtility.DisplayDialog ("Error", err.Message, "Ok");
});
}
/// <summary>
/// Parses and Returns List of Collections from firebase json data
/// </summary>
/// <param name="json"></param>
/// <returns>
/// List<string>
/// </returns>
private static List<string> ParseCollectionIds(JObject json){
List<string> returnList = new List<string>();
var list = (JArray)json.SelectToken("documents");
foreach(var item in list){
var str = (string)item.SelectToken("name");
var _list = str.Split("/");
returnList.Add(_list.Last());
// Debug.Log($"{_list.Last()}");
// item.SelectToken("name"));
}
return returnList;
}
/* Method to create update field(s) for firebase rest api*/
private static List<string> getUpdateFields(object obj){
Type objType = obj.GetType();
var objField = objType.GetFields();//.GetValue(0);//GetValue(1);
List<string> fields = new();
foreach (var member in objField){
fields.Add(member.Name);
}
return fields;
}
/* Method to create fields for updating firebase using json*/
private static JObject createField(string name, string type, object value){
var obj = new JObject();
JToken _value;
// obj = new JObject();
string _type = "";
if (type == "System.String") {
_type = "stringValue";
_value = (string)value;
} else if(type == "System.Boolean"){
_type = "booleanValue";
_value = (bool)value;
} else if (type == "System.Double"){
_type = "doubleValue";
_value = (double)value;
}
else if (type == "System.Single"){
_type = "doubleValue";
_value = (double)value;
}
else if (type == "System.Int16"){
_type = "integerValue";
_value = (int)value;
}
else if (type == "System.Int32"){
_type = "integerValue";
_value = (int)value;
}
else if (type == "System.Int64"){
_type = "integerValue";
_value = (int)value;
}
else {
_type = "unknownValue";
_value = (string)value;
}
if (value == null) {
_type = "nullValue";
_value = null;
}
obj[_type] = _value;
return obj;
}
/* Method to create json of an object*/
private static JObject getObj(JObject newJson, object i_obj)
{
// JObject newJson = new();
Type objType = i_obj.GetType();
//FieldInfo[]
var objField = objType.GetFields();//.GetValue(0);//GetValue(1);
var count = 0;
foreach (var member in objField)
{
if (member.GetValue(i_obj) is Array){
count++;
// create arrayValue/mapValue
var _json = new JObject();
var _array = new JArray();
_json["mapValue"] = new JObject();
var item = (Array)member.GetValue(i_obj);
foreach(var element in item){
_json["mapValue"]["fields"] = getObj(new JObject(), (object)element);
_array.Add(_json);
}
newJson[member.Name] = new JObject();
newJson[member.Name]["arrayValue"] = new JObject();
newJson[member.Name]["arrayValue"]["values"] = _array;
} else {
newJson[member.Name] = createField(member.Name, member.FieldType.ToString(), member.GetValue(i_obj));
}
}
return newJson;
}
public static void NewWriteDoc<T>(object data, string path, string docId, PostDocCallback callback) where T : new()
{
var myClass = new T();
Type myType = myClass.GetType();
Type t = typeof(T);
MemberInfo[] members = myType.GetMembers(BindingFlags.Public|BindingFlags.Instance);
List<string> classFields = new();// = new();
for (int i =0 ; i < members.Length ; i++)
{
if (members[i].MemberType == MemberTypes.Field){
classFields.Add(members[i].Name);
}
// Display name and type of the member of 'MyClass'.
}
var dbURL = $"projects/{projectId}/databases/(default)/documents/";
var json = new JObject();
var _json = new JObject();
_json = getObj(_json, data);
var fieldList = getUpdateFields(data);
json["fields"] = _json;
string _updateMasks = "";
var count = 0;
var _method = "POST";
var _path = "";
if (docId == ""){
_method = "POST";
_path = $"{path}/?";
} else {
json["name"] = $"{dbURL}{path}/{docId}";
foreach(var item in fieldList){
count++;
if (count == fieldList.Count()){
_updateMasks += $"updateMask.fieldPaths={item}";
} else {
_updateMasks += $"updateMask.fieldPaths={item}&";
}
}
_method = "PATCH";
_path = $"{path}/{docId}?";
}
RequestHelper req = new RequestHelper {
Uri = $"{baseUrl}/{_path}{_updateMasks}",
Method = $"{_method}",
IgnoreHttpException = true, //Prevent to catch http exceptions
ParseResponseBody = true, //Don't encode and parse downloaded data as JSON
ContentType = "application/json",
EnableDebug = true,
DefaultContentType = false,
Headers = new Dictionary<string, string> {
{ "Authorization", $"Bearer {authToken}" },
// { "Accept", "application/json" },
// { "Content-Type", "application/json"}
},
Params = new Dictionary<string, string>
{
{"key", apiKey},// FirebaseConfig.api},
},
BodyString = json.ToString(),
};
if (docId == ""){
RestClient.Post(req).Then(response =>
{
callback(response.ToString());
});
} else {
RestClient.Patch(req).Then(response =>
{
callback(response.ToString());
});
}
}
/// <summary>
/// Retrieves a user from the Firebase Database, given their id
/// </summary>
/// <param name="userId"> Id of the user that we are looking for </param>
/// <param name="callback"> What to do after the user is downloaded successfully </param>
public void NewGetDoc<T>(string path, string doc, GetDocCallback callback) where T : new()
{
// JObject json = new();
var myClass = new T();
Type t = typeof(T);
// Debug.Log(t);
Type myType = myClass.GetType();
MemberInfo[] members = myType.GetMembers(BindingFlags.Public|BindingFlags.Instance);
// Get the public properties.
PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public);
// Debug.LogFormat( "\nThe public instance members of class '{0}' are : \n", myType);
List<string> classFields = new();// = new();
for (int i =0 ; i < members.Length ; i++)
{
if (members[i].MemberType == MemberTypes.Field){
classFields.Add(members[i].Name);
// Debug.LogFormat( "'{0}' is a {1}", members[i].Name, members[i].MemberType);
}
// Display name and type of the member of 'MyClass'.
}
RequestHelper req = new RequestHelper {
Uri = $"{baseUrl}/{path}/{doc}",
Method = "GET",
IgnoreHttpException = true, //Prevent to catch http exceptions
ParseResponseBody = true, //Don't encode and parse downloaded data as JSON
ContentType = "application/json",
EnableDebug = false,
DefaultContentType = false,
Headers = new Dictionary<string, string> {
{ "Authorization", $"Bearer {authToken}" },
},
Params = new Dictionary<string, string>
{
{"key", apiKey},// FirebaseConfig.api}
},
};
// var payload = $"https://firestore.googleapis.com/v1/projects/{projectId}/databases/(default)/documents/users/{userId}?key={apiKey}";
RestClient.Get(req).Then(response => {
// Waiting(500);
JObject obj = JObject.Parse(response.Text);
var jsonData = obj.ToObject<FirestoreDocument>();
var fields = (JObject)obj["fields"];
foreach(var field in classFields){
json = GetJsonValue(fields, field);
}
callback(json);
}).Catch(err => {
EditorUtility.DisplayDialog ("Error", err.Message, "Ok");
});
}
private static JObject GetJsonValue(JObject obj, string key){
// JObject json = new();
var value = obj[key];
string found = "";
var stringValue = value.SelectToken("stringValue");
if (stringValue != null) json.Add(key, String.Format("{0}",stringValue));
if (stringValue == null) {
var doubleValue = value.SelectToken("doubleValue");
if (doubleValue != null) json.Add(key, (double)doubleValue);
if (doubleValue == null) {
var booleanValue = value.SelectToken("booleanValue");
if (booleanValue != null) json.Add(key, (bool)booleanValue);
if (booleanValue == null) {
var intValue = value.SelectToken("integerValue");
if (intValue != null) json.Add(key, (int)intValue);
}
}
}
var arrayValue = value.SelectToken("arrayValue");
if (arrayValue != null){
var aa = new JArray();
var values = arrayValue.SelectToken("values");
var mapValue = values[0]["mapValue"];
var fields = mapValue.Value<JObject>("fields").Properties();
var fieldsDict = fields.ToDictionary(
k => k.Name,
v => v.Value.ToString()
);
JObject dd = new JObject();
foreach (var field in fields) {
var v = field.Value.SelectToken("stringValue");
if (v == null) {
v = (double)field.Value.SelectToken("doubleValue");
if (v == null) {
v = (bool)field.Value.SelectToken("booleanValue");
if (v == null) {
v = (int)field.Value.SelectToken("integerValue");
}
}
}
dd.Add(field.Name, v);
}
aa.Add(dd);
found = "found array";
json[key] =aa;
}
// Debug.Log(json.ToString());
return json;
//String.Format("key = {0} value = {1} {2}",key, value, found);
}
/* end of code from other project */
// Method to convert list to JArray
private JArray ConvertListToJArray(List<object> list) {
JArray jsonArray = new JArray();
foreach (var item in list) {
// Parse different types of items
if (item is string) {
jsonArray.Add((string)item);
} else if (item is int) {
jsonArray.Add((int)item);
} else if (item is bool) {
jsonArray.Add((bool)item);
}
// Add more types as needed (e.g., float)
}
return jsonArray;
}
// Method to parse mapValue fields
private Dictionary<string, object> ParseMapValue(JObject mapValue) {
Dictionary<string, object> fields = new Dictionary<string, object>();
foreach (var field in mapValue) {
string fieldName = field.Key;
JToken fieldValue = field.Value;
// Parse different types of values
if (fieldValue["stringValue"] != null){
// CheckValue((JObject)fieldValue,"stringValue")) {
fields[fieldName] = (string)fieldValue["stringValue"];
} else if (fieldValue["integerValue"] != null) {
fields[fieldName] = (int)fieldValue["integerValue"];
} else if (fieldValue["booleanValue"] != null) {
fields[fieldName] = (bool)fieldValue["booleanValue"];
}
if (fieldValue["arrayValue"] != null) {
// If the value contains arrayValue, parse its values
JArray arrayValue = (JArray)fieldValue["arrayValue"]["values"];
List<object> arrayValues = ParseArrayValue(arrayValue);
JArray array = ConvertListToJArray(arrayValues);
// Print the parsed values
Debug.Log("Key: " + fieldName);
fields[fieldName] = array;
foreach (var arrayItem in arrayValues) {
Debug.Log("Value: " + arrayItem.ToString());
}
}
// Add more types as needed (e.g., floatValue, arrayValue, etc.)
}
return fields;
}
// Method to parse arrayValue values
private List<object> ParseArrayValue(JArray arrayValue) {
List<object> values = new List<object>();
foreach (var item in arrayValue) {
// Parse different types of values
if (item["stringValue"] != null) {
values.Add((string)item["stringValue"]);
} else if (item["integerValue"] != null) {
values.Add((int)item["integerValue"]);
} else if (item["booleanValue"] != null) {
values.Add((bool)item["booleanValue"]);
}
// Add more types as needed (e.g., floatValue)
}
return values;
}
public void ParseFirebaseJSON(JObject jsonObject){
Dictionary<string, object> fields = new Dictionary<string, object>();
Debug.Log(jsonObject.ToString());
// Iterate through the JSON object
foreach (var property in (JObject)jsonObject["fields"]) {
string key = property.Key;
JToken value = property.Value;
if (value["stringValue"] != null) {
fields[key] = (string)value["stringValue"];
} else if (value["integerValue"] != null) {
fields[key] = (int)value["integerValue"];
} else if (value["booleanValue"] != null) {
fields[key] = (bool)value["booleanValue"];
} else if (value["doubleValue"] != null) {
fields[key] = (double)value["doubleValue"];
}
if (value["mapValue"] != null) {
// If the value contains mapValue, parse its fields
JObject mapValue = (JObject)value["mapValue"]["fields"];
Dictionary<string, object> nestedFields = ParseMapValue(mapValue);
fields[key] = nestedFields;
}
if (value["arrayValue"] != null) {
// If the value contains arrayValue, parse its values
JArray arrayValue = (JArray)value["arrayValue"]["values"];
List<object> arrayValues = ParseArrayValue(arrayValue);
// Print the parsed values
Debug.Log("Key: " + key);
foreach (var arrayItem in arrayValues) {
Debug.Log("Value: " + arrayItem.ToString());
}
}
}
Debug.Log(ConvertDictionaryToJson(fields));
}
// Method to convert dictionary to JSON
private string ConvertDictionaryToJson(Dictionary<string, object> dictionary) {
JObject jsonObject = new JObject();
foreach (var pair in dictionary) {
jsonObject[pair.Key] = JToken.FromObject(pair.Value);
}
return jsonObject.ToString();
}
IEnumerator GetDocumentList() //string accessToken)
{
string url = $"{baseUrl}/users?key={apiKey}";
UnityWebRequest request = UnityWebRequest.Get(url);
request.SetRequestHeader("Authorization", $"Bearer {authToken}");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
Debug.Log("Document list retrieved successfully:");
Debug.Log(request.downloadHandler.text);
}
else
{
Debug.LogError("Failed to retrieve document list:");
Debug.LogError(request.error);
Debug.Log(request.downloadHandler.text);
}
}
public IEnumerator GetDocument(string collectionPath, string documentId, Action<string> onSuccess, Action<string> onFailure)
{
string url = $"{baseUrl}/{collectionPath}/{documentId}?key={apiKey}";
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
onSuccess?.Invoke(request.downloadHandler.text);
}
else
{
onFailure?.Invoke(request.error);
}
}
public void NewSignIn(string email, string password, SignInCallback callback, bool signup = false)
{
Dictionary<string, string> payload = new();
string url = "";
int status = 0;
if (email !="" && password !="") {
// Debug.Log("Sign In with Email/Password");
url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword";
// payload = $"{{\"email\":\"{email}\",\"password\":\"{password}\",\"returnSecureToken\":true}}";
payload["email"] = $"{email}";
payload["password"] = $"{password}";
payload["returnSecureToken"] = "true";
} else {
return;
}
payload["key"] = $"{apiKey}";
RequestHelper req = new RequestHelper {
Uri = $"{url}",
Method = "POST",
Timeout = 10,
IgnoreHttpException = true, //Prevent to catch http exceptions
ParseResponseBody = true, //Don't encode and parse downloaded data as JSON
ContentType = "application/json",
EnableDebug = false,
DefaultContentType = false,
Params = payload,
Headers = new Dictionary<string, string> {
{ "Authorization", $"Bearer {authToken}" }
},
BodyString = "" //Serialize object using JsonUtility by default
};
// $"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key={apiKey}"
// RestClient.Post($"{url}?key={apiKey}",
// payload)
RestClient.Post(req).Then(
response =>
{
var responseJson = response.Text;
var _response = JObject.Parse(responseJson);
// Kept in to display error message in custom text object
// where it parses the return error and displays the message.
// if ()
// var children = _response.Children().Children();
// Waiting(500);
// GameObject errorPanel = GameObject.Find("ErrorCanvas");
// ErrorPanelCanvas panel = errorPanel.GetComponent<ErrorPanelCanvas>();
// Dictionary<string, string> messages = new();
// messages["INVALID_PASSWORD"] = "Invalid Password Provided.";
// messages["EMAIL_NOT_FOUND"] = "Invalid Email Address Provided.";
// foreach(var child in children){
// if (child.SelectToken("message") != null){
// // Debug.Log($"{child.SelectToken("message")}");
// // ShowDialog<ErrorPanelCanvas>(errorPanel, $"{messages[(string)child.SelectToken("message")]}");
// panel.message.text = $"{messages[(string)child.SelectToken("message")]}";
// panel.rootCanvas.SetActive(true);
// // RestDBAuthErrorEvent _evt = restDBAuthErrorEvent;
// status = -1;
//
// }
// }
// EditorUtility.DisplayDialog("Response", responseJson, "Ok");
// Debug.Log(responseJson);
// Using the FullSerializer library: https://github.com/jacobdufault/fullserializer
// its also included now with unity visual scripting
// to serialize more complex types (a Dictionary, in this case)
var data = fsJsonParser.Parse(responseJson);
var _data = JObject.Parse(responseJson);
// Debug.Log($"refreshToken = {_data["refreshToken"]}");
object deserialized = null;
serializer.TryDeserialize(data, typeof(Dictionary<string, string>), ref deserialized);
var authResponse = deserialized as Dictionary<string, string>;
//SetAuthTokenID((string)authResponse["idToken"]);
// Debug.Log($"{AuthTokenId}");
// authResponse["idToken"]);
if (signup){
//CreateDocument("users", authResponse["localId"]);
}
// EditorUtility.DisplayDialog("Response", authResponse["mfaPendingCredential"], "Ok");
currentUser.UserId = authResponse["localId"];
currentUser.displayName = (string)_data["displayName"];
currentUser.email = (string)_data["email"];
currentUser.refreshToken = (string)_data["refreshToken"];
authToken = authResponse["idToken"];
// Debug.Log((string)_data["email"]);
callback(currentUser);
});
}
public void SignIn()
{
StartCoroutine(SignInWithEmailPassword(email, password));
// StartCoroutine(GetDocumentList(authToken));
// StartCoroutine(GetDoc());
}
IEnumerator SignInWithEmailPassword(string email, string password)
{
string url = $"{authBaseUrl}:signInWithPassword?key={apiKey}";
Dictionary<string, string> signInData = new Dictionary<string, string>()
{
{ "email", email },
{ "password", password },
{ "returnSecureToken", "true" }
};
string jsonRequestBody = JsonConvert.SerializeObject(signInData);
//JsonUtility.ToJson(signInData);
// Debug.Log(jsonRequestBody);
// UnityWebRequest request = new UnityWebRequest.Post(url,jjsonRequestBody);
using (UnityWebRequest request = new UnityWebRequest(url, "POST"))
{
// UnityWebRequest.SerializeSimpleForm(signInData);
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonRequestBody);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
string responseBody = request.downloadHandler.text;
SignInResponse response = JsonUtility.FromJson<SignInResponse>(responseBody);
Debug.Log("Sign-in successful. User ID: " + response.localId);
userID = response.localId;
Debug.Log("ID token: " + response.idToken);
authToken = response.idToken;
Debug.Log("Refresh token: " + response.refreshToken);
}
else
{
authToken = "";
userID = "";
Debug.LogError("Sign-in failed: " + request.error);
Debug.Log(request.downloadHandler.text);
}
}
}
}
[Serializable]
public class DocumentData
{
public string name;
public string message;
}
[Serializable]
public class SignInResponse
{
public string kind;
public string localId;
public string email;
public string displayName;
public string idToken;
public string refreshToken;
public string expiresIn;
public string registered;
}