Hello,
Currently I have a trouble with the authentication. When I input the correct username and password, It responded really well. But not with wrong username/password. The ecxeption did not work well.
Here is the code
using UnityEngine;
using com.shephertz.app42.paas.sdk.csharp;
using com.shephertz.app42.paas.sdk.csharp.user;
using com.shephertz.app42.paas.sdk.csharp.session;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;
using AssemblyCSharp;
public class App42Login : MonoBehaviour
{
private const string apiKey = "78e127a04f3fba734e42bd76dbd6cc0e3d2dca95b9a110d10a86e2d7877f4993";
private const string secretKey = "7a7ee19b94644eb0a230b2c6527a7669a7fe83ac62bf63ff36ba32d2b607db0b";
//Service for creating, authenticating, deleting, updating User.
private UserService userService;
//User object that keeps informations about user.
public static User user = new User();
public GameObject username;
public GameObject password;
public string Username;
private string Password;
public bool userBlank = false;
public bool passBlank = false;
public bool authError = false;
// Use this for initialization
void Start()
{
ServiceAPI serviceAPI = new ServiceAPI(apiKey, secretKey);
userService = App42API.BuildUserService();
}
public void OnGUI()
{
if (userBlank)
{
showUserBlank();
}
else if (passBlank)
{
showPassBlank();
}
else if (authError)
{
showAuthError();
}
}
public void showUserBlank()
{
GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100), "Username cannot be blank!");
if (GUI.Button(new Rect(Screen.width / 2 - 25, Screen.height / 2 - 5, 50, 30), "OK"))
{
hideError();
}
}
public void showPassBlank()
{
GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100), "Password cannot be blank!");
if (GUI.Button(new Rect(Screen.width / 2 - 25, Screen.height / 2 - 5, 50, 30), "OK"))
{
hideError();
}
}
public void showAuthError()
{
GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100), "Username / password are incorrect!");
if (GUI.Button(new Rect(Screen.width / 2 - 25, Screen.height / 2 - 5, 50, 30), "OK"))
{
hideError();
}
}
public void hideError()
{
userBlank = false;
passBlank = false;
authError = false;
}
public void RegisterButton()
{
SceneManager.LoadScene("Register");
}
public bool checkLogin()
{
if (string.IsNullOrEmpty(Username))
{
userBlank = true;
return false;
}
else
{
if (string.IsNullOrEmpty(Password))
{
passBlank = true;
return false;
}
else
{
return true;
}
}
}
public void LoginButton()
{
if (checkLogin())
{
PlayerPrefs.SetString("username", Username);
PlayerPrefs.Save();
try
{
userService.Authenticate(Username, Password, new UnityCallBack());
SessionService sessionService = App42API.BuildSessionService();
sessionService.GetSession(Username, new UnityCallBack());
}
catch
{
authError = true;
}
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
LoginButton();
}
//get string dari textbox
Username = username.GetComponent<InputField>().text;
Password = password.GetComponent<InputField>().text;
}
}
public class UnityCallBack : App42CallBack
{
public void OnSuccess(object response)
{
User user = (User)response;
App42Log.Console("userName is " + user.GetUserName());
App42Log.Console("sessionId is " + user.GetSessionId());
SceneManager.LoadScene("Main Menu");
}
public void OnException(System.Exception e)
{
/* Above statement throws App42Exception if authentication fails with error code 2002 */
App42Log.Console("Exception : " + e);
}
}