Exemplo n.º 1
0
    public IEnumerator registrarUsuarioHeroku()
    {
        var document1 = new BsonDocument {
            { "username", inputFieldUserRegister.GetComponent <Text>().text }
        };
        var request1 = new UnityWebRequest(baseHeroku + "/getUser", "POST");

        byte[] bodyRaw1 = Encoding.UTF8.GetBytes(document1.ToString());
        request1.uploadHandler   = (UploadHandler) new UploadHandlerRaw(bodyRaw1);
        request1.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        request1.SetRequestHeader("Content-Type", "application/json");
        yield return(request1.SendWebRequest());

        if (request1.isNetworkError || request1.isHttpError)
        {
            Debug.LogError(request1.error);
            yield break;
        }

        bool usuarioInfo1 = JSON.Parse(request1.downloadHandler.text);

        if (usuarioInfo1 == false)
        {
            if (inputFieldUserRegister.GetComponent <Text>().text != "" && inputFieldpasswordRegister.GetComponent <InputField>().text != "")
            {
                String passwordHash = SecureHelper.Hash(inputFieldpasswordRegister.GetComponent <InputField>().text);
                var    document     = new BsonDocument {
                    { "username", inputFieldUserRegister.GetComponent <Text>().text }, { "password", passwordHash }
                };
                var    request = new UnityWebRequest(baseHeroku + "/newUser", "POST");
                byte[] bodyRaw = Encoding.UTF8.GetBytes(document.ToString());
                request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(bodyRaw);
                request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
                request.SetRequestHeader("Content-Type", "application/json");
                yield return(request.SendWebRequest());

                if (request.isNetworkError || request.isHttpError)
                {
                    Debug.LogError(request.error);
                    yield break;
                }

                JSONNode usuarioInfo = JSON.Parse(request.downloadHandler.text);
                Debug.Log(usuarioInfo);
                textDisplayRegister.text = "Registro completado";
                inputFieldUserLoginParent.GetComponent <InputField>().text = inputFieldUserRegister.GetComponent <Text>().text;
                inputFieldpasswordLogin.GetComponent <InputField>().text   = inputFieldpasswordRegister.GetComponent <InputField>().text;
                LoginUsuario();
                stateManager.loginGame();
            }
            else
            {
                textDisplayRegister.text = "¡No dejes campos vacíos!";
            }
        }
        else
        {
            textDisplayRegister.text = "¡El usuario ya existe!";
        }
    }
Exemplo n.º 2
0
    public IEnumerator LoginUsuarioHeroku()
    {
        if (inputFieldUserLogin.GetComponent <Text>().text != "" && inputFieldpasswordLogin.GetComponent <InputField>().text != "")
        {
            String passwordHash = SecureHelper.Hash(inputFieldpasswordLogin.GetComponent <InputField>().text);
            var    document1    = new BsonDocument {
                { "username", inputFieldUserLogin.GetComponent <Text>().text }, { "password", passwordHash }
            };
            var    request1 = new UnityWebRequest(baseHeroku + "/LoginUser", "POST");
            byte[] bodyRaw1 = Encoding.UTF8.GetBytes(document1.ToString());
            request1.uploadHandler   = (UploadHandler) new UploadHandlerRaw(bodyRaw1);
            request1.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
            request1.SetRequestHeader("Content-Type", "application/json");
            yield return(request1.SendWebRequest());

            if (request1.isNetworkError || request1.isHttpError)
            {
                Debug.LogError(request1.error);
                yield break;
            }

            JSONNode usuarioInfo1 = JSON.Parse(request1.downloadHandler.text);
            string   opciones     = request1.downloadHandler.text;
            if (opciones != "null")
            {
                if (opciones == "ok")
                {
                    textDisplayLogin.text          = "Este nombre de usuario existe.";
                    usuarioSave                    = inputFieldUserLogin.GetComponent <Text>().text;
                    textDisplayLogin.text          = "";
                    textDisplayUsuarioLoginOK.text = "Bienvenido " + usuarioSave;
                    inputFieldUserLoginParent.GetComponent <InputField>().text = "";
                    inputFieldpasswordLogin.GetComponent <InputField>().text   = "";
                    SaveDataToFileAndMongo(0);
                    inputFieldpasswordLogin.active   = false;
                    inputFieldUserLoginParent.active = false;
                    BtnLogout.active = true;
                    textDisplayUsuarioLoginOK.enabled = true;
                    BtnLogin.active = false;
                    textDisplayCrearCuenta.enabled       = false;
                    BtnRegister.active                   = false;
                    textDisplayLogin.enabled             = false;
                    v_boton_loginUI.image.overrideSprite = Image2Login;
                }
                else
                {
                    textDisplayLogin.text = "La contraseña es incorrecta. Inténtalo de nuevo.";
                }
            }
            else
            {
                textDisplayLogin.text = "Este nombre de usuario no existe.";
            }
        }
        else
        {
            textDisplayLogin.text = "¡No dejes campos vacíos!";
        }
    }