This project demonstrates how to implement JWT (JSON Web Token) authentication in a Golang web application using Gin framework.
- User signup: Users can register by providing a username, email, and password.
- User login: Registered users can log in using their email and password.
- JWT Authentication: JWT is used to generate and authenticate user tokens for secure access to protected routes.
- Go (v1.16 or later)
- PostgreSQL (v9.5 or later)
-
Clone the repository:
git clone https://github.com/CABON-TECH/jwt-auth-in-golang.git
-
Navigate to the project directory:
cd jwt-auth-in-golang
-
Install dependencies:
go mod tidy
-
Set up the PostgreSQL database:
- Create a PostgreSQL database named
jwt_db
. - Update the database connection details in
initializers/connectDB.go
file.
- Create a PostgreSQL database named
-
Set environment variables:
Create a
.env
file in the root directory and define the following environment variables:SECRET=your-secret-key
-
Run the application:
go run main.go
-
Access the application in your web browser at
http://localhost:8080
.
-
URL:
/signup
-
Method: POST
-
Body:
{ "username": "Cabon TEch", "email": "cabontech@gmail.com", "password": "cabon123" }
-
URL:
/login
-
Method: POST
-
Body:
{ "email": "cabontech@gmail.com", "password": "cabon123" }
This project is licensed under the MIT License - see the LICENSE file for details.