# Login

## Description

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

## Details

### Signature

`async Login(userIdentifier, token, apiKey, secretKey)`

### Arguments

<table><thead><tr><th width="252">Parameter</th><th width="156">Type</th><th>Definition</th></tr></thead><tbody><tr><td><code>userIdentifier</code></td><td>String</td><td><p>The unique user ID for the current player. </p><p><br><strong>Please ensure that this userIdentifier matches the value returned by your verification endpoint.</strong></p></td></tr><tr><td><code>token</code></td><td>String</td><td>The encoded Auth token issued to the user.</td></tr><tr><td><code>apiKey</code></td><td>String</td><td>The API Key generated from the <a href="https://developer-test.usemeta.fi/">Developer Portal</a></td></tr><tr><td><code>secretKey</code></td><td>String</td><td>The Secret Key generated from the <a href="https://developer-test.usemeta.fi/">Developer Portal</a></td></tr></tbody></table>

### Return Value

Returns user information object (same as the one returned by [RetrieveUser](https://docs.usemeta.fi/js-sdk/sdk-reference/retrieveuser))

```json
{
    "statusCode": 1/0,
    "data": {
        "userIdentifier": "userIdentifier", // the userIdentifier returned from your JWT verification endpoint
        "chainWallets": [
            "eth": [
                "address" // list of addresses for each chain
            ],
            ...
        ], // list of wallet addresses for each chain
        "assets": [
            "eth_eth": { // assetKey
                "totalBalance": "totalBalance", // total balance in BigNumber, across all user accounts
                "accountBalances": [
                    {
                        "address": "address", // wallet address
                        "balance": "balance" // balance in BigNumber
                    },
                ]
            },
            ...
        ], // list of assets owned by user
        "collectibles": [
            {
                /****** Metafi Asset fields *******/
                "symbol": "",
                "chain": {
                    /* Metafi chain Object */
                },
                "decimals": 0,
                "isNative": false,
                "contractAddress": "contractAddress",
                "assetKey": "",
                "className": "Asset",
                "tokenStandard": {
                    "name": "erc1155"
                },
                /****** NFT specific fields *******/
                "name": "name", // name extracted from NFT metadata
                "image": "imageURL", // image URL extracted from NFT metadata
                "description": "description", // description extracted from NFT metadata
                "tokenUri": "tokenUri", // fetched from smart contract
                "metadata": { // raw NFT metadata extracted from token URI
                    "name": "name",
                    "description": "description",
                    "image": "imageURL",
                },
                "tokenId": "tokenId" // fetched from smart contract
              },
            ...
        ],
    }
    "error": "error" // error if any
}
```

### JWT Verification Endpoint

{% hint style="info" %}
You can update your Auth Token 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 Auth Token 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<mark style="color:red;">\*</mark> | 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" %}

```jsx
import { Login } from '@metafi/metafi-js-package';
import { getLoginToken } from './controllers/login'

function App() {

    const handleLogin = async () => {
        var email = "test@mail.com";
        var jwtToken = getLoginToken(email);  // your logic to login user
        
        var result = await Login(
            email,
            jwtToken,
            "YOUR-API-KEY",
            "YOUR-SECRET-KEY"
        );
        
        console.log("Login results", result);
    }
    
    return (
        <div>
	    <button onClick={handleLogin}>Handle login</button>
        </div>
    );
}

export default App;

```

{% endcode %}
