Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9KB

  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. namespace CLre_server.API.Utility
  7. {
  8. public static class CardLifeUserAuthentication
  9. {
  10. [Serializable]
  11. private struct AuthPayload
  12. {
  13. public string EmailAddress;
  14. public string Password;
  15. }
  16. private const string LOGIN_URL = "https://live-auth.cardlifegame.com/api/auth/authenticate";
  17. public delegate void OnResponse(AuthenticationResponse data);
  18. public static IEnumerator Authenticate(string email, string password, OnResponse then)
  19. {
  20. UnityWebRequest req = new UnityWebRequest(LOGIN_URL, "POST");
  21. AuthPayload payload = new AuthPayload
  22. {
  23. EmailAddress = email,
  24. Password = password,
  25. };
  26. byte[] bytes = Encoding.UTF8.GetBytes(JsonUtility.ToJson(payload));
  27. req.uploadHandler = new UploadHandlerRaw(bytes);
  28. req.downloadHandler = new DownloadHandlerBuffer();
  29. req.SetRequestHeader("Content-Type", "application/json");
  30. AsyncOperation op = req.SendWebRequest();
  31. while (!op.isDone)
  32. {
  33. yield return null;
  34. }
  35. if (req.responseCode != 200)
  36. {
  37. Logging.LogError($"Authentication with email {email} returned code {req.responseCode} and was aborted. Response:\n{req.downloadHandler.text}");
  38. yield break;
  39. }
  40. AuthenticationResponse resp = JsonUtility.FromJson<AuthenticationResponse>(req.downloadHandler.text);
  41. then(resp);
  42. }
  43. }
  44. [Serializable]
  45. public struct AuthenticationResponse
  46. {
  47. public string PublicId;
  48. public string EmailAddress;
  49. public string DisplayName;
  50. public bool Confirmed;
  51. public string Token;
  52. public uint ID;
  53. }
  54. }