Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AccelByte Sample Game Unity - Auth Logic
public void Start()
{
if (useSteam)
{
#if UNITY_STANDALONE && !DISABLESTEAMWORKS
isUsingOtherPlatform = true;
loginType = E_LoginType.Steam;
UIHandlerAuthComponent.loginPanel.gameObject.SetActive(false);
Debug.Log("Valid ABUSER:"+abUser.Session.IsValid());
Debug.Log("Valid Steam Auth:" + steamAuth.isActiveAndEnabled);
abUser.LoginWithOtherPlatform(PlatformType.Steam, steamAuth.GetSteamTicket(), OnLogin);
Debug.Log("USE STEAM");
UIHandlerAuthComponent.mainMenuLogoutButton.gameObject.SetActive(false);
#endif
}
else
{
Debug.Log("Don't USE STEAM");
#if UNITY_STANDALONE
UIHandlerAuthComponent.gameObject.SetActive(true);
// Try to login with launcher
LoginWithLauncher();
#elif UNITY_STADIA
UIHandlerAuthComponent.loginPanel.gameObject.SetActive(false);
// Try to login with stadia
Debug.Log("[LF] Login with stadia");
LoginWithStadia();
UIHandlerAuthComponent.mainMenuLogoutButton.gameObject.SetActive(false);
#endif
}
}
//Attempts to log the user in
public void Login()
{
if (string.IsNullOrEmpty(UIHandlerAuthComponent.loginEmail.text))
{
if (string.IsNullOrEmpty(UIHandlerAuthComponent.loginPassword.text))
ShowErrorMessage(true, "Please fill your email address and password");
else
ShowErrorMessage(true, "Please fill your email address");
}
else if (string.IsNullOrEmpty(UIHandlerAuthComponent.loginPassword.text))
{
ShowErrorMessage(true, "Please fill your password");
}
else
{
loginType = E_LoginType.Username;
abUser.LoginWithUsername(UIHandlerAuthComponent.loginEmail.text, UIHandlerAuthComponent.loginPassword.text, OnLogin);
UIElementHandler.ShowLoadingPanel();
}
}
//Attempts to login with launcher
public void LoginWithLauncher()
{
isUsingOtherPlatform = true;
// Check if auth code is available from launcher
string authCode = Environment.GetEnvironmentVariable(AUTHORIZATION_CODE_ENVIRONMENT_VARIABLE);
if (authCode != null)
{
loginType = E_LoginType.Launcher;
abUser.LoginWithLauncher(OnLogin);
UIElementHandler.ShowLoadingPanel();
}
else
{
Debug.Log("LoginWithLauncher authCode is null");
}
}
//Handles User Login, on Success it will get the User's account details
private void OnLogin(Result result)
{
if (result.IsError)
{
Debug.Log("[LF] OnLogin Failed");
if (!useSteam)
{
UIElementHandler.HideLoadingPanel();
}
Debug.Log("Login failed:" + result.Error.Message);
Debug.Log("Login Response Code: " + result.Error.Code);
//Show Error Message
if (loginType == E_LoginType.Launcher)
{
ShowErrorMessage(true, "Can't login from launcher.");
}
else if (loginType == E_LoginType.Username)
{
ShowErrorMessage(true, "Incorrect email address or password.");
}
else if (loginType == E_LoginType.Steam)
{
ShowErrorMessage(true, "Can't login from steam");
}
else if(loginType == E_LoginType.Stadia)
{
ShowErrorMessage(true, "Can't login from stadia");
}
}
else
{
Debug.Log("Login successful. Getting User Data");
//Show Login Successful
//Show "Getting User Details"
GetUserDetails();
ShowErrorMessage(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment