A Sample Project using Gin Framework that implements an auth-based system using JWT Token with OTP authentication and user management features.
- OTP-based registration and login
- JWT token authentication
- Rate limiting (3 OTP requests per phone number within 10 minutes)
- User management with pagination and search
- MongoDB database integration
- Swagger API documentation
POST /auth/signup- Register user with phone number (generates OTP)POST /auth/signup/confirm-otp- Confirm OTP and get JWT token
GET /user/- Get current user detailsGET /user/{userId}- Get user by IDGET /user/all- Get all users with pagination and phone search- Query parameters:
page,pageSize,phone
- Query parameters:
go run main.gogo buildswag initOnce the application is running, access the Swagger documentation at:
http://localhost:3000/swagger/index.html
For detailed API examples and usage, see API_EXAMPLES.md
- Docker and Docker Compose installed on your system
- Clone the repository
git clone <your-repo-url>
cd go-auth- Set up environment file
cp .env.docker .env
# Edit .env file if needed- Build and run with Docker Compose
docker-compose up -d- Access the application
- API: http://localhost:3000
- Swagger Documentation: http://localhost:3000/swagger/index.html
- MongoDB: localhost:27017
# Build Docker images
make docker-build
# Start services
make docker-run
# Stop services
make docker-stop
# View logs
make docker-logs
# Clean up (removes containers and volumes)
make docker-clean
# Restart services
make docker-restart
# Rebuild and restart
make docker-rebuild# Build images
docker-compose build
# Start services in background
docker-compose up -d
# Start services with logs
docker-compose up
# Stop services
docker-compose down
# Stop services and remove volumes
docker-compose down -v
# View logs
docker-compose logs -f
# View logs for specific service
docker-compose logs -f go-authThe Docker setup includes:
- go-auth: The main Go application (Port: 3000)
- mongodb: MongoDB database (Port: 27017)
The application uses these environment variables:
| Variable | Default | Description |
|---|---|---|
| MONGO_URI | mongodb://mongodb:27017 | MongoDB connection URI (internal) |
| MONGO_USERNAME | root | MongoDB username |
| MONGO_PASSWORD | my-secret-pw | MongoDB password |
| MONGO_DB_NAME | go-auth | Database name |
| JWT_SECRET_KEY | my_very_secret_key | JWT signing secret |
| GIN_MODE | release | Gin framework mode |
| PORT | 3000 | Server port |
# Using Make
make mongo-shell
# Using Docker Compose
docker-compose exec mongodb mongosh -u root -p my-secret-pw --authenticationDatabase admin go-auth# Backup
make mongo-backup
# Restore
make mongo-restore# Using Make
make docker-shell
# Using Docker Compose
docker-compose exec go-auth /bin/sh# All services
make docker-logs
# Specific service
docker-compose logs -f go-auth
docker-compose logs -f mongodb# Check if services are running
make docker-health
# Manual health check
curl http://localhost:3000/swagger/index.html-
Port already in use
- Change ports in docker-compose.yml
- Stop conflicting services
-
MongoDB connection issues
- Ensure MongoDB container is healthy:
docker-compose ps - Check logs:
docker-compose logs mongodb
- Ensure MongoDB container is healthy:
-
Build failures
- Clean Docker cache:
docker system prune -f - Rebuild:
make docker-rebuild
- Clean Docker cache:
For production deployment, update the environment variables in docker-compose.yml:
environment:
MONGO_URI: mongodb://your-mongo-host:27017
MONGO_USERNAME: your-username
MONGO_PASSWORD: your-secure-password
MONGO_DB_NAME: go-auth-prod
JWT_SECRET_KEY: your-very-secure-jwt-secret
GIN_MODE: release