---
title: "@xid-kit/electron"
description: "メインプロセス PKCE フロー、contextBridge プリロード、OS キーチェーントークンストレージ、ループバックまたはカスタムスキームコールバック戦略を備えた Electron SDK。"
locale: "ja"
---

> Documentation Index
> Fetch the locale documentation index at: https://xid.dev/ja/llms.txt
> Use this file to discover all available pages before exploring further.

# @xid-kit/electron

## 状態

パッケージステータス：**現行パッケージ**。メインプロセスアプリ、contextBridge プリロード、レンダラー側ブリッジが実装済みです。本番インフラでの実際の IdP ラウンドトリップはまだ手動検証待ちです。

## エントリーポイント

| エントリー | 目的 |
| --- | --- |
| `@xid-kit/electron` | デフォルトエクスポート：レンダラーサーフェスと型 |
| `@xid-kit/electron/main` | メインプロセス専用：XidElectronApp |
| `@xid-kit/electron/renderer` | レンダラープロセス：getXidBridge、XidClient |
| `@xid-kit/electron/preload` | window.xidBridge を公開する既製のプリロードスクリプト |

## メインプロセスセットアップ

```ts
// main.ts
import { app, ipcMain } from 'electron'
import { XidElectronApp } from '@xid-kit/electron/main'

const xidApp = new XidElectronApp({
  issuer: 'https://xid.dev',
  clientId: 'client_abc123',
  // callbackStrategy: 'loopback' (default, RFC 8252 s.7.3) | 'custom-scheme'
})

app.whenReady().then(async () => {
  await xidApp.init(ipcMain)
  const win = new BrowserWindow({
webPreferences: {
  contextIsolation: true,
  preload: path.join(__dirname, 'preload.js'),
},
  })
  win.on('closed', () => xidApp.dispose(ipcMain))
})
```

## プリロードスクリプト

```ts
// preload.ts
import '@xid-kit/electron/preload'
// Exposes window.xidBridge with storage, signIn, signOut,
// getAccessToken, getSession, setTokenStorage
```

## レンダラープロセス

```ts
import { getXidBridge } from '@xid-kit/electron/renderer'

const bridge = getXidBridge()

// Opens system browser, waits for loopback callback, exchanges code.
const accessToken = await bridge.signIn()

// Get current access token (transparently refreshes if near expiry).
const token = await bridge.getAccessToken() // null when not signed in

// Get full session (accessToken + expiresAt in epoch seconds).
const session = await bridge.getSession()

// Sign out and clear tokens.
await bridge.signOut()
```

## カスタムスキーム（ループバックの代替）

```ts
// main.ts
import { app } from 'electron'
import { XidElectronApp } from '@xid-kit/electron/main'

app.setAsDefaultProtocolClient('myapp')

const xidApp = new XidElectronApp({
  issuer: 'https://xid.dev',
  clientId: 'client_abc123',
  callbackStrategy: 'custom-scheme',
  customScheme: 'myapp',  // redirect_uri = myapp://callback
})

xidApp.registerDeepLinkHandler(app)
```

## トークンストレージ

- トークンは `safeStorage.encryptString()`（OS キーチェーン）で暗号化され、デフォルトで `app.getPath('userData')/xid-tokens/` にバイナリファイルとして保存されます。
- `safeStorage.isEncryptionAvailable()` が `false`（キーリングのないヘッドレス Linux）を返す場合、`setItem()` は平文を黙って書き込む代わりにコード `encryption_unavailable` の `ElectronStorageError` をスローします。
- `XidElectronMainOptions` の `storageDir` でストレージディレクトリを上書きします。

## 共有ネイティブコントラクト

| 方式 | 説明 |
| --- | --- |
| `signIn(options?)` | システムブラウザを開き、コードを交換してトークンを保存します |
| `signOut()` | ローカルトークンをクリアします |
| `getAccessToken()` | 現在のトークンを返します（期限切れ間近なら更新）。サインアウト中は null |
| `getSession()` | accessToken と expiresAt を返します。なければ null |

Source: https://xid.dev/ja/sdks/electron/index.mdx
