A secure authentication service built with Go and Gin framework, providing JWT-based authentication and user management.
- User registration and authentication
- JWT-based authentication
- Protected routes
- Password reset functionality
- Email notifications
- PostgreSQL database integration
- Postman collection for API testing
- Go 1.25 or higher
- SMTP server credentials for email functionality
- Environment variables configured
- Clone the repository:
git clone https://github.com/0xKimutai/secure-auth-service.git
cd secure-auth-service- Install dependencies:
go mod download- Configure environment variables:
cp .env.example .envEdit the .env file with your configuration:
PORT=8080
JWT_SECRET=your-secret-key
JWT_EXPIRY=24h
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
[email protected]
SMTP_PASSWORD=your-app-password
FRONTEND_URL=http://localhost:3000
POST /api/v1/register- Register a new userPOST /api/v1/login- Authenticate user and receive JWT tokenPOST /api/v1/reset-password- Request password reset
GET /api/v1/user- Get user profilePUT /api/v1/user- Update user profileDELETE /api/v1/user- Delete user account
- Start the server:
go run main.go- The server will start on the configured port (default: 8080)
-
Import the collection:
- Open Postman
- Click "Import"
- Select
auth-system.postman_collection.jsonfrom the project root
-
Create an environment:
- Click "Environments" -> "New"
- Name it (e.g., "Local Auth System")
- The collection automatically sets:
base_url:http://localhost:8080jwt_token: (set automatically after login)
- Click "Save"
-
Register a New User
- Use "Register User" request
- Provides example JSON body for registration
- Returns user profile on success
-
Login
- Use "Login" request with registered credentials
- Automatically saves JWT token to environment
- Returns token and user profile
-
Protected Routes (requires login first)
- Get Profile: Fetch user details
- Update Profile: Modify user information
- Delete Account: Remove user
-
Password Reset
- Request Reset: Sends reset token via email
- Confirm Reset: Set new password using token
-
Public Endpoints (no auth required)
POST /api/v1/register- Create accountPOST /api/v1/login- Get JWT tokenPOST /api/v1/reset-password- Request resetPOST /api/v1/reset-password/confirm- Reset password
-
Protected Endpoints (JWT required)
GET /api/v1/users/me- Get profilePUT /api/v1/users/me- Update profileDELETE /api/v1/users/me- Delete account
-
Automatic Token Handling
- Login success saves JWT token
- Protected routes use token automatically
- Token included in Authorization header
curl -X POST http://localhost:8080/api/v1/register \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "securepassword123",
"first_name": "John",
"last_name": "Doe"
}'curl -X POST http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "securepassword123"
}'curl -X GET http://localhost:8080/api/v1/users/me \
-H "Authorization: Bearer your-jwt-token"curl -X PUT http://localhost:8080/api/v1/users/me \
-H "Authorization: Bearer your-jwt-token" \
-H "Content-Type: application/json" \
-d '{
"first_name": "Updated",
"last_name": "Name"
}'.
├── config/
│ └── config.go # Configuration management
├── controllers/
│ └── auth_controller.go # Authentication handlers
├── middleware/
│ └── auth_middleware.go # JWT authentication middleware
├── models/
│ └── user.go # User model and DTOs
├── routes/
│ └── routes.go # Route definitions
├── utils/
│ ├── email.go # Email utilities
│ ├── jwt.go # JWT utilities
│ └── password.go # Password hashing utilities
├── .env # Environment variables
├── go.mod # Go module file
├── go.sum # Go module checksum
├── main.go # Application entry point
└── README.md # This file
- Password hashing using bcrypt
- JWT-based authentication
- Secure password reset mechanism
- Email verification support
- Request validation and sanitization
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request