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 (or Identity Provider, IdP): The service that authenticates the user and grants the OAuth tokens. In this example, Clerk would be the authorization service or IdP.
- Resource service (or Service Provider, SP): 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.
Create a Clerk OAuth application
To 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. Learn more about the scopes currently supported by Clerk.
- Select Add. A modal will open with your Client Secret. For security reasons, Clerk does not store your Client Secret and cannot show it to you again, so ensure that you copy it and store it somewhere secure, like an environment variable.
- You'll be redirected to your app's settings page. Under Application credentials, save the Client ID somewhere secure as you'll need it later.
- 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. This guide uses
http://localhost:3000/oauth_callback
as an example.
To learn more about the other OAuth application configuration settings, like Dynamic client registration, see the dedicated guide.
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
and client_secret
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 return 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