61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Text;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.Networking;
|
||
|
|
||
|
namespace CLre_server.API.Utility
|
||
|
{
|
||
|
public static class CardLifeUserAuthentication
|
||
|
{
|
||
|
[Serializable]
|
||
|
private struct AuthPayload
|
||
|
{
|
||
|
public string EmailAddress;
|
||
|
public string Password;
|
||
|
}
|
||
|
|
||
|
private const string LOGIN_URL = "https://live-auth.cardlifegame.com/api/auth/authenticate";
|
||
|
|
||
|
public delegate void OnResponse(AuthenticationResponse data);
|
||
|
|
||
|
public static IEnumerator Authenticate(string email, string password, OnResponse then)
|
||
|
{
|
||
|
UnityWebRequest req = new UnityWebRequest(LOGIN_URL, "POST");
|
||
|
AuthPayload payload = new AuthPayload
|
||
|
{
|
||
|
EmailAddress = email,
|
||
|
Password = password,
|
||
|
};
|
||
|
byte[] bytes = Encoding.UTF8.GetBytes(JsonUtility.ToJson(payload));
|
||
|
req.uploadHandler = new UploadHandlerRaw(bytes);
|
||
|
req.downloadHandler = new DownloadHandlerBuffer();
|
||
|
req.SetRequestHeader("Content-Type", "application/json");
|
||
|
AsyncOperation op = req.SendWebRequest();
|
||
|
while (!op.isDone)
|
||
|
{
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
if (req.responseCode != 200)
|
||
|
{
|
||
|
Logging.LogError($"Authentication with email {email} returned code {req.responseCode} and was aborted. Response:\n{req.downloadHandler.text}");
|
||
|
yield break;
|
||
|
}
|
||
|
|
||
|
AuthenticationResponse resp = JsonUtility.FromJson<AuthenticationResponse>(req.downloadHandler.text);
|
||
|
then(resp);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Serializable]
|
||
|
public struct AuthenticationResponse
|
||
|
{
|
||
|
public string PublicId;
|
||
|
public string EmailAddress;
|
||
|
public string DisplayName;
|
||
|
public bool Confirmed;
|
||
|
public string Token;
|
||
|
public uint ID;
|
||
|
}
|
||
|
}
|