Use OAuth for scoped access
Clerk's OAuth implementation supports OAuth Scoped Access, which lets third-party applications request limited access to specific parts of a user's data through Clerk's API. This enables applications to interact with user data in a secure and controlled manner, based on the user’s explicit consent. The most common implementation of OAuth Scoped Access is the "Authorization Code Flow", which is the flow covered in this guide.
How the flow works
Before diving into implementations details, it's important to understand the different actors involved in this flow:
- Client - The third-party app that wants to access a user's data from your application (resource service). This app is configured to obtain OAuth tokens from Clerk.
- Authorization service - The service that authorizes the user and grants the OAuth tokens. In this example, Clerk would be the authorization service.
- Resource service - The backend server that hosts the user's data that the client wants to access. In this example, this would be your own application that uses Clerk for authentication and route protection.
This guide provides step-by-step instructions on how to build OAuth scoped access into a Clerk app.
Before you start
- You need to create a Clerk account or sign into the Clerk Dashboard.
- You need to create a Clerk Application in the Clerk Dashboard. For more information, see the setup guide.
Let's begin by creating an OAuth application in the Clerk Dashboard.
Create a Clerk OAuth application
-
In the Clerk Dashboard, navigate to the OAuth Applications page.
-
Select Add OAuth application. A modal will open.
-
Complete the following fields:
-
Name
- Helps you identify your application. -
Scopes
- The scopes that you would like to leverage. To find out more about the scopes currently supported by Clerk, check out the OAuth reference documentation.
-
-
Select Add. A modal will open with your Client Secret. Ensure that you copy this and store it somewhere secure.
-
You'll be redirected to your OAuth app's settings page. Under Application credentials, you can find your Client ID, which will be necessary to complete the OAuth flow.
-
In the Redirect URIs field, add the redirect URI provided by your client. This is the URL that Clerk will redirect to with an authorization code after the user has authenticated and consented to the access request. For this example, we will use
http://localhost:3000/oauth_callback
.
The Clerk setup is now complete. If you'd like to know more about other OAuth application configuration settings, refer to the dedicated reference documentation.
Test the OAuth flow
If you are aiming to integrate your Clerk app with a third-party app that acts as an OAuth client, you can reference their documentation on how to integrate OAuth connections in order to test your OAuth flow. It will likely be necessary to provide the client_id
, client_secret
, and redirect_uri
from the OAuth app created above.
If you don't have a specific, existing OAuth client in mind, you can use the following OAuth test client project. It contains a minimal Express.js server that can run through an OAuth flow. Let's walk through how to use this test client below.
-
Clone the following repository to your machine.
-
In your terminal and within your project, run the following command:
terminal cp .env.sample .env
-
In your newly created
.env
file, add the required credentials:CLERK_PUBLISHABLE_KEY
: Your Clerk Publishable Key from the API keys page in the Clerk Dashboard.CLIENT_ID
: The client ID of your OAuth app from the Clerk Dashboard.CLIENT_SECRET
: The client secret of your OAuth app from the Clerk Dashboard.
-
In your terminal, run the following command to install dependencies:
terminal npm i
-
In your terminal, run the following command to start the server:
terminal npm start
Finished 🎉
Once this is complete, you'll be able to see the OAuth flow begin. If you are not currently signed in, you'll first be presented with a sign in screen. After signing in, Clerk will redirect to an OAuth consent screen, showing the requested scopes and asking for your consent.
Once you have accepted, you'll be redirected to the OAuth callback route, the redirect_uri
specified earlier. This process will exchange the authorization code for an access token, and render the access token and refresh token as a JSON response, similar to this:
{
"access_token": "xxx",
"expires_in": 7200,
"id_token": "xxx",
"refresh_token": "xxx",
"scope": "openid email profile",
"token_type": "bearer"
}
With the OAuth flow complete, the client now has a working access token. The next step would normally be for the client to use that token to make an authenticated request to your app's API endpoints. To learn more about how to accept and verify OAuth tokens using Clerk, see the guide on verifying OAuth tokens.
Feedback
Last updated on