Setting up a custom backend
Set up an custom login screen for visitors to your docs.
Last updated
Was this helpful?
Set up an custom login screen for visitors to your docs.
Last updated
Was this helpful?
This guide takes your through setting up a protected sign-in screen for your docs. Before going through this guide, make sure you’ve first gone through “Enabling visitor authentication”.
This guide walks you through setting up a protected sign-in screen for your GitBook documentation site using your own custom authentication backend.
To setup a custom authentication system for your GitBook site, follow these key steps:
Implement a backend that prompts users to login and authenticate them.
Create a JWT token and sign it with your site’s private key.
Configure a URL to be used when an unauthenticated visitor access your site.
Configure your backend to handle authentication across multiple GitBook sites.
Configure your backend to work with adaptive content in GitBook.
In order to start authenticating users before they can visit your documentation, you’ll need to set up a server that can handle login and authentication of users.
Your backend should:
Prompt users to log in using your preferred authentication method.
Validate user credentials and authenticate them.
Generate and sign a JSON Web Token (JWT) upon successful authentication.
Redirect users to GitBook with the JWT included in the URL.
Once your backend authenticates a user, it must generate a JWT and pass it to GitBook when redirecting them to your site. The token should be signed using the private key provided in your site's audience settings after .
The following example should demonstrate how a login request handler in your custom backend could look like:
The fallback URL is used when an unauthenticated visitor tries to access your protected site. GitBook will then redirect them to this URL.
This URL should point to a handler in your custom backend, where you can prompt them to login, authenticate and then redirect them back to your site with the JWT included in the URL.
For instance, if your login screen is located at https://example.com/login
, you should include this value as the fallback URL.
You can configure this fallback URL within your site's audience settings under the “Visitor authentication” tab.
When redirecting to the fallback URL, GitBook includes a location
query parameter to the fallback URL that you can leverage in your handler to redirect the user to the original location of the user:
If you're using GitBook as a platform to provide content to your different customers, you probably need to set up multi-tenant visitor authentication. Your authentication backend needs to be responsible for handling authentication across multiple different sites. This is possible in GitBook with a few small tweaks to your custom authentication backend code.
Your authentication backend will need to know the JWT signing keys and the URLs of all the GitBook sites you expect it to handle. If you have two sites in your organization for Customer A and Customer B, you can imagine your authentication code storing such mapping:
When GitBook is unable to authenticate a user's request, it redirects them to the fallback URL. This URL points to your authentication backend, which is responsible for authenticating the user and redirecting them back to the requested content.
To support multiple tenants, your authentication backend needs to know which GitBook site the user is meant to access. This information can be passed in the fallback URL.
So for example, you could setup the fallback URLs for each sites as follow:
Customer A site
https://auth-backend.acme.org/login?site=customer-a
Customer B site
https://auth-backend.acme.org/login?site=customer-b
Your authentication backend can then check this information and handle the redirection to the correct site accordingly:
To leverage the Adaptive Content capability in your visitor authentication setup, you can include additional user attributes (claims) in the payload of the JWT that your custom backend generates and include in the URL when redirecting the user to the site.
These claims when included in the JWT are used by GitBook to adapt content dynamically for your site visitors.
Putting it all together, the following code example demonstrates how you could include these claims in the JWT, which can then be used by GitBook to adapt content for your visitors:
After setting up and configuring the right claims to send to GitBook, head to “Adapting your content” to continue configuring your site.
This feature is still under development and coming soon.
Please sign up for the waitlist at