Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thoriqdharmawan/go-jwt-mux
REST API project built with Golang, featuring JWT authentication, Gorilla Mux routing
https://github.com/thoriqdharmawan/go-jwt-mux
golang gorilla jwt mux
Last synced: about 1 month ago
JSON representation
REST API project built with Golang, featuring JWT authentication, Gorilla Mux routing
- Host: GitHub
- URL: https://github.com/thoriqdharmawan/go-jwt-mux
- Owner: thoriqdharmawan
- Created: 2024-09-17T08:20:01.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2024-09-17T10:15:57.000Z (3 months ago)
- Last Synced: 2024-09-17T12:57:12.067Z (3 months ago)
- Topics: golang, gorilla, jwt, mux
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# 🛡️ Golang JWT Authentication with Gorilla Mux
This project is a simple REST API built with Golang, using JWT (JSON Web Token) for authentication and Gorilla Mux as the HTTP router.
## 🚀 Features
- 🔒 **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## 🔧 Installation
1. Clone the repository:
```bash
git clone https://github.com/thoriqdharmawan/go-jwt-mux.git
cd go-jwt-mux
```2. Install the required dependencies:
```bash
go mod tidy
```3. Set up your database connection in the `models` package.
4. Run the project:
```bash
go run main.go
```## 🛠️ Endpoints
| 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 | ✅ |## 🛡️ JWT Middleware
The `/api/products` route is protected by the JWT middleware, ensuring that only authenticated users can access it.
## 📦 Dummy Product Data
The `productscontroller` currently serves static, dummy product data for demonstration purposes. This can be replaced with real data from a database in the future.
## 🔐 Authentication Flow
1. **Register**: Create a new user by sending a `POST` request to `/register` with a `username` and `password`.
2. **Login**: After registering, login via `/login`. This will return a JWT token in a cookie.
3. **Access Protected Routes**: Use the JWT token to access protected routes such as `/api/products`.
4. **Logout**: Clear the token by hitting `/logout`.## 🔑 Technologies Used
- **Golang** 🐹
- **Gorilla Mux** 🦍
- **JWT** 🔑
- **GORM** 🗄️## ✨ Future Improvements
- Integrate with a real product database.
- Improve error handling.
- Add more user roles and permissions.