In class HttpManager I have a string for a token which im getting from response from server when Im logged in. So then I want to use this token to make Request “/initialize” and I want to provide this token in header of the request but before this, that token goes into null
My HttpManager.cs - its not MonoBehaviour
using System.Threading;
using Dtos.Authorize;
using Dtos.Finish;
using Dtos.Initialize;
using Dtos.JWT;
using UnityEngine;
using UnityEngine.Networking;
public class HttpManager
{
private const string URL = "https://localhost:7071";
public string token;
public (bool success, ResponseInitializeDto) SendInitializeRequest(RequestInitializeDto initializeRequestDTO)
{
var json = JsonUtility.ToJson(initializeRequestDTO);
var www = new UnityWebRequest(URL + "/initialize", "POST");
var jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.certificateHandler = new CertificateOverride();
www.SetRequestHeader("Authorization","Bearer " + token);
www.SetRequestHeader("Content-Type", "application/json");
www.SendWebRequest();
Thread.Sleep(300);
if (www.result == UnityWebRequest.Result.Success)
{
Debug.Log("Initialize response success");
Debug.Log(www.downloadHandler.text);
var response = JsonUtility.FromJson<ResponseInitializeDto>(www.downloadHandler.text);
return (true, response);
}
Debug.Log("Wrong initialize response");
return (false, new ResponseInitializeDto());
}
public (bool succes, TokenDto) SendLoginRequest(AuthorizeDto authorizeDto)
{
var json = JsonUtility.ToJson(authorizeDto);
var www = new UnityWebRequest(URL + "/authentication/login", "POST");
var jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.certificateHandler = new CertificateOverride();
www.SetRequestHeader("Content-Type", "application/json");
www.SendWebRequest();
Thread.Sleep(300);
if (www.result == UnityWebRequest.Result.Success)
{
Debug.Log("Login response success");
Debug.Log(www.downloadHandler.text);
var response = JsonUtility.FromJson<TokenDto>(www.downloadHandler.text);
token = response.token;
return (true, response);
}
Debug.Log("Wrong login response");
token = string.Empty;
return (false, new TokenDto());
}
public (bool succes, TokenDto) SendRegisterRequest(AuthorizeDto authorizeDto)
{
var json = JsonUtility.ToJson(authorizeDto);
var www = new UnityWebRequest(URL + "/authentication/register", "POST");
var jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.certificateHandler = new CertificateOverride();
www.SetRequestHeader("Content-Type", "application/json");
www.SendWebRequest();
Thread.Sleep(300);
if (www.result == UnityWebRequest.Result.Success)
{
Debug.Log("Register response success");
Debug.Log(www.downloadHandler.text);
var response = JsonUtility.FromJson<TokenDto>(www.downloadHandler.text);
token = response.token;
return (true, response);
}
Debug.Log("Wrong register response");
token = string.Empty;
return (false, new TokenDto());
}
}
Thats the AuthorizeManager and this SendLogin method is calling the SendLoginRequest method inside of HttpManager
using Dtos.Authorize;
using TMPro;
using UnityEngine;
public class AuthorizeManager : MonoBehaviour
{
[SerializeField] private string token;
[SerializeField] private TMP_InputField loginUsernameInput;
[SerializeField] private TMP_InputField loginPasswordInput;
[SerializeField] private TMP_InputField registerUsernameInput;
[SerializeField] private TMP_InputField registerPasswordInput;
private readonly HttpManager _httpManager = new HttpManager();
public void SendLogin()
{
var username = loginUsernameInput.text;
var password = loginPasswordInput.text;
var credentials = new AuthorizeDto()
{
username = username,
password = password
};
var (success, tokenDto) = _httpManager.SendLoginRequest(credentials);
token = success ? tokenDto.token : string.Empty;
GameManager.GetInstance().InitializeGame();
}
public void SendRegister()
{
var username = registerUsernameInput.text;
var password = registerPasswordInput.text;
var credentials = new AuthorizeDto()
{
username = username,
password = password
};
var (success, tokenDto) = _httpManager.SendRegisterRequest(credentials);
token = success ? tokenDto.token : string.Empty;
}
}
And then SendLogin method calls the InitializeGame method inside of the GameManager singleton class
In GameManager.cs
public void InitializeGame()
{
var initializeRequestDto = new RequestInitializeDto
{
id = id
};
var (success, initializeResponseDto) =
_httpManager.SendInitializeRequest(initializeRequestDto);
if (success)
{
id = initializeResponseDto.id;
wonCount = initializeResponseDto.wonCount;
totalMoneyWon = initializeResponseDto.totalMoneyWon;
canPlay = initializeResponseDto.canPlay;
loginScreen.SetActive(false);
gameCanvas.SetActive(true);
}
else
{
print("Wrong response from Server");
}
}
I noticed when I was debugging the token goes null right before the SendInitializeRequest method is called.
Login Request is returning token string