A simple Go API that demonstrates JWT authentication using the Chi router and the jwt-go library for secure token handling.
- JWT Authentication: Protects API routes with JWT-based authentication.
- Middleware: Utilizes Chi middleware for easy route handling and token verification.
- Modular Code: Organized for readability and scalability.
- Go (version 1.16 or higher is recommended)
- Git
-
Clone the repository
git clone https://github.com/manimovassagh/jwt-go-chi.git cd jwt-go-chi
-
Install dependencies
go mod download
-
Run the server
go run main.go
Define the following environment variables in a .env
file or export them in your shell:
JWT_SECRET
: Secret key for signing JWT tokens.
Example .env
:
JWT_SECRET=your_secret_key
- Generate a Token: Access the
/login
endpoint with user credentials to receive a JWT token. - Access Protected Routes: Use the JWT token in the
Authorization
header asBearer <token>
to access protected routes like/protected
.
-
Login:
POST /login Content-Type: application/json { "username": "your_username", "password": "your_password" }
-
Access Protected Route:
GET /protected Authorization: Bearer <your_jwt_token>
To download this repository directly, you can use this command:
curl -L https://github.com/manimovassagh/jwt-go-chi/archive/refs/heads/main.zip -o jwt-go-chi.zip
Then unzip and navigate into the project directory:
unzip jwt-go-chi.zip
cd jwt-go-chi-main
This project is licensed under the MIT License. See the LICENSE file for details.
Happy coding!