Integrate InstantDB with Clerk
You will learn the following:
- Configure your Clerk session token to include the
email
claim. - Configure InstantDB to use your Clerk application.
- Integrate InstantDB into your Clerk application.
Integrating InstantDB with Clerk gives you the benefits of using an InstantDB database while leveraging Clerk's authentication, prebuilt components, and webhooks.
This guide will walk you through the steps to integrate InstantDB with Clerk in your Next.js app.
Configure your Clerk session token
InstantDB uses Clerk's session token to authenticate users. To use InstantDB with Clerk, you need to include the email
claim in your session token.
- Navigate to the Clerk Dashboard.
- In the navigation sidebar, select Sessions.
- In the Customize session token section, select Edit.
- Add the
email
claim to your session token:
You can have additional claims as long as the email
claim is set to {{user.primary_email_address}}
.
Get your Clerk publishable key
- Navigate to the Clerk Dashboard.
- In the navigation sidebar, select API Keys.
- In the Quick Copy section, copy your Clerk publishable key.
Configure InstantDB
- Navigate to the InstantDB dashboard.
- Select the Auth tab.
- Select Setup Clerk.
- Add the publishable key you copied in the previous step.
- Confirm the The session token has the "email" claim. message.
- Select Add Clerk app.
Install the InstantDB library
Add the InstantDB library to your project.
Integrate InstantDB to your Clerk application
To sign in to InstantDB with Clerk, you need to:
- Initialize InstantDB with your Clerk publishable key.
- Use Clerk's
getToken()
helper to get the session for the user that is signed in to Clerk. - Pass the session to InstantDB's
db.auth.signInWithIdToken()
method in order to sign in to InstantDB. When you calldb.auth.signInWithIdToken()
, InstantDB will verify that the session was signed by your Clerk app. If verified, InstantDB uses the email in the session's claims to sign in, look up your user, or create a new one.
To sign out of the application, call both InstantDB's db.auth.signOut()
method and Clerk's signOut()
method.
Feedback
Last updated on