This project provides a comprehensive authentication and authorization system built with Go, adhering to SOLID principles and Clean Architecture. It's designed to be a secure and scalable foundation for managing user identities and access control in your applications.
The auth-golang project is a Go-based backend service focused on handling user authentication and authorization. It implements a layered architecture following Clean Architecture principles, ensuring maintainability, testability, and adherence to software design best practices.
This system offers a robust set of features for managing user authentication and authorization:
✅ User Registration: Securely register new users with email and password.✅ Login: Authenticate users using their email and password.✅ JWT Tokens: Issue short-lived access tokens and long-lived refresh tokens for secure session management.✅ Token Refresh: Seamlessly refresh access tokens using valid refresh tokens.✅ Logout: Basic logout functionality (token blacklisting could be added for production).✅ Role-Based Access Control (RBAC): Assign roles to users for group-level permissions.✅ Permission-Based Authorization: Define fine-grained permissions for specific actions on resources.✅ Authentication Middleware: Protect API endpoints requiring a valid access token.✅ Permission Middleware: Enforce access control based on user roles and permissions.✅ View User Profile: Retrieve detailed user information.✅ Update Profile: Allow users to update their first and last names.✅ Change Password: Securely update a user's password.✅ Role Management (Admin): Admins can assign and remove roles from users.godotenv (Environment variable loading)go.mod)The project follows a Clean Architecture approach, organizing code into logical layers:
auth-system/
├── cmd/server/ # Application entry point
├── internal/
│ ├── config/ # Configuration management (env loading)
│ ├── domain/ # Core business entities & interfaces (repositories, services)
│ │ ├── entities/ # Data models (User, Role, Permission)
│ │ ├── repositories/ # Repository interfaces
│ │ └── services/ # Service interfaces
│ ├── application/ # Application logic (use cases, DTOs, concrete services)
│ │ ├── dto/ # Data Transfer Objects for API requests/responses
│ │ └── services/ # Concrete service implementations
│ ├── infrastructure/ # External dependencies and implementations
│ │ ├── database/ # Database connection and migration logic
│ │ └── security/ # Security implementations (JWT, Password hashing)
│ └── interfaces/ # Presentation layer (HTTP handlers, middleware, routes)
│ ├── handlers/ # HTTP request handlers
│ ├── middleware/ # Request middleware (Auth, CORS, Permissions)
│ └── routes/ # API route definitions
├── pkg/ # Reusable utility packages (errors, response helpers)
└── .env.example # Environment variable template
Follow these steps to set up the project locally:
Clone the repository:
git clone https://github.com/phambaophuc/auth-golang.git
cd auth-golang
Install Go dependencies:
go mod download
Configure Environment Variables:
cp .env.example .env
.env file and update the following settings:DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME, DB_SSL_MODE.JWT_SECRET, JWT_ACCESS_TTL, JWT_REFRESH_TTL (ensure JWT_SECRET is strong and unique for production).SERVER_PORT (defaults to 8080).Set up PostgreSQL Database:
.env file (e.g., auth_db).# Example using psql command line
createdb auth_db
Once dependencies are installed and the environment is configured, start the server:
go run cmd/server/main.go
The application will start and listen on the configured SERVER_PORT (default is 8080).
This authentication and authorization system is suitable for:
Authorization header with the Bearer token.curlHere are examples of how to interact with the API using curl:
curl -X POST http://localhost:8080/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "password123",
"first_name": "Test",
"last_name": "User"
}'
curl -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "password123"
}'
curl -X GET http://localhost:8080/api/v1/user/profile \
-H "Authorization: Bearer <your_access_token>"
This project is built with SOLID principles in mind:
This project is licensed under the MIT License - see the LICENSE.md file for details.
auth-golang | phambaophuc/auth-golang
If you find this project useful, please consider giving it a star ⭐, forking it 🍴, or opening an issue to report bugs or suggest improvements.