This project is a simple REST API built with Golang, using JWT (JSON Web Token) for authentication and Gorilla Mux as the HTTP router.
- 🔒 User Authentication (Login & Register) with JWT
- 🍪 JWT Token stored in Cookies for secure access
- 📦 Dummy Products API for demonstration purposes
- 🛡️ JWT Middleware to protect certain routes
-
Clone the repository:
git clone https://github.com/thoriqdharmawan/go-jwt-mux.git cd go-jwt-mux
-
Install the required dependencies:
go mod tidy
-
Set up your database connection in the
models
package. -
Run the project:
go run main.go
Method | Endpoint | Description | Auth Required? |
---|---|---|---|
POST | /login |
Login user and generate JWT | ❌ |
POST | /register |
Register new user | ❌ |
GET | /logout |
Logout user | ❌ |
GET | /api/products |
Get dummy product data | ✅ |
The /api/products
route is protected by the JWT middleware, ensuring that only authenticated users can access it.
The productscontroller
currently serves static, dummy product data for demonstration purposes. This can be replaced with real data from a database in the future.
- Register: Create a new user by sending a
POST
request to/register
with ausername
andpassword
. - Login: After registering, login via
/login
. This will return a JWT token in a cookie. - Access Protected Routes: Use the JWT token to access protected routes such as
/api/products
. - Logout: Clear the token by hitting
/logout
.
- Golang 🐹
- Gorilla Mux 🦍
- JWT 🔑
- GORM 🗄️
- Integrate with a real product database.
- Improve error handling.
- Add more user roles and permissions.