An authentication service project using Golang and gin framework, with MongoDB as the database backend. The service provides the functionality for user registration(sign-up), login(sign-in), and token management through JWT.
auth-service/
├── api/
│ └── routes/ # Route handlers
├── db/ # Database configuration
├── internal/
│ ├── verify/ # Authentication middleware functions
│ ├── models/ # Data models
│ └── services/ # API service logic
├── utils/ # Utility functions
├── .env # Environment variables
├── docker-compose.yml # Docker compose config
└── Dockerfile # Docker config
The Authentication service can be run as a containerized application using Docker (recommended).
- Create a
.envfile in the root directory of the project similar to.env.example - Docker & Docker compose installed
- Go 1.21 or higher (if running locally)
- MongoDB (if running locally)
using Docker-compose:
docker-compose up --buildThis command builds and starts both the API service and MongoDB.
To stop the services, use:
docker-compose downThe service exposes RESTful endpoints for user authentication and token management. Here's how to interact with each endpoint using curl commands (formatted for Windows PowerShell):
curl -X POST http://localhost:8080/auth/signup -H "Content-Type: application/json" -d '{"email": "user1.test@example.com", "password": "password123"}'{
"id": "user_id",
"email": "test@example.com",
"created_at": "2025-01-01T00:00:01Z",
"updated_at": "2025-01-01T00:00:10Z"
}curl -X POST http://localhost:8080/auth/signin -H "Content-Type: application/json" -d '{"email": "user1.test@example.com", "password": "password123"}'{
"access_token": "eyJKngGc...",
"refresh_token": "eyKmvTo..."
}After signing in, you'll receive an access token and refresh token. Store the access token in a variable for subsequent requests:
$ACCESS_TOKEN="<received_access_token>"
$REFRESH_TOKEN="<refresh_token_here>"Protected Profile Access
curl -X GET http://localhost:8080/protected/profile -H "Authorization: Bearer $ACCESS_TOKEN"Token Revocation
curl -X POST http://localhost:8080/auth/revoke -H "Authorization: Bearer $ACCESS_TOKEN"Refresh expired token
curl -X POST http://localhost:8080/auth/refresh -H "X-Refresh-Token: $REFRESH_TOKEN"Test coverage for core functionalities, Test scripts are written for token utilities (token_test.go), authentication services (user_service_test.go), and API handlers (auth_handler_test.go). Run the full test using:
go test ./... -v