This Go API demonstrates JWT token validation for Better-Auth integration. It provides a secure backend that can validate JWT tokens issued by the Better-Auth frontend.
- JWT token validation using JWKS (JSON Web Key Sets)
- CORS support for frontend integration
- Request logging middleware
- Health check endpoint
- Protected authentication verification endpoint
- Go 1.24.2 or higher
- Better-Auth frontend application running (for JWT token generation)
- Clone the repository and navigate to the API directory:
cd api- Install dependencies:
go mod downloadStart the server:
go run main.goThe API will start on port 8080 by default.
GET /health- Health check endpointGET /api/auth/verify- Verify JWT token (requires valid Authorization header)
The API uses the following environment variables (optional):
PORT- Server port (default: 8080)
Run tests:
go test ./...- The API fetches public keys from the Better-Auth JWKS endpoint
- When a request comes in with a JWT token, it validates the token using the cached public keys
- Valid tokens allow access to protected endpoints and return user information
- Invalid or expired tokens are rejected with appropriate error messages