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.
100 lines
2.8 KiB
100 lines
2.8 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class LoginManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private TextMeshProUGUI _loginName;
|
|
[SerializeField] private TextMeshProUGUI _loginPassword;
|
|
|
|
[SerializeField] private TextMeshProUGUI _regName;
|
|
[SerializeField] private TextMeshProUGUI _regEmail;
|
|
[SerializeField] private TextMeshProUGUI _regPassword;
|
|
[SerializeField] private TextMeshProUGUI _regPassword2;
|
|
|
|
[SerializeField] private GameObject startPanel;
|
|
[SerializeField] private GameObject loginPanel;
|
|
[SerializeField] private GameObject regPanel;
|
|
|
|
[System.Serializable]
|
|
class TempID
|
|
{
|
|
public int id;
|
|
}
|
|
|
|
public void TryReg()
|
|
{
|
|
StartCoroutine(RegisterReq());
|
|
}
|
|
|
|
public void TryLogin()
|
|
{
|
|
StartCoroutine(TryLoginReq());
|
|
}
|
|
|
|
private IEnumerator RegisterReq()
|
|
{
|
|
string url = "http://localhost:3000/api/RegUser";
|
|
|
|
string fields = $@"{{ ""username"": ""{_regName.text}"",
|
|
""email"": ""{_regEmail.text}"",
|
|
""password"": ""{_regPassword.text}""}}";
|
|
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Put(url, fields))
|
|
{
|
|
www.method = "POST";
|
|
www.SetRequestHeader("Content-Type", "application/json");
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError(www.error);
|
|
}
|
|
else
|
|
{
|
|
string responseBody = www.downloadHandler.text;
|
|
Debug.Log("Response: " + responseBody);
|
|
}
|
|
}
|
|
}
|
|
|
|
private IEnumerator TryLoginReq()
|
|
{
|
|
string url = "http://localhost:3000/api/TryLogin";
|
|
url += $"?login={_loginName.text}&password={_loginPassword.text}";
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
www.method = "GET";
|
|
www.SetRequestHeader("Content-Type", "application/json");
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError(www.error);
|
|
}
|
|
else
|
|
{
|
|
string responseBody = www.downloadHandler.text;
|
|
|
|
if (responseBody == "")
|
|
{
|
|
Debug.Log("NoCorrect");
|
|
}
|
|
else
|
|
{
|
|
TempID tempID = JsonUtility.FromJson<TempID>(responseBody);
|
|
Debug.Log("Response: " + tempID.id);
|
|
SceneManager.LoadScene("TempGameplay");
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|