# Login Events

## Inherit from EasyManager.cs

```csharp
using EasyCodeForVivox;

public class VivoxManager : EasyManager
{

}
```

## Inject EasyEvents

```csharp
using EasyCodeForVivox.Events;
using UnityEngine;
using Zenject;

public class VivoxEvents : MonoBehaviour
{
    EasyEvents _events;

    [Inject]
    private void Initialize(EasyEvents events)
    {
        _events = events;
    }
}
```

## Dynamic Events

#### Make sure Dynamic Events are enabled

<figure><img src="https://3152232848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWHF2U_tfZEUaf_IsL-%2Fuploads%2FgNWd8mgjCB90w9n3fR8A%2FEnableDynamicEvents.png?alt=media&#x26;token=f9e1f6a3-8d21-49a1-9ff5-d1a8151efad1" alt=""><figcaption></figcaption></figure>

## How to Subscribe to Login Events in EasyCode&#x20;

#### EasyManager

{% hint style="info" %}
Keeping the *<mark style="color:blue;">**base**</mark>* methods are not necessary. They are simply <mark style="color:blue;">**Debug.Logs()**</mark>. Feel free to delete them
{% endhint %}

```csharp
// Login Event Callbacks

protected override void OnLoggingIn(ILoginSession loginSession)
{
    base.OnLoggingIn(loginSession);
}

protected override void OnLoggedIn(ILoginSession loginSession)
{
    base.OnLoggedIn(loginSession);
}

protected override void OnLoggingOut(ILoginSession loginSession)
{
    base.OnLoggingOut(loginSession);
}

protected override void OnLoggedOut(ILoginSession loginSession)
{
    base.OnLoggedOut(loginSession);
}

protected override void OnLoginAdded(AccountId accountId)
{
    base.OnLoginAdded(accountId);
}

protected override void OnLoginRemoved(AccountId accountId)
{
    base.OnLoginRemoved(accountId);
}

protected override void OnLoginUpdated(ILoginSession loginSession)
{
    base.OnLoginUpdated(loginSession);
}
```

#### EasyEvents

```csharp
public void SubscribeToLoginEvents()
{
    _events.LoggingIn += OnLoggingIn;
    _events.LoggedIn += OnLoggedIn;
    _events.LoggingOut += OnLoggingOut;
    _events.LoggedOut += OnLoggedOut;

    _events.LoginAdded += OnLoginAdded;
    _events.LoginRemoved += OnLoginRemoved;
    _events.LoginUpdated += OnLoginUpdated;
}

public void UnsubscribeToLoginEvents()
{
    _events.LoggingIn -= OnLoggingIn;
    _events.LoggedIn -= OnLoggedIn;
    _events.LoggingOut -= OnLoggingOut;
    _events.LoggedOut -= OnLoggedOut;

    _events.LoginAdded -= OnLoginAdded;
    _events.LoginRemoved -= OnLoginRemoved;
    _events.LoginUpdated -= OnLoginUpdated;
}


#region Login / Logout Callbacks

protected virtual void OnLoggingIn(ILoginSession loginSession)
{
        Debug.Log($"Logging In : {loginSession.LoginSessionId.DisplayName}");
}

protected virtual void OnLoggedIn(ILoginSession loginSession)
{
    Debug.Log($"Logged in : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
}

protected virtual void OnLoggingOut(ILoginSession loginSession)
{
        Debug.Log($"Logging out : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
}

protected virtual void OnLoggedOut(ILoginSession loginSession)
{
        Debug.Log($"Logged out : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
}

protected virtual void OnLoginAdded(AccountId accountId)
{
        Debug.Log($"LoginSession Added : For user {accountId.Name}");
}

protected virtual void OnLoginRemoved(AccountId accountId)
{
        Debug.Log($"Login Removed : For user {accountId}");
}

protected virtual void OnLoginUpdated(ILoginSession loginSession)
{
        Debug.Log($"LoginSession has been Updated : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");
}

#endregion
```

#### Dynamic Events

