This project implements a JWT authentication flow in Golang, featuring both access and refresh tokens. The following APIs have been implemented:
GET /api/auth/greet
- Greet endpointPOST /api/auth/users
- Create a new userPOST /api/auth/sessions
- Login user
GET /api/auth/users/me
- Get current user informationPOST /api/auth/logout
- Logout userDELETE /api/auth/users
- Delete userPOST /api/auth/tokens/refresh
- Refresh access token
- JWT verification and authentication
- Request logging
- Claims parsing
A Postman collection is included in the repository to help you test the APIs.
golang-jwt-authentication.postman_collection.json
- Go 1.16+
- A
.env
file with the necessary environment variables
-
Clone the repository:
git clone https://github.com/the-arcade-01/golang-jwt-authentication.git cd golang-jwt-authentication
-
Install dependencies:
go mod tidy
-
Create a
.env
file in the root directory and add the required environment variables:ENV=development WEB_URL=http://localhost:5173 JWT_SECRET_KEY=<secret> DB_DRIVER=mysql DB_URL=<user>:<password>@tcp(<mysql_container_name>:3306)/<db_name>?parseTime=true DB_MAX_IDLE_CONN=10 DB_MAX_OPEN_CONN=10 DB_MAX_CONN_TIME_SEC=180 MYSQL_ROOT_PASSWORD=<password> MYSQL_DATABASE=<db_name> HTTP_COOKIE_HTTPONLY=false HTTP_COOKIE_SECURE=false HTTP_REFRESH_TOKEN_EXPIRE=720 HTTP_ACCESS_TOKEN_EXPIRE=15
- Run the db using docker compose in the
scripts
foldercd scripts docker compose --env-file ../../.env up
- Start the server by running:
go run cmd/main.go
ORmake run