# session()

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

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

此客户端正在使用的会话。 它首先读取缓存, 只在缓存无法说时才询问服务器 。

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

## 返回值 {#returns}

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

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `id` | string |  | 什麽 [撤销( )](/zh/sdk/ref/auth/revoke-session) 取用。 |
| `storageId` | string|null |  | 服务器将这个进程存储为哪个 。 对于机器来说, 机器在重新启动时会保持一个身份 。 |

## 如何回答 {#order}

| 缓存状态 | 发生什么 |
| --- | --- |
| 选择了会话代号 | 立即返回——没有网络 |
| 标记已缓存, 但无代号 | 列出活动会话,并匹配同一存储的会话 |
| 列名失败 | 无效——失败被吞吞,没有被抛 |
| 什么都没有缓存 | 空无一物,无所交换 以找出 |

第二行是为什么这种方法比它看从旧缓存中恢复的客户端慢。第一行是正常情况。

> **它不会创造一会,它不会抛弃,**
>
> 冷酷客户的回答是无效的。 任何普通的电话先打—— 获得用户( GetUser) 是最便宜的 — 交换会在那里发生。 因为它吞噬了自己的失败, 一个无名氏从未告诉你网络已经倒闭; 它告诉你方法不能命名会话。

```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 }
```

## 用途 {#use}

- 显示一个进程以什么方式运行, 在一个仪表板上, 或者在启动时用一个日志行 。
- 将ID传给 [撤销( )](/zh/sdk/ref/auth/revoke-session)包括结束这届会议 从别的地方。
- 阅读 `storageId` 以确认服务器是否承认此机器为您预期的机器, 而不是注册第二个机器 。

### 启动一线

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

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

### 确认一台机器, 数量不多

机器发送 `storageId` 当部署在每次重新启动时失去会话缓存时,每个靴子都登记一个新的——它显示成一堆几乎相同的储存。

```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.
```

> **集装箱,每个启动一次没有持久性会话路径登记册**
>
> 挂载会话路径, 或者通过您控制的存储。 否则每次重新启动都是服务器的新机器, 以及您所依赖的您下面的更改的身份 。

## 届会不连续举行的 {#worker}

在没有耐久储存的运行过程中,会议会活在记忆中,只要孤立状态能够——所以 `session()` 答案是这个孤立, 答案是没有别的。 这是商店的属性, 不是这个方法, 而是你提供的商店 改变了它 。

## 结束 {#ending}

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

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

注意不对称性: 此方法为您提供 *您的* id. 另一台机器的ID不是 SDK 列表中 - - 当你想要机器关闭时, 撤销它持有的密钥 。

- [会话](/zh/sdk/concepts/sessions): 交换 刷新 和什么被隐藏。
- [会话存储](/zh/sdk/concepts/session-storage): 藏匿处居住的地方 运行时间 以及如何替换
- [revokeSession()](/zh/sdk/ref/auth/revoke-session): 由 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
```
