How to use Logto for your Encore application
Learn how to use Logto for user authentication in your Encore backend application. In this guide we show you how to integrate your Go backend with Logto.
Learn how to use Logto for user authentication in your Encore backend application. In this guide we show you how to integrate your Go backend with Logto.
Encore is a backend development platform that makes it easy to build production-ready APIs and microservices.
Logto is a modern Auth0 alternative that helps you build the sign-in experience and user identity within minutes. It's particularly well-suited for protecting API services built with Encore.
This guide will show you how to integrate Logto with your Encore application to implement secure user authentication and protect your API endpoints.
Before we begin integrating with Encore, you'll need to set up a few things in Logto:
Create an account at Logto Cloud if you don't have one yet.
Create an API Resource in Logto Console, this represents your Encore API service
https://api.encoreapp.com)

Now let's implement the authentication in your Encore application. We'll use Encore's built-in auth handler to validate Logto's JWT tokens.
Add these two modules in your Encore application:
Create auth/auth.go and add the following code:
And then, you can use this auth handler to protect your API endpoints:
We've completed our work in the Encore API service. Now we need to integrate Logto with our frontend application.
You can choose the framework you are using in the Logto Quick start page to integrate Logto with your frontend application. In this guide we use React as an example.
Check out the Add authentication to your React application guide to learn how to integrate Logto with your React application. In this example, you only need to complete up to the Integration section. After that, we'll demonstrate how the frontend application can obtain an access token from Logto to access the Encore API.
First, update your LogtoConfig by adding the API resource used in your Encore app to the resources field. This tells Logto that we will be requesting access tokens for this API resource (Encore API).
After updating the LogtoConfig, if a user is already signed in, they need to sign out and sign in again for the new LogtoConfig settings to take effect.
Once the user is logged in, you can use the getAccessToken method provided by the Logto React SDK to obtain an access token for accessing specific API resources. For example, to access the Encore API, we use https://api.encoreapp.com as the API resource identifier.
Then, add this access token to the request headers as the Authorization field in subsequent requests.
That's it, you've successfully integrated Logto with your Encore application.
If you want to use more Logto features in Encore, you can refer to the following links for more information:
scope information to your Access token (note that you need to sign in again after updating Logto config)