Access
Connect cross-platform accounts & identity management
using AccelByte.Api;
using AccelByte.Core;
using AccelByte.Models;
User user = AccelBytePlugin.GetUser();
user.LoginWithUsername(username, password,
result =>
{
if (result.IsError)
{
Debug.Log("Login failed");
// some actions
}
else
{
Debug.Log("Login successful");
// some actions
}
});
AccelBytePlugin.GetUser().Session.IsValid();
In this section, you will learn how to log in and connect to the IAM Services.
using AccelByte.Api;
using AccelByte.Models;
using AccelByte.Core;
// Function called to Login by email to AccelByte's IAM services.
public void OnLoginClick(string username, string password)
{
// Grab a current User reference, even if it has not logged in yet.
User user = AccelBytePlugin.GetUser();
// Call the Login Function and supplying a asynchronous callback to act based upon success or failure
user.LoginWithUsername(username,password,
result =>
{
if (result.IsError)
{
// Print the Error Code and Message if error happened
Debug.Log($"Login failed : {result.Error.Code}: {result.Error.Message}");
}
else
{
Debug.Log("Login successful");
}
});
}
void Start()
{
OnLoginClick("user@example.net","SuperPassword");
}
AccelBytePlugin.GetUser().Session.IsValid();
For testing purposes, place this code in the Update function of your script:
void Update()
{
if (AccelBytePlugin.GetUser().Session.IsValid())
{
Debug.Log("Logged in");
}
}
When you press Play, you should now see that the player is correctly logged in and the stored credentials are valid. Once completed, remove this debug check from your script.
TROUBLESHOOTING
Troubleshooting If you encounter a Login failed message, check your login credentials or the API URLs supplied in the AccelByteSDKConfig.json file. If the error is related to .JSON reading, check the format of your AccelByteSDKConfig.json file.
Congratulations! You have successfully learnt how to log in.
Continue on for a step by step example of the UI and code implementation. Otherwise, you are now ready to move on to the Lobby (opens new window).
using UnityEngine.UI;
Inside the class, add references to the UI components you have just created:
[SerializeField]
Button loginButton;
[SerializeField]
Text statusText;
[SerializeField]
InputField usernameInputField;
[SerializeField]
InputField passwordInputField;
[SerializeField]
RectTransform loginPanel;
private void OnEnable()
{
// When we Activate, set the Text of the Login Status
// and add the Login Call to the Button's listener
statusText.text = "Please Login";
loginButton.onClick.AddListener(()
=>
{
statusText.text = "Attempting Login";
OnLoginClick(usernameInputField.text, passwordInputField.text);
});
}
private void OnDisable()
{
// When we disable, clear all of the Listeners from the Login Button
loginButton.onClick.RemoveAllListeners();
}
public void OnLoginClick(string username, string password)
{
// Disable Interaction with the Login Button so the player cannot spam click it and send multiple requests
loginButton.interactable = false;
statusText.text = "Logging in...";
...
user.LoginWithUsername(username, password,
result =>
{
if (result.IsError)
{
// If we error, grab the Error Code and Message to print in the Log
Debug.Log($"Login failed : {result.Error.Code}: {result.Error.Message}");
// Set the Status Text to display the Error if there is any
statusText.text = $"Login failed : {result.Error.Code}: {result.Error.Message}";
}
else
{
Debug.Log("Login successful");
// Set the loginPanel to be inactive, (In later tutorials, we'll also use this functionality to display the Lobby/Main Menu)
loginPanel.gameObject.SetActive(false);
}
// Enable interaction with the Button again
loginButton.interactable = true;
});
...
If your credentials are correct, the scene will become blank as the LoginPanel disappears.
Congratulations! You have now fully implemented the Login, which is the gateway to all other AccelByte services.
Proceed to the next section to learn how to implement Lobby Services (opens new window).
// Copyright (c) 2021 - 2022 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AccelByte.Api;
using AccelByte.Models;
using AccelByte.Core;
using UnityEngine.Serialization;
using UnityEngine.UI;
public class LoginHandler : MonoBehaviour
{
[SerializeField]
Button loginButton;
[SerializeField]
Text statusText;
[SerializeField]
InputField usernameInputField;
[SerializeField]
InputField passwordInputField;
[SerializeField]
RectTransform loginPanel;
// Start is called before the first frame update
void Start()
{
}
private void OnEnable()
{
// When we Activate, set the Text of the Login Status
// and add the Login Call to the Button's listener
statusText.text = "Please Login";
loginButton.onClick.AddListener(()
=>
{
statusText.text = "Attempting Login";
OnLoginClick(usernameInputField.text, passwordInputField.text);
});
}
private void OnDisable()
{
// When we disable, clear all of the Listeners from the Login Button
loginButton.onClick.RemoveAllListeners();
}
// Update is called once per frame
void Update()
{
// if (AccelBytePlugin.GetUser().Session.IsValid())
// {
// Debug.Log("Logged in");
// }
}
/// <summary>
/// Function called to Login to AccelByte's IAM services
/// </summary>
/// <param name="username">The Username (typically an email address) of the user</param>
/// <param name="password">The password of the user</param>
public void OnLoginClick(string username, string password)
{
// Disable Interaction with the Login Button so the player cannot spam click it and send multiple requests
loginButton.interactable = false;
statusText.text = "Logging in...";
// Grab a reference to the current User, even though they have not been logged in yet.
// This also acts as the initialisation point for the whole AccelByte plugin.
User user = AccelBytePlugin.GetUser();
// Calling the Login Function and supplying a callback to act upon based upon success or failure
// You will almost certainly want to extend this functionality further
// Note that this callback is asynchronous
user.LoginWithUsername(username, password,
result =>
{
if (result.IsError)
{
// If we error, grab the Error Code and Message to print in the Log
Debug.Log($"Login failed : {result.Error.Code}: {result.Error.Message}");
//Set the Status Text to display the Error if there is any
statusText.text = $"Login failed : {result.Error.Code}: {result.Error.Message}";
}
else
{
Debug.Log("Login successful");
loginPanel.gameObject.SetActive(false);
}
//Enable interaction with the Button again
loginButton.interactable = true;
});
}
}