Make sure the parameter in your method matches the event type. See what parameter is required for each [Login Event here](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#login-events). Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this **there is no need to Subscribe/Unsubscribe from events** with the usual <mark style="color:red;">**+=/-=**</mark>

```csharp
[LoginEvent(LoginStatus.LoggingIn)]
private void OnPlayerLoggingIn(ILoginSession loginSession)
{
    Debug.Log($"Logging In : {loginSession.LoginSessionId.DisplayName}");
}

[LoginEvent(LoginStatus.LoggedIn)]
private void OnPlayerLoggedIn(ILoginSession loginSession)
{
    Debug.Log($"Logged in : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
}

[LoginEvent(LoginStatus.LoggingOut)]
private void OnPlayerLoggingOut(ILoginSession loginSession)
{
    Debug.Log($"Logging out : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
}

[LoginEvent(LoginStatus.LoggedOut)]
private void OnPlayerLoggedOut(ILoginSession loginSession)
{
    Debug.Log($"Logged out : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
}

[LoginEvent(LoginStatus.LoginAdded)]
private void OnLoginSessionAdded(AccountId accountId)
{
    Debug.Log($"A new Login was added for player {accountId.DisplayName}");
}

[LoginEvent(LoginStatus.LoginRemoved)]
private void OnLoginSessionRemoved(AccountId accountId)
{
    Debug.Log($"A new Login was removed for player {accountId.DisplayName}");
}

[LoginEvent(LoginStatus.LoginValuesUpdated)]
private void OnLoginSessionValuesUpdated(ILoginSession loginSession)
{
    Debug.Log($"LoginSession has been updated for player {loginSession.LoginSessionId.DisplayName}");
}
```

#### Dynamic Async Events

Make sure the parameter in your method matches the event type. See what parameter is required for each [Login Event here](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#login-events). Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this **there is no need to Subscribe/Unsubscribe from events** with the usual <mark style="color:red;">**+=/-=**</mark>

{% hint style="info" %}
**Remember to use&#x20;**<mark style="color:blue;">**async void**</mark>**&#x20;or&#x20;**<mark style="color:blue;">**async Task**</mark>**&#x20;or else the event may run synchronously**
{% endhint %}

{% hint style="warning" %}
**More information on the methods called in any async method can be found here.** [**Unity Gaming Services Examples**](https://fullstackindie.gitbook.io/easy-code-for-vivox/related-info/unity-gaming-services)**. They are direct copies from Unity's docs.&#x20;**<mark style="color:red;">**These are just examples and don't mimic real world use cases**</mark>
{% endhint %}

```csharp
[LoginEventAsync(LoginStatus.LoggingIn)]
private async void OnPlayerLoggingInAsync(ILoginSession loginSession)
{
    Debug.Log($"Logging In : {loginSession.LoginSessionId.DisplayName}");
    await GetJoinedLobbies();
}

[LoginEventAsync(LoginStatus.LoggedIn)]
private async void OnPlayerLoggedInAsync(ILoginSession loginSession)
{
    Debug.Log($"Logged in : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
    await LoadPlayerData();
}

[LoginEventAsync(LoginStatus.LoggingOut)]
private async void OnPlayerLoggingOutAsync(ILoginSession loginSession)
{
    Debug.Log($"Logging out : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
    await RemovePlayerFromLobby();
}

[LoginEventAsync(LoginStatus.LoggedOut)]
private async void OnPlayerLoggedOutAsync(ILoginSession loginSession)
{
    Debug.Log($"Logged out : {loginSession.LoginSessionId.DisplayName}  : Presence = {loginSession.Presence.Status}");
    await SavePlayerData();
}

[LoginEventAsync(LoginStatus.LoginAdded)]
private async void OnLoginSessionAddedAsync(AccountId accountId)
{
    Debug.Log($"A new Login was added for player {accountId.DisplayName}");
    await GetJoinedLobbies();
}

[LoginEventAsync(LoginStatus.LoginRemoved)]
private async void OnLoginSessionRemovedAsync(AccountId accountId)
{
    Debug.Log($"A new Login was removed for player {accountId.DisplayName}");
    await RemovePlayerFromLobby();
}

[LoginEventAsync(LoginStatus.LoginValuesUpdated)]
private async void OnLoginSessionValuesUpdatedAsync(ILoginSession loginSession)
{
    Debug.Log($"LoginSession has been updated for player {loginSession.LoginSessionId.DisplayName}");
    await UpdatePlayerData();
}
```
