Skip to content
Download all docs .md

Signing Game Users In

A player who already has an account signs in with GameFuseUser.SignInAsync. They can use their email or their username. To make a new account, see Signing Game Users Up.

Basic Usage

Set your Game ID and API key first (GameFuse Settings Configuration). Then:

using GameFuse;
using UnityEngine;

public class UserSignIn : MonoBehaviour
{
    public async void SignIn(string emailOrUsername, string password)
    {
        try
        {
            // The Game ID and API key come from GameFuseSettings
            GameFuseUser user = await GameFuseUser.SignInAsync(emailOrUsername, password);

            Debug.Log($"Signed in as {user.Username} (ID {user.Id})");
            Debug.Log($"Credits: {user.Credits}, Score: {user.Score}");
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Sign in failed: {e.Message}");
        }
    }
}

Method Signature

public static async Task<GameFuseUser> SignInAsync(
    string emailOrUsername,
    string password,
    string gameId = null,
    string gameApiKey = null,
    CancellationToken cancellationToken = default
)
  • emailOrUsername: the player's email or username
  • password: the player's password
  • gameId, gameApiKey: leave out to use the ones in GameFuseSettings
  • cancellationToken: cancels the request

It returns the signed-in GameFuseUser. The same player is also kept in GameFuseUser.CurrentUser.

The Signed-In User

After sign in or sign up, use GameFuseUser.CurrentUser from any script:

GameFuseUser me = GameFuseUser.CurrentUser;

if (me != null && me.IsAuthenticated())
{
    Debug.Log($"{me.Username} is signed in");
    await me.AddCreditsAsync(10);
}

The sign in lasts until the game closes. The SDK does not save the password or the session, so ask the player to sign in again on the next start.

Signing Out

// Sign out: CurrentUser becomes null
GameFuseUser.SignOut();

Error Handling

SignInAsync throws when it fails:

  • GameFuseApiException with StatusCode 404: wrong email, username or password
  • GameFuseApiException with another StatusCode: GameFuse refused the request, for example a wrong Game ID or API key, or a disabled game
  • ArgumentNullException: an empty email or password, or no Game ID and API key
  • Other exceptions: no internet, or the request timed out
using System.Net;
using GameFuse;
using GameFuse.Exceptions;
using UnityEngine;

public class SignInWithErrors : MonoBehaviour
{
    public async void SignIn(string emailOrUsername, string password)
    {
        try
        {
            await GameFuseUser.SignInAsync(emailOrUsername, password);
            Debug.Log("Signed in");
        }
        catch (GameFuseApiException e) when (e.StatusCode == HttpStatusCode.NotFound)
        {
            Debug.LogWarning("Wrong email, username or password");
        }
        catch (GameFuseApiException e)
        {
            Debug.LogError($"GameFuse said no ({(int)e.StatusCode}): {e.Message}");
        }
        catch (System.ArgumentNullException e)
        {
            Debug.LogError($"Something is missing: {e.ParamName}");
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Could not reach GameFuse: {e.Message}");
        }
    }
}

Playing Multiplayer as the Signed-In User

The multiplayer client plays as the signed-in user when there is one:

await GameFuseUser.SignInAsync(emailOrUsername, password);

var mp = GameFuseMultiplayerUnity.Create();   // plays as the signed-in user
var room = await mp.JoinQueue("1v1");

If the player signs in after Create(), call mp.UseGameFuseUser(). See the Unity C# client reference.

Requirements

  • GameFuse Unity SDK installed (Getting Started)
  • Your Game ID and API key set in GameFuseSettings
  • Unity 2022.3 or later