This project demonstrates the implementation of authentication and authorization in a Golang application.
- User Authentication: Secure login system using JWT (JSON Web Tokens).
- Authorization: Role-based access control for different user types (Admin, User).
- Password Hashing: Implements bcrypt for secure password storage.
- REST API: Exposes APIs for user login and restricted access to protected routes.
- Golang installed (Go 1.15 or higher)
- A running database (PostgreSQL, MySQL, etc.)
-
Clone the repository:
git clone https://github.com/JawherKl/auth-golang cd auth-golang
-
Install dependencies:
go mod tidy
-
Configure the environment variables:
JWT_SECRET
: The secret key for signing JWTs.DATABASE_URL
: Connection string for the database.
-
Run the project:
go run main.go
For the organization of our server, we will categorize the directories into the following sections:
- routes - This directory will contain the definitions of the different routes for the server, which map URLs to specific functions.
- controllers - This directory will hold the functions that will handle the incoming requests and interact with the models and other components.
- models - This directory will store the data models that represent the objects in the application and their relationships.
- middlewares - This directory will contain any middleware functions that process requests and responses, such as authentication or logging.
- utils - This directory will house various utility functions that are used throughout the application, such as helper functions or custom data types.
These directories will also serve as packages.
- POST /login: Authenticate a user and generate a JWT token.
- POST /signup: Create a user and inserted to database.
- GET /home: Protected route that returns home profile information (requires valid JWT).
- GET /premium: Protected route that returns premium information (requires valid JWT).
- GET /logout: Protected route that can be logout session user.
This project is open-source and available under the MIT License.