Integrate Fauna with Clerk
You will learn the following:
- Configure Fauna to accept JWTs from Clerk
- Create a JWT template in Clerk to generate Fauna JWTs
- Use the Fauna JWTs to authenticate Fauna queries in your Next.js app
Integrating Fauna with Clerk gives you the benefits of using a Fauna database while leveraging Clerk's authentication, prebuilt components, and webhooks.
This guide will walk you through the steps to integrate Fauna with Clerk in your Next.js app.
Add the following keys to your .env.local
file. These keys can always be retrieved from the API keys page in the Clerk Dashboard.
- In the Clerk Dashboard, navigate to the API keys page.
- In the navigation sidebar, select Show API URLs.
- Copy the Frontend API URL.
- In the navigation sidebar, select Show JWT public key.
- Copy the JWKS URL.
- Paste your keys into your
.env.local
file.
The final result should resemble the following:
Configure Fauna
- Navigate to the Fauna Dashboard and select your database.
- Select the Access Providers tab and select Create Access Provider.
- Fill out the form:
- Enter a Name for the access provider, such as Clerk.
- Copy and save the Audience URL. You will need this later.
- In Issuer, paste the Clerk Frontend API URL you copied in the previous step. Do not include a trailing slash (
/
). - In JWKS Endpoint, paste the Clerk JWKS URL you copied in the previous step.
- Select Create.
- Select the access provider that you just created.
- Update the access provider's FSL schema to include a user-defined role. For example:
- Select Save.
Create a JWT template in Clerk
Clerk's JWT templates allow you to generate a new valid Fauna authentication token (JWT) for each signed in user. These tokens allow authenticated users to access your data with Fauna's API.
To create a JWT template for Fauna:
- In the Clerk Dashboard, navigate to the JWT templates page.
- Select the New template button, then select Fauna from the list of options.
- Configure your template:
- The value of the Name field will be required when using the template in your code. For this tutorial, name it
fauna
. - You can leave all other fields as their default settings or customize them to your needs. See the JWT template guide to learn more about these settings.
- In the Claims section, set the
aud
claim to the Audience URL you copied from Fauna in Step 2. The URL format should behttps://db.fauna.com/db/<YOUR_FAUNA_DB_ID>
. You can include additional claims if you’d like, butaud
is the only required one. Shortcodes are available to make adding dynamic user values easy. - Select Save from the notification bubble to complete setup.
- The value of the Name field will be required when using the template in your code. For this tutorial, name it
Install the Fauna library
Add the Fauna library to your project.
Authenticate Fauna queries in your Next.js app
You can now create Fauna JWTs in Clerk using the JWT template you created in the previous step. Generate the Fauna JWT by calling Clerk's useAuth().getToken
method, and use it to authenticate with Fauna as an end user, as shown in the following example:
Feedback
Last updated on