Skip to content

Commit

Permalink
Added Login
Browse files Browse the repository at this point in the history
  • Loading branch information
Himidiri committed Sep 2, 2023
1 parent 2ede2e3 commit b5d6eba
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 270 deletions.
62 changes: 58 additions & 4 deletions NPCInteractionWorld/Assets/Scripts/SceneLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Networking;
using TMPro;
using System.Text;

public class SceneLoader : MonoBehaviour
{
public GameObject loaderUI;
public GameObject errorMsg;
public Slider progressSlider;

private const string apiUrl = "https://backend-ghumo23ctq-as.a.run.app/api/login/";

// Reference to the input fields for username and password
private TMP_InputField usernameInputField;
private TMP_InputField passwordInputField;

public GameObject userName;
public GameObject password;

public void Start()
{
usernameInputField = userName.GetComponent<TMP_InputField>();
passwordInputField = password.GetComponent<TMP_InputField>();
errorMsg.SetActive(false);
}

public void LoadScene(int index)
{
StartCoroutine(LoadScene_Coroutine(index));
Expand All @@ -24,18 +44,52 @@ public IEnumerator LoadScene_Coroutine(int index)

float progress = 0;

while (!asyncOperation.isDone)
{
progress = Mathf.MoveTowards(progress,asyncOperation.progress,Time.deltaTime);
while (!asyncOperation.isDone)
{
progress = Mathf.MoveTowards(progress, asyncOperation.progress, Time.deltaTime);
progressSlider.value = progress;
if (progress >= 0.9f)
{
progressSlider.value = 1;
asyncOperation.allowSceneActivation = true;
}
yield return null;
yield return null;
}

}

public void Login()
{
StartCoroutine(sendHttpRequest());
}

IEnumerator sendHttpRequest()
{
string username = usernameInputField.text;
string password = passwordInputField.text;



var request = new UnityWebRequest(apiUrl, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes("{\"email\":\"" + username + "\",\"password\":\"" + password + "\"}");
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
Debug.Log("Status Code: " + request.responseCode);

if (request.responseCode == 200)
{
errorMsg.SetActive(false);
LoadScene(1);
}
else
{
errorMsg.SetActive(true);
}

request.downloadHandler.Dispose();
request.uploadHandler.Dispose();
request.Dispose();
}
}
Loading

0 comments on commit b5d6eba

Please sign in to comment.