Route Handlers
Clerk provides helpers that allow you to protect your Route Handlers, fetch the current user, and interact with the Clerk backend API.
Protect your Route Handlers
If you aren't protecting your Route Handler using clerkMiddleware()
, you can protect your Route Handler in two ways:
- Use
auth().protect()
if you want Clerk to return a404
error when there is no signed in user. - Use
auth().userId
if you want to customize the behavior or error message.
Retrieve data from external sources
Clerk provides integrations with a number of popular databases.
The following example demonstrates how to use auth().getToken()
to retrieve a token from a JWT template and use it to fetch data from the external source.
Retrieve the current user
In some cases, you might need the current user in your Route Handler. Clerk provides an asynchronous helper called currentUser()
to retrieve the current Backend User
object.
Interact with Clerk's Backend API
The Clerk Backend SDK exposes Clerk's backend API resources and low-level authentication utilities for JavaScript environments.
clerkClient
exposes an instance of the Clerk Backend SDK for use in server environments.