> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/ja-gitbook-documentation/publish/adaptive-content/enabling-adaptive-content/cookies.md).

# Cookie

公開 Cookie または署名付き 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/publish/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

GitBook に公開Cookieからデータを渡すには、アプリケーションから公開 `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を通じて渡されるデータは、訪問者スキーマ内で次の [署名なし](/docs/documentation/ja-gitbook-documentation/publish/adaptive-content/enabling-adaptive-content.md#set-an-unsigned-claim) オブジェクト。
{% endhint %}

### 署名付きCookie

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

これを設定するには、アプリケーションのログインフローを次の手順に合わせて調整する必要があります：

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

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

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

次に、サイトの **訪問者署名キー**を使ってJWTに署名してください。これは、Adaptive Contentを有効にした後、サイトのオーディエンス設定で確認できます。
{% 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/ja-gitbook-documentation/publish/adaptive-content/enabling-adaptive-content/cookies.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
