Skip to content

Instantly share code, notes, and snippets.

@TobiasBerg
Created February 9, 2021 09:21
Show Gist options
  • Save TobiasBerg/657d3a5bf6b5c93c44046a9b79569337 to your computer and use it in GitHub Desktop.
Save TobiasBerg/657d3a5bf6b5c93c44046a9b79569337 to your computer and use it in GitHub Desktop.
IEnumerator Login()
{
if (loginStarted)
{
loginDone = true;
loginFailed = true;
Debug.Log("Multiple calls to login detected. Rejected attempt.");
}
else
{
loginStarted = true;
Debug.Log("Login called");
// Set up the information for the auth request.
LoginPayload payload = new LoginPayload();
payload.game_key = this.LootLockerAPIKey;
payload.player_identifier = this.PlayerIdentifier;
payload.platform = "steam";
payload.game_version = "0.0.1";
String requestBody = payload.ToJSON();
Debug.Log(requestBody);
using (UnityWebRequest webRequest = UnityWebRequest.Post(LootLocker.baseUrl + "v2/session", ""))
{
// Create an upload handler and turn the string into a byte array
UploadHandler uploader = new UploadHandlerRaw(Encoding.ASCII.GetBytes(requestBody));
webRequest.uploadHandler = uploader;
// Set header to let the server know we are sending JSON
webRequest.SetRequestHeader("Content-Type", "application/json");
// Send request and wait for the response
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError || webRequest.isHttpError)
{
Debug.Log("Login failed!");
Debug.Log(webRequest.error);
Debug.Log(webRequest.downloadHandler.text);
// Login failed, make sure to break the while loop in Start()
this.loginFailed = true;
this.loginDone = true;
}
else
{
Debug.Log("Login succeeded!");
Debug.Log(webRequest.responseCode);
Debug.Log(webRequest.downloadHandler.text);
// Login succeeded! Parse the response so we can use the data returned
PlayerInfoResponse pir = PlayerInfoResponse.ParseSuccessfulLoginResponse(webRequest.downloadHandler.text);
Debug.Log(pir.player_id);
Debug.Log(pir.session_token);
// Mark login as done and save the request token
this.loginDone = true;
this.sessionToken = pir.session_token.ToString();
}
}
Debug.Log("Login completed");
}
}
[System.Serializable]
public class PlayerInfoResponse
{
public string request_token;
public bool success;
public bool seen_before;
public int player_id;
public bool check_grant_notifications;
public bool check_deactivation_notifications;
public static PlayerInfoResponse ParseSuccessfulLoginResponse(string jsonString)
{
return JsonUtility.FromJson<PlayerInfoResponse>(jsonString);
}
}
[System.Serializable]
public class LoginPayload
{
public string game_key;
public string player_identifier;
public string game_version;
public string platform;
public string ToJSON()
{
return JsonUtility.ToJson(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment