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

# Session Management

> Manage user authentication sessions in your Unity application.

## Authentication state

### Get current token

```csharp theme={"system"}
string token = DynamicSDK.Instance.Auth.Token;

if (!string.IsNullOrEmpty(token))
{
    Debug.Log("User is authenticated");
}
```

### Subscribe to token changes

```csharp theme={"system"}
DynamicSDK.Instance.Auth.OnTokenChanged += (token) =>
{
    if (!string.IsNullOrEmpty(token))
    {
        Debug.Log("User logged in");
        // Navigate to main screen, load wallets, etc.
    }
    else
    {
        Debug.Log("User logged out");
        // Navigate to login screen, clear UI, etc.
    }
};
```

### Get current user profile

```csharp theme={"system"}
DynamicSDK.Instance.Auth.OnUserChanged += (user) =>
{
    if (user != null)
    {
        Debug.Log($"User email: {user.Email}");
        Debug.Log($"User ID: {user.UserId}");
    }
};
```

## Logout

```csharp theme={"system"}
await DynamicSDK.Instance.Auth.Logout();
```

## Complete example

```csharp theme={"system"}
using DynamicSDK.Core;
using UnityEngine;

public class SessionManager : MonoBehaviour
{
    private void Start()
    {
        // Subscribe to auth state changes
        DynamicSDK.Instance.Auth.OnTokenChanged += HandleTokenChanged;
        DynamicSDK.Instance.Auth.OnUserChanged += HandleUserChanged;
        
        // Check if already authenticated
        CheckExistingSession();
    }
    
    private void CheckExistingSession()
    {
        string token = DynamicSDK.Instance.Auth.Token;
        
        if (!string.IsNullOrEmpty(token))
        {
            Debug.Log("Existing session found");
            OnAuthenticated();
        }
        else
        {
            Debug.Log("No existing session");
            ShowLoginScreen();
        }
    }
    
    private void HandleTokenChanged(string token)
    {
        if (!string.IsNullOrEmpty(token))
        {
            OnAuthenticated();
        }
        else
        {
            OnLoggedOut();
        }
    }
    
    private void HandleUserChanged(UserProfile user)
    {
        if (user != null)
        {
            Debug.Log($"Welcome, {user.Email}!");
        }
    }
    
    private void OnAuthenticated()
    {
        // Navigate to main screen
        // Load user wallets
    }
    
    private void OnLoggedOut()
    {
        // Clear user data
        // Navigate to login screen
    }
    
    private void ShowLoginScreen()
    {
        // Show your login UI or Dynamic Widget
        DynamicSDK.Instance.UI.ShowAuth();
    }
    
    public async void Logout()
    {
        await DynamicSDK.Instance.Auth.Logout();
    }
    
    private void OnDestroy()
    {
        // Unsubscribe from events
        DynamicSDK.Instance.Auth.OnTokenChanged -= HandleTokenChanged;
        DynamicSDK.Instance.Auth.OnUserChanged -= HandleUserChanged;
    }
}
```

## Next steps

* [Wallet Creation](/docs/unity/wallet-creation) - Learn about wallet creation after authentication
* [Authentication](/docs/unity/authentication) - Implement different authentication methods
