Encore Middleware With JWT

n Encore, services communicate within a network primarily through gRPC. To enhance security in this process, we’ll integrate JWT (JSON Web Token) as middleware. Here’s how to set it up in your Encore project:
Begin by installing the JWT package for Go. You can find it at github.com/golang-jwt/jwt/v5
. This package will handle JWT creation and validation.
In your Encore project, create a new directory named pkg/jwk
. This will organize your JWT-related code.
Inside the pkg/jwk
directory, create a file named jwt.go
. This file will contain all the necessary functions for handling JWTs.
In the secrect can use a Secrets of encore for hide you phrase, or use a env.
Implement a function to create new tokens. This function should accept a user interface, which allows you to use any user data structure for token generation. For the secret key, consider using Encore’s Secrets feature to secure your key, or alternatively, store it in an environment variable.

Develop a method to validate tokens. This is crucial for ensuring that incoming requests to your service have valid, unexpired tokens.

Finally, add a method to parse the token. This method retrieves token data (like user information) that you’ll use in your service.

Implement a login endpoint in your application that generates a new token. This endpoint will handle user authentication by verifying user credentials stored in the database.

In an Encore application, it’s essential to have an authentication handler, commonly referred to as AuthHandler
. This handler is automatically invoked for endpoints that require authentication, as indicated in their descriptions. Here's how you can implement it.

Now when a route use auth you can get user date.

Throughout this article, we delved into the integration of JWT (JSON Web Token) as middleware in the Encore framework, a strategic choice for bolstering security in service communications within a network predominantly using gRPC. The process involved key steps, from installing the JWT package for Go to implementing specific functions for creating, validating, and parsing JWT tokens.
Structuring the pkg/jwk
directory and creating the jwt.go
file within the Encore project efficiently organized the JWT-related code. The flexibility in token generation was ensured by allowing the use of various user data structures, while the security of the secret key was safeguarded by using Encore's Secrets feature or environment variables.
The development of the login endpoint emerged as a critical component, managing user authentication and new token generation based on the verification of credentials stored in the database. Furthermore, the implementation of the AuthHandler, an authentication handler, proved vital for applying authentication consistently and automatically to endpoints requiring it.
In summary, integrating JWT as middleware in Encore offers a robust and flexible approach to authentication and security in services. This strategy not only simplifies the management of authentication in applications but also ensures that sensitive data and critical operations are protected against unauthorized access. Thus, developers and organizations can benefit from a more secure, efficient, and reliable communication system.
Rerefences:
https://encore.dev/docs/develop/auth