# session()

Canonical URL: https://developers.iagenify.com/pt/sdk/ref/iagenify/session/
Language: pt

`api.session() → Promise<{ id, storageId } | null>`

Quale sessione questo client sta using. Lee prima la cache, e chiede al server solo quando la cache non sa dire.

Status: cache first · never throws · null before the first exchange

## Retorno {#returns}

```javascript
{
  id: "…",          // the session id
  storageId: "…"    // the storage this session is bound to, or null
}
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `id` | string |  | - Ce? [Revore Session()](/pt/sdk/ref/auth/revoke-session) e il |
| `storageId` | string|null |  | Quale stoccatura il server riconosce questo processo come. Per una máquina, é así que mantiene una identitä a travers reinizios. |

## Como risponde {#order}

| Stat de cache | Ce succeda |
| --- | --- |
| Un id di sessione è cached | retornò d'immediat — senza rete |
| Un gèn è cached, ma no id | le sessioni attive sono listate, e la unita al medès stoccage è igual |
| Que la lista failes | null — il fallimento è inghiottito, non gettato |
| - Nada è cached | null, e nulla è scambiato per scopri-lo |

La seconda fila è il motivo per cui questo metodo può essere più lento di quanto pare su un client restaurat da un cache ancestrale. La prima fila è il cas normal.

> **Non crea una sessione, e non lança mai**
>
> Su un client fria risponde null. Fai prima una chiamata ordinaria — getUser() è la meno barata — e il cambio avviene là. Perché inghiote il suo proprio fallimento, un null mai dice que la rete è abatida; dice que il metodo non puèt nomire una sessione.

```javascript
const fresh = new IaGenify({ apiKey });

await fresh.session();      // null — nothing has been exchanged
await fresh.getUser();      // this is what exchanges
await fresh.session();      // { id, storageId }
```

## Finalidade {#use}

- Mostrando quale sessione un processo è in esecuzione come — in un dashboard, o in una línea de log al bott.
- Passando l'id a [revokeSession()](/pt/sdk/ref/auth/revoke-session), incluso per terminare questa sessione da un altro lugar.
- Lectura `storageId` per confermare il server ha riconosciuto questa macchina come quella che si aspettava, invece di registrare un secondo.

### Una linea a bott

```javascript
const [user, session] = await Promise.all([api.getUser(), api.session()]);

console.log(
  `iagenify · ${user?.email} · session ${session?.id ?? "none"} · storage ${session?.storageId ?? "none"}`,
);
```

### Confirmando una macchina, non tante

Una macchina mantiene la stessa identitât con l'invio di suo `storageId` di nuovo sul proximo canvà. Quando un implementamento perde la cache di sessione in ogni reinizio, ogni boot registra un nuovo — e che appare come un mucchio di stoccajs quasi-identica.

```javascript
const before = (await api.session())?.storageId;

// … restart the process, with the same sessionPath …

const after = (await api.session())?.storageId;
// Same value: one machine. A different one: the session cache did not survive.
```

> **Un container senza seduzione persistente registra una volta per botto**
>
> Montare la via di sessione, o passare un storage che controli. Altrimenti, ogni reinizio è una macchina nova al server, e l'identitât che tu eras basando su cambis sotto tu.

## Quando non persistere sessioni {#worker}

In un tempo di runtime senza stoccatura duratura la sessione vive in memoria, per il tempo in cui l'isolat — so `session()` risposte per questo isolat e nò più. Quest è una proprietà del negozio, non di questo metodo, e un negozio che fornir lo cambia.

## Fine {#ending}

```javascript
const current = await api.session();

await api.auth.logout();              // this one
await api.auth.revokeSession(id);     // another, by id
```

Nota l'assimetria: questo metodo ti da *tu* Id. Un'altra macchina id non è qualcosa SDK listas — quando si desidera una máquina shut out, revocare la chave che detiene.

- [Sessões](/pt/sdk/concepts/sessions): L'intercambio, la refrescare, e il chen in cache.
- [Armazenamento de sessões](/pt/sdk/concepts/session-storage): Onde vive la cache, per runtime, e come la sostituiu.
- [revokeSession()](/pt/sdk/ref/auth/revoke-session): Terminando una sessione per id.

```python
{
    "id": "…",          # the session id
    "storageId": "…",   # the storage this session is bound to, or None
}
```

```python
fresh = IaGenify(api_key=api_key)

fresh.session()     # None — nothing has been exchanged
fresh.get_user()    # this is what exchanges
fresh.session()     # {"id", "storageId"}
```

```python
user, session = api.get_user(), api.session()

print(
    f"iagenify · {(user or {}).get('email')} · "
    f"session {(session or {}).get('id', 'none')} · storage {(session or {}).get('storageId') or 'none'}"
)
```

```python
before = (api.session() or {}).get("storageId")

# … restart the process, with the same session_path …

after = (api.session() or {}).get("storageId")
# Same value: one machine. A different one: the session cache did not survive.
```

```python
current = api.session()

api.auth.logout()                    # this one
api.auth.revoke_session(session_id)  # another, by id
```
