> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hyperx.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# User Lifecycle

> Create users, sign in, refresh sessions, and manage characters from Unity.

User lifecycle is how your game connects a player to a HyperX user inside your project.

## Choose a Login Method

| Method         | Use It When                                                         |
| -------------- | ------------------------------------------------------------------- |
| Guest          | Players should start immediately without account creation.          |
| Custom account | Your game uses its own username/password flow.                      |
| Social login   | Players should recover accounts through Google, Apple, or Facebook. |

## Start a Guest User

```cs theme={null}
using UnityEngine;

public class HyperXGuestLogin : MonoBehaviour
{
    async void Start()
    {
        await HyperX.Core.Init("PROJECT_CODE");

        var player = await HyperX.Core.Users.StartGuestSession(
            authenticationToken: SystemInfo.deviceUniqueIdentifier,
            country: "US",
            language: "en"
        );

        var me = await player.Me();
        Debug.Log($"User: {me.Id}, name: {me.Name}");
    }
}
```

`StartGuestSession` signs in instead of creating a duplicate user when the same guest token already exists, then returns a `PlayerSession`. For shipped games, choose a guest token strategy that matches your recovery policy. If players need recovery after device changes, provide social login or custom account linking.

The existing token-based API remains available for advanced migration flows and compatibility adapters that manage access tokens directly.

```cs theme={null}
var rawSession = await HyperX.Core.Users.StartGuest("guest-device-or-save-id");
var player = HyperX.Core.Users.UseSession(rawSession);
```

## Custom Accounts

```cs theme={null}
await HyperX.Core.Users.RegisterCustom(
    username: "ari",
    password: "strong-password",
    country: "US",
    language: "en"
);

var player = await HyperX.Core.Users.LoginCustomSession("ari", "strong-password");
```

Usernames must be unique within the project. Duplicate names, invalid passwords, and invalid values fail with `HyperXServerException`.

## Social Login

Google, Apple, and Facebook login pass short-lived tokens from provider SDKs to HyperX. HyperX validates them with credentials registered in Console.

```cs theme={null}
var player = await HyperX.Core.Users.LoginSocialSession(
    provider: HyperX.SocialProvider.Google,
    identityToken: googleIdToken,
    country: "US",
    language: "en",
    appIdentifier: "com.example.game"
);
```

You can link a social identity to an existing HyperX user.

```cs theme={null}
await player.LinkSocialIdentity(
    provider: HyperX.SocialProvider.Apple,
    identityToken: appleIdToken
);
```

See [Provider Integrations](/guide/en/dotnet/provider-integrations) for provider app setup and Console credentials.

## Refresh a Session

Login responses include `AccessToken` for API calls and `RefreshToken` for session renewal.

```cs theme={null}
await player.RefreshIfNeeded();
var me = await player.Me();
```

Store refresh tokens securely. If the refresh token expires, sign in again.

## Current User

```cs theme={null}
var me = await player.Me();
Debug.Log($"{me.Id} / {me.Name}");
```

You can confirm the same user in Console on the `Users` page.

## Name and Characters

```cs theme={null}
var name = await player.CheckName("Ari");

if (name.Available)
{
    await player.UpdateName("Ari");
}
```

```cs theme={null}
var character = await player.CreateCharacter(name: "Ari");

await player.UpdateRepresentativeCharacter(character.Id);
```

The representative character is used for profile display and the default Character Data convenience APIs. Data for non-representative characters can be saved and loaded by passing the character ID explicitly.

## Logout

```cs theme={null}
await player.Logout();
```

To end every active session:

```cs theme={null}
await player.LogoutAll();
```

## Account Deletion and Recovery

When a player requests account deletion, the account status changes to `deletion_requested` and HyperX records the scheduled deletion time. Operators can review the request in Console Users, then finalize or cancel it according to your retention policy.

```cs theme={null}
var lifecycle = await player.RequestDeletion(reason: "player requested");

Debug.Log(lifecycle.AccountStatus);
Debug.Log(lifecycle.DeletionScheduledAt);
```

Canceling a deletion request requires a recovery token. Deliver that token through your authentication or support flow.

```cs theme={null}
var recovery = await HyperX.Core.Users.RequestRecovery(
    username: "ari"
);

await HyperX.Core.Users.ConfirmRecovery(recovery.RecoveryToken);
```

If the current signed-in session receives the token, it can cancel deletion directly.

```cs theme={null}
await player.CancelDeletion(recovery.RecoveryToken);
```

Finalized deleted accounts cannot sign in or refresh sessions. Purchases, refunds, audit logs, and other retention-sensitive records can remain according to the project's data policy.
