A simple OTP-based authentication REST API built with Golang, Gin, MongoDB, JWT, and Docker.
- Send OTP to user phone number (simulated via console log)
- Verify OTP and generate JWT token
- Store users and OTP requests in MongoDB
- REST API documented with Swagger UI
- Dockerized for easy deployment with MongoDB
- Go 1.21+
- Docker and Docker Compose (optional)
- MongoDB (if not using Docker)
- Set environment variables in
.envfile:
PORT=8080
MONGO_URI=mongodb://localhost:27017/otp_auth
JWT_SECRET=your_jwt_secret_key
OTP_EXPIRATION_MINUTES=5
-
Run the API server:
go run cmd/main.go -
Access Swagger UI at:
http://localhost:8080/swagger/index.html
- Build and start services:
docker-compose up --build - The API will be available at:
http://localhost:8080 - Swagger UI at:
http://localhost:8080/swagger/index.html
- POST /send-otp - Send OTP to phone number
- POST /verify-otp - Verify OTP and receive JWT token
curl -X POST http://localhost:8080/send-otp \
-H "Content-Type: application/json" \
-d '{"phone":"09123456789"}'