# Login

{% hint style="warning" %}
**This method is only relevant if you are not using Metafi SSO login.** If you are using Metafi SSO, you do not need to implement this function. This function does not trigger a UI modal.
{% endhint %}

## Description

This function should be called whenever a new JWT token is issued to the user when they first log in, or if the user is switched. This JWT will be authenticated via a webhook endpoint that you will provide, and only once validated can the user access their wallet.

## Details

### Signature

{% code overflow="wrap" %}

```csharp
Task Login(string userIdentifier, string token, System.Action<dynamic> callback = null)
```

{% endcode %}

### Arguments

<table><thead><tr><th width="252">Parameter</th><th width="111">Type</th><th>Definition</th></tr></thead><tbody><tr><td><code>userIdentifier</code></td><td>String</td><td><strong>Optional</strong>. The unique user ID for the current player. Only pass this value in if you are using your own custom login instead of Metafi Login.<br><br><strong>Please ensure that this userIdentifier matches the value returned by your verification endpoint.</strong></td></tr><tr><td><code>token</code></td><td>String</td><td>The encoded JWT token issued to the user.</td></tr><tr><td><code>callback?</code></td><td>Action&#x3C;dynamic></td><td>A callback function that will be executed once the user is successfully logged in. The callback function will return a data structure similar to that returned by the <a href="/pages/fwivMJ3ThYYlH2agBbAM">RetrieveUser</a> function</td></tr></tbody></table>

### Return Value

None

### JWT Verification Endpoint

{% hint style="info" %}
You can update your JWT verification URL on the Developer Portal under the Project Settings menu.
{% endhint %}

[Here is a sample repo](https://github.com/metafi-nft/test-service/blob/master/routes/jwt-handler.js) containing sample code for the JWT verification endpoint.

When the Login function is invoked, we will invoke the configured JWT verification URL with the token passed to us in the Login function in the body. The response must be the userIdentifier of the user to which you want the user's wallet to be linked to.

The endpoint must implement the following format:

<mark style="color:green;">`POST`</mark> `https://your-webhook-url/path`

#### Request Body

| Name  | Type   | Description                  |
| ----- | ------ | ---------------------------- |
| token | String | The JWT token to be verified |

{% tabs %}
{% tab title="200: OK The user identifier associated with the valid JWT token" %}

```javascript
{
    "userIdentifier": "userIdentifier"
}
```

{% endtab %}

{% tab title="401: Unauthorized For cases where the JWT is invalid" %}

```javascript
{
    "error": "error"
}
```

{% endtab %}
{% endtabs %}

## Example

{% code title="" lineNumbers="true" %}

```csharp
using System;
using Metafi.Unity;

public class GameManager : MonoBehaviour {
    public async void Login(string userIdentifier, string jwtToken) {
        await MetafiProvider.Instance.Login(
            userIdentifier, 
            jwtToken, 
            ((System.Action<dynamic>) (result => {
                Debug.Log("Login complete, result: " + result.ToString());
        })));  
    }
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.usemeta.fi/unity-sdk/sdk-reference/login.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
