Authentication
Embed GitBook Assistant on sites that require authentication by dynamically loading the script after users sign in
Last updated
Was this helpful?
Embed GitBook Assistant on sites that require authentication by dynamically loading the script after users sign in
Last updated
Was this helpful?
Was this helpful?
If your GitBook documentation requires authentication (e.g., visitor authentication via OIDC, Auth0, or a custom backend), the embedded Assistant cannot access your docs content unless the user's authentication token is available.
When a user signs in to your authenticated docs, GitBook stores a visitor token in their browser cookies under the key gitbook-visitor-token. The Assistant needs this token to fetch content from your docs.
The recommended flow:
User signs in to your docs site
GitBook stores the visitor token in browser cookies
Your app checks for the token
If the token exists, load the Assistant
If the token doesn't exist, prompt the user to sign in
Use this snippet to load the Assistant only after a user has signed in:
Important: Replace docs.example.com with your actual docs site URL.
If the token is missing, you can prompt users to sign in:
For React apps, conditionally render the Assistant based on token presence:
When using the NPM package, check for the token before initializing:
Loading the Assistant before sign-in β Always check for the token before loading the script or components.
Token not persisting across domains β Cookies don't persist across different domains due to browser security policies. Your app and docs must be on the same domain or subdomain.
Token expired β Tokens can expire. If the Assistant returns authentication errors, prompt users to sign in again.
Using wrong cookie name β The token is stored as gitbook-visitor-token, not gitbook-token or other variations.
To verify the token is present, open your browser console and run:
If this returns undefined, the user hasn't signed in to your docs yet.
Customizing the Assistant β Add welcome messages and buttons
Creating custom tools β Integrate with your product APIs
<script>
(function () {
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length === 2) return parts.pop().split(";").shift();
}
if (!getCookie("gitbook-visitor-token")) {
// Redirect to docs or show a message
alert("Please sign in to your docs to access help.");
window.location.href = "https://docs.example.com";
return;
}
// Load the Assistant
var script = document.createElement("script");
script.src = "https://docs.example.com/~gitbook/embed/script.js";
script.async = true;
script.onload = function () {
window.GitBook("show");
};
document.head.appendChild(script);
})();
</script>import { useEffect, useState } from "react";
import { GitBookProvider, GitBookAssistantFrame } from "@gitbook/embed/react";
function App() {
const [hasToken, setHasToken] = useState(false);
useEffect(() => {
// Check for token in cookies
const getCookie = (name) => {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(";").shift();
};
const token = getCookie("gitbook-visitor-token");
setHasToken(!!token);
}, []);
if (!hasToken) {
return (
<div>
<p>Please sign in to access help.</p>
<a href="https://docs.example.com">Sign in</a>
</div>
);
}
return (
<GitBookProvider siteURL="https://docs.example.com">
<YourAppContent />
<GitBookAssistantFrame />
</GitBookProvider>
);
}import { createGitBook } from "@gitbook/embed";
function initializeAssistant() {
// Check for token in cookies
const getCookie = (name) => {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(";").shift();
};
if (!getCookie("gitbook-visitor-token")) {
console.warn("[GitBook Assistant] User must sign in first.");
return null;
}
const gitbook = createGitBook({
siteURL: "https://docs.example.com",
});
const iframe = document.createElement("iframe");
iframe.src = gitbook.getFrameURL();
const frame = gitbook.createFrame(iframe);
document.getElementById("assistant-container").appendChild(iframe);
return frame;
}
initializeAssistant();document.cookie.split(";").find((c) => c.includes("gitbook-visitor-token"));<script>
(function () {
// Check for the visitor token in cookies
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split