# Cookie

{% hint style="info" %}
ガイドを確認して、 [完全な手順](/docs/guides/docs-personalization-and-authentication/setting-up-adaptive-content.md) Cookie を使ったアダプティブコンテンツの設定方法を確認してください。
{% endhint %}

{% hint style="warning" %}
機能フラグとともにアダプティブコンテンツを使用するには、アプリケーションにコードを追加する必要があります。

この方法が機能するのは、サイトが次の下で配信されている場合のみです [カスタムドメイン](/docs/documentation/ja-gitbook-documentation/docs-site/custom-domain.md).
{% endhint %}

訪問者のブラウザ Cookie を通じて、訪問者データをドキュメントに渡すことができます。以下に各方法の概要を示します。

<table data-full-width="false"><thead><tr><th width="335.125">方法</th><th width="266.6015625">用途</th><th width="206.58984375">設定の容易さ</th><th width="202">セキュリティ</th><th>形式</th></tr></thead><tbody><tr><td>署名付き Cookie <code>gitbook-visitor-token</code></td><td>API テスト用認証情報、顧客識別</td><td>署名とカスタムドメインが必要</td><td><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span> プロパティはバックエンドでのみ定義できます</td><td>JWT</td></tr><tr><td>公開 Cookie <code>gitbook-visitor-public</code></td><td>機能フラグ、ロール</td><td>設定が簡単</td><td><span data-gb-custom-inline data-tag="emoji" data-code="274c">❌</span> 訪問者はプロパティを上書きできます</td><td>JSON</td></tr></tbody></table>

### 公開 Cookie

公開 Cookie から GitBook にデータを渡すには、公開 `gitbook-visitor-public` Cookie を設定して、アプリケーションからデータを送信する必要があります。

以下は簡単な JavaScript の例です:

```javascript
import Cookies from 'js-cookie';

const cookieData = {
  isLoggedIn: true,
  isBetaUser: false,
};

Cookies.set('gitbook-visitor-public', JSON.stringify(cookieData), {
  secure: true,
  domain: '*.acme.org',
})
```

{% hint style="warning" %}
公開 Cookie 経由で渡されるデータは、訪問者スキーマ内で次の方法を使って定義する必要があります [署名なしの](https://gitbook.com/docs/publishing-documentation/adaptive-content/enabling-adaptive-content#setting-unsigned-claims) オブジェクト。
{% endhint %}

### 署名付き Cookie

より安全に GitBook にデータを渡すには、データを次の形式で送信する必要があります [JSON Web Token](https://jwt.io/introduction) を、アプリケーションから次の名前の Cookie に入れて `gitbook-visitor-token` 自分のドメインに紐付けます。

これを設定するには、アプリケーションのログインフローを次の手順を含むように調整する必要があります:

{% stepper %}
{% step %}
**ユーザーがアプリケーションにログインしたときに JWT を生成する**

ユーザーが製品にログインするたびに、認証済みユーザー情報の選択した属性を含む JWT を生成します。
{% endstep %}

{% step %}
**サイトの訪問者署名キーを使用して JWT に署名する**

次に、サイトの **訪問者署名キー**を使って JWT に署名してください。これは、Adaptive Content を有効にした後、サイトの audience 設定で確認できます。
{% endstep %}

{% step %}
**JWT をワイルドカードのセッション Cookie に保存する**

最後に、ユーザー情報を含む署名済み JWT をワイルドカードのセッション Cookie に保存する必要があります **製品ドメインの下に**.

たとえば、アプリケーションが次の下で配信されている場合 `app.acme.org` ドメイン、Cookie は次の下で作成する必要があります `.acme.org` ワイルドカードドメイン。
{% endstep %}
{% endstepper %}

以下は簡単な TypeScript の例です:

```typescript
import * as jose from 'jose';

import { Request, Response } from 'express';

import { getUserInfo } from '../services/user-info-service';
import { getFeatureFlags } from '../services/feature-flags-service';

const GITBOOK_VISITOR_SIGNING_KEY = process.env.GITBOOK_VISITOR_SIGNING_KEY;
const GITBOOK_VISITOR_COOKIE_NAME = 'gitbook-visitor-token';


export async function handleAppLoginRequest(req: Request, res: Response) {
   // ログインリクエストを処理するためのビジネスロジック
   // たとえば、認証情報を確認してユーザーを認証する
   //
   // 例:
   // const loggedInUser = await authenticateUser(req.body.username, req.body.password);

   // ユーザーを認証した後、渡したいユーザー情報を取得します
   // GitBook に渡すために、データベースまたはユーザーサービスから取得します。
   const userInfo = await getUserInfo(loggedInUser.id);
      
   // ユーザー情報を使って JWT ペイロードを作成します
   const gitbookVisitorClaims = {
       firstName: userInfo.firstName,
       lastName: userInfo.lastName,
       isBetaUser: userInfo.isBetaUser
       products: userInfo.products.map((product) => product.name),
       featureFlags: await getFeatureFlags({userId: loggedInUser.id})
   }
   
   // クレームを使って署名済み JWT を生成します
   const gitbookVisitorJWT = await new jose.SignJWT(gitbookVisitorClaims)
     .setProtectedHeader({ alg: 'HS256' })
     .setIssuedAt()
     .setExpirationTime('2h') // 任意の 2 時間の有効期限
     .sign(GITBOOK_VISITOR_SIGNING_KEY);
     
  // エンコードされた JWT を含む `gitbook-visitor-token` Cookie を
  // ログインハンドラーのレスポンスに含めます
  res.cookie(GITBOOK_VISITOR_COOKIE_NAME, gitbookVisitorJWT, {
     httpOnly: true,
     secure: process.env.NODE_ENV === 'production',
     maxAge: 2 * 60 * 60 * 1000, // 任意の 2 時間の有効期限
     domain: '.acme.org' //
  });
  
  // ユーザーをアプリにリダイレクトする処理を含むログインハンドラーの残りのロジック
  res.redirect('/'); // リダイレクトの例
}
```


---

# 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://gitbook.com/docs/documentation/ja-gitbook-documentation/saitoakusesu/adaptive-content/enabling-adaptive-content/cookies.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.
