Authentication#

To fully interact with Xsolla Backend services, a local user must be logged in. There are several types of login methodologies utilized by Xsolla Backend.

Chess showcases two types of login:#

  • Device

  • Email/Password

Device Login#

To perform device login, there is a single line call:

await _sdk.Instance.LoginDevice();

This in turn will use a generated device id to log the local user without friction.

It is recommended to store off a login token locally, for subsequent logins. In Unity, a good place to store off the login token is in PlayerPrefs:

 1// Grab a stored token from the previous login
 2string token = PlayerPrefs.GetString("logToken", null);
 3
 4// If token doesnt exist use device login
 5if (string.IsNullOrEmpty(token))
 6    await _sdk.Instance.LoginDevice();
 7else
 8    await _sdk.Instance.LoginToken(token);
 9
10_user = _sdk.Instance.LoggedInUser;

Email/Password Login#

Performing an email/password login is similar to Device Login in that it is a single-line call.

In Chess we perform a two-part login. First we register the local user.

To register a local user, a User object must be created and populated with:

  • Name

  • FirstName

  • LastName

  • Email

  • Roles

Once the User object has been created, the user can be registered with a single-line call:

_user = await _sdk.Instance.RegisterUserAndPassword(user, pass);

This will return a new user object for the newly created local user:

 1public async Task AXRRegisterEmail(string email, string pass)
 2{
 3    try
 4    {
 5        // Create a new user
 6        User user = new User()
 7        {
 8            Name = email,
 9            FirstName = "chess",
10            LastName = "player",
11            Email = email,
12            Roles = new List<string>(),
13        };
14
15        // Register user with supplied password
16        _user = await _sdk.Instance.RegisterUserAndPassword(user, pass);
17
18        // Define email variable in PlayerPrefs
19        if (_user != null)
20            PlayerPrefs.SetString(EMAIL_ID, _user.Email);
21    }
22    catch (Exception e)
23    {
24        throw e;
25    }
26}

Next we login the local user. This can be done with this single line call:

await _sdk.Instance.LoginPassword(email, pass);

Once the LoginPassword method is called the user can be pulled from the CoreSdk instance’s LoggedInUser property.

_user = _sdk.Instance.LoggedInUser;