90 lines
3.9 KiB
C#
90 lines
3.9 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
|
|
public class AccountChangePassword : MonoBehaviour
|
|
{
|
|
[SerializeField] private TMP_Text changePasswordStatusText;
|
|
[SerializeField] private TMP_InputField changePasswordCurrentPasswordInput;
|
|
[SerializeField] private TMP_InputField changePasswordNewPasswordInput;
|
|
[SerializeField] private TMP_InputField changePasswordRetypeNewPasswordInput;
|
|
[SerializeField] private Button changePasswordBackButton;
|
|
[SerializeField] private Button changePasswordSubmitButton;
|
|
|
|
void Awake()
|
|
{
|
|
changePasswordBackButton.onClick.AddListener(() => AccountHandler.instance.SwitchPanel(0));
|
|
changePasswordSubmitButton.onClick.AddListener(() => ChangePassword());
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
changePasswordCurrentPasswordInput.text = "";
|
|
changePasswordNewPasswordInput.text = "";
|
|
changePasswordRetypeNewPasswordInput.text = "";
|
|
changePasswordStatusText.text = "";
|
|
}
|
|
|
|
async void ChangePassword()
|
|
{
|
|
if (changePasswordNewPasswordInput.text != changePasswordRetypeNewPasswordInput.text)
|
|
{
|
|
Tools.UpdateStatusText(changePasswordStatusText, "Passwords do not match", Color.red);
|
|
return;
|
|
}
|
|
changePasswordBackButton.interactable = false;
|
|
changePasswordSubmitButton.interactable = false;
|
|
WWWForm dataForm = new();
|
|
dataForm.AddField("oldpassword", changePasswordCurrentPasswordInput.text);
|
|
dataForm.AddField("newpassword", changePasswordNewPasswordInput.text);
|
|
dataForm.AddField("token", BazookaManager.Instance.GetAccountSession());
|
|
dataForm.AddField("username", BazookaManager.Instance.GetAccountName());
|
|
using UnityWebRequest request = UnityWebRequest.Post(SensitiveInfo.SERVER_DATABASE_PREFIX + "berrydash/changeAccountPassword.php", dataForm);
|
|
request.SetRequestHeader("Requester", "BerryDashClient");
|
|
request.SetRequestHeader("ClientVersion", Application.version);
|
|
request.SetRequestHeader("ClientPlatform", Application.platform.ToString());
|
|
await request.SendWebRequest();
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
changePasswordBackButton.interactable = true;
|
|
changePasswordSubmitButton.interactable = true;
|
|
Tools.UpdateStatusText(changePasswordStatusText, "Failed to make HTTP request", Color.red);
|
|
return;
|
|
}
|
|
string response = request.downloadHandler.text;
|
|
if (response == "-999")
|
|
{
|
|
Tools.UpdateStatusText(changePasswordStatusText, "Server error while fetching data", Color.red);
|
|
}
|
|
else if (response == "-998")
|
|
{
|
|
Tools.UpdateStatusText(changePasswordStatusText, "Client version too outdated to access servers", Color.red);
|
|
}
|
|
else if (response == "-997")
|
|
{
|
|
Tools.UpdateStatusText(changePasswordStatusText, "Encryption/decryption issues", Color.red);
|
|
}
|
|
else if (response == "-996")
|
|
{
|
|
Tools.UpdateStatusText(changePasswordStatusText, "Can't send requests on self-built instance", Color.red);
|
|
}
|
|
else
|
|
{
|
|
var jsonResponse = JObject.Parse(response);
|
|
if ((bool)jsonResponse["success"])
|
|
{
|
|
BazookaManager.Instance.SetAccountSession((string)jsonResponse["token"]);
|
|
AccountHandler.instance.SwitchPanel(0);
|
|
Tools.UpdateStatusText(AccountHandler.instance.accountLoggedIn.loggedInText, "Password changed successfully", Color.green);
|
|
}
|
|
else
|
|
{
|
|
Tools.UpdateStatusText(changePasswordStatusText, (string)jsonResponse["message"], Color.red);
|
|
}
|
|
}
|
|
changePasswordBackButton.interactable = true;
|
|
changePasswordSubmitButton.interactable = true;
|
|
}
|
|
} |