This is a simple JWT (JSON Web Token) authentication system built using Golang. The project structure follows the MVC architecture with controllers, models, middleware, and initializers for better organization.
- JWT Authentication: Secure login with JWT.
- Controllers: Handles user authentication (login, signup) and protected routes.
- Middleware: JWT-based authorization for protecting endpoints.
- Environment Configurations: Uses
.env
file for managing secrets. - Golang Modules: Clean module management using
go.mod
andgo.sum
.
├── controllers # Handlers for authentication routes
├── initializers # Initialization files for database and environment variables
├── middleware # Middleware to protect routes with JWT
├── models # Database models for the application
├── .env # Environment variables (DO NOT share this file publicly)
├── .gitignore # Ignore unnecessary files for git tracking
├── Authorization.postman_collection.json # Postman collection for testing API
├── go.mod # Go module file
├── go.sum # Dependencies for the Go module
├── LICENSE # License for the project
├── main.go # Entry point of the application
└── README.md # Project documentation (this file)
- Go 1.19 or later installed on your machine.
- Postman or cURL to test the API endpoints.
- PostgreSQL
-
Clone the repository:
git clone https://github.com/shahriaarrr/goAuthSystem.git cd go-jwt-auth
-
Install dependencies:
go mod download
-
Create a
.env
file and add your JWT secret key and database credentials:PORT=3000 DB="host=localhost user={PUT_YOUR_USERNAME} password={PUT_YOUR_PASSWORD} dbname={PUT_YOUR_DB_NAME} port=5432 sslmode=disable" SECRET={YOUR_SECRET_KEY}
-
Run the application:
go run main.go
- Signup: Create a new user by sending a POST request to
/signup
withemail
andpassword
. - Login: Authenticate an existing user by sending a POST request to
/login
. - Protected Route: Access a protected route by sending a request to
/validate
with a valid JWT in the Authorization header.
The project includes a Postman collection file Authorization.postman_collection.json
which contains pre-configured requests for testing the API.
To use the collection:
- Open Postman.
- Import the collection by selecting the Authorization.postman_collection.json file from the project.
- You will see three endpoints: signup, login, and validate.
The following endpoints are available for the authentication system. You can use Postman or curl
to interact with the API.
Endpoint: POST /signup
Description: Register a new user by providing an email and password.
Request:
POST http://localhost:3000/signup
Content-Type: application/json
{
"email": "example@example.com",
"password": "yourpassword"
}
Response:
{
"message": "User created successfully"
}
Endpoint: POST /login
Description: Authenticate a user and receive a JWT token.
Request:
POST http://localhost:3000/login
Content-Type: application/json
{
"email": "example@example.com",
"password": "yourpassword"
}
Response:
{
"message": "welcome back :)"
}
Note: Upon successful login, a JWT token will be set in the response as an HTTP-only cookie.
Endpoint: GET /validate
Description: Validate a JWT token and access protected routes.
Request:
GET http://localhost:3000/validate
Authorization: Bearer your_jwt_token
Response:
{
"message": User_data
}
Feel free to open an issue or submit a pull request if you have suggestions for improving the project. Contributions are welcome!