Encore is an open source backend framework for TypeScript with built-in infrastructure automation and observability.
This tutorial outlines how to integrate Clerk's authentication into your Encore backend application, allowing you to manage user authentication and sessions securely. This guide uses TypeScript and Encore's built-in auth handler pattern.
Encore has a built-in auth handler pattern for protecting endpoints. In the auth folder, create a handler.ts file and copy and paste the following code. This is the auth handler that verifies Clerk session tokens using the verifyToken() function and makes authenticated user data available to your endpoints:
Warning
For optimal performance and to avoid rate limiting, it's recommended to use the useUser() hook on the client-side when possible. Only use getUser() when you specifically need user data in a server context.
This endpoint gets the authenticated user's profile information. The auth: true option protects the endpoint, allowing only authenticated requests to access it. The getAuthData() function retrieves the authenticated user's data from the auth handler. Learn more about authentication in Encore documentation.
Open the local development dashboard at http://localhost:9400 in your browser to view traces for all requests, including auth handler execution.
If you want to add more to your Clerk + Encore integration, such as database integration, webhooks, or organizations, check out this advanced Clerk + Encore tutorial guide.