This project provides a JWT authentication system using Golang, with a PostgreSQL backend. It includes user registration, login, logout, and token refresh functionality, as well as CRUD operations for user management.
- Go 1.23.2 or higher
- A PostgreSQL database
-
Clone the repository:
git clone <repository-url> cd golang-jwt-auth
-
Install dependencies:
go mod download
-
Database Setup:
- Create a database in your PostgreSQL server.
- Run the SQL schema file
schema.sqlto create the necessary tables and indices.
-
Environment Variables:
- Create a
.envfile in the root of the project. - Set the
DATABASE_URLandJWT_SECRETenvironment variables in the.envfile:DATABASE_URL=postgres://user_name:password@127.0.0.1:5432/database_name?sslmode=disable JWT_SECRET=my-top-secret-key - You can generate a secure secret key with the following command:
openssl rand -hex 64
- Create a
To start the server, run:
go run cmd/server/main.goThe server will start on port 8080.
- Create a separate PostgreSQL database for testing (e.g.,
ewallet_test). - Run the
schema.sqlfile to create the necessary tables. - Set the
TEST_DATABASE_URLenvironment variable:export DATABASE_URL=postgres://user_name:password@127.0.0.1:5432/ewallet_test?sslmode=disable
- Run the tests:
go test ./...
- Authenticated Routes: For endpoints that require authentication, you must include an
Authorizationheader in your request with a valid JWT access token:Authorization: Bearer <your_access_token> - Request/Response Format: All request and response bodies are in JSON format. Ensure your requests have the
Content-Type: application/jsonheader.
These endpoints handle user login, logout, and token management.
- Description: Authenticates a user and returns an access token and a refresh token.
- Method:
POST - Path:
/login - Authentication: Not required.
- Request Body:
{ "email": "user@example.com", "password": "your_password" } - Success Response (200 OK):
{ "access_token": "...", "refresh_token": "..." }
- Description: Generates a new access token using a valid refresh token.
- Method:
POST - Path:
/token/refresh - Authentication: Not required.
- Request Body:
{ "user_id": 1, "refresh_token": "your_refresh_token" } - Success Response (200 OK):
{ "access_token": "..." }
- Description: Invalidates the user's refresh token on the server. The client is responsible for deleting the tokens.
- Method:
POST - Path:
/logout - Authentication: Required.
- Request Body:
{ "user_id": 1 } - Success Response:
204 No Content
These endpoints handle CRUD operations for users.
- Description: Creates a new user account.
- Method:
POST - Path:
/users - Authentication: Not required.
- Request Body:
{ "first_name": "John", "last_name": "Doe", "phone_number": "1234567890", "email": "john.doe@example.com", "password": "a_strong_password", "status": "active" } - Success Response (201 Created): The newly created user object.
- Description: Retrieves a list of all users.
- Method:
GET - Path:
/users - Authentication: Required.
- Success Response (200 OK): An array of user objects.
- Description: Retrieves a single user by their ID.
- Method:
GET - Path:
/users/{id}(e.g.,/users/1) - Authentication: Required.
- Success Response (200 OK): A single user object.
- Description: Updates an existing user's information.
- Method:
PUT - Path:
/users/{id}(e.g.,/users/1) - Authentication: Required.
- Request Body:
{ "first_name": "John", "last_name": "Doe", "phone_number": "1234567890", "email": "john.doe@example.com", "status": "active" } - Success Response (200 OK): The updated user object.
- Description: Deletes a user by their ID.
- Method:
DELETE - Path:
/users/{id}(e.g.,/users/1) - Authentication: Required.
- Success Response:
204 No Content