recipes/auth-jwt

Auth + JWT

Simple JWT authentication.

Auth JWT Example

Github StackBlitz

This example demonstrates a boilerplate setup for a Go Fiber application that uses JWT for authentication.

Description

This project provides a starting point for building a web application with user authentication using JWT. It leverages Fiber for the web framework and GORM for ORM.

Requirements

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/auth-jwt
  2. Set the environment variables in a .env file (see .env.sample):

    DB_HOST=localhost
    DB_PORT=5432
    DB_USER=example_user
    DB_PASSWORD=example_password
    DB_NAME=example_db
    DB_SSLMODE=disable
    SECRET=example_secret
    ACCESS_TOKEN_TTL_MINUTES=15

    Note: DB_SSLMODE controls the PostgreSQL SSL connection mode (disable, require, verify-full, etc.). Set to require or verify-full in production.

  3. Install the dependencies:

    go mod download
  4. Run the application:

    go run main.go

The API should now be running on http://localhost:3000.

Running PostgreSQL with Docker

If you do not have a PostgreSQL server available, start one in a container. The values below match the example .env in the setup section, so adjust them together with your .env file:

docker run --name auth-jwt-db -d \
  -e POSTGRES_USER=example_user \
  -e POSTGRES_PASSWORD=example_password \
  -e POSTGRES_DB=example_db \
  -p 5432:5432 \
  postgres:18-alpine

The container's data is lost when you remove it. Mount a volume at /var/lib/postgresql (-v auth-jwt-data:/var/lib/postgresql) to keep it. Postgres 18 keeps its data in a versioned subdirectory of that path, and a mount at /var/lib/postgresql/data makes the container exit.

Database Management

You can manage the database via psql with the following command:

psql -U <DB_USER> -d <DB_NAME> -h localhost -p <DB_PORT>

Replace <DB_USER>, <DB_NAME>, and <DB_PORT> with the values from your .env file.

API Endpoints

The following endpoints are available in the API:

  • POST /api/auth/register: Register a new user.
  • POST /api/auth/login: Authenticate a user and return a JWT.
  • POST /api/auth/logout: Logout a user and revoke their JWT.
  • POST /api/auth/refresh-token: Refreshes a user token and returns a JWT. The body is {"refresh_token": "..."}. A refresh token can be used once: the response holds a new one, and the old one is revoked in the same database transaction. When several requests present the same token at the same time, exactly one succeeds and the others get 401.
  • GET /api/users/:id: Get a user (requires a valid JWT).
  • PATCH /api/users/:id: Update a user (requires a valid JWT).
  • DELETE /api/users/:id: Delete a user (requires a valid JWT).
  • GET /api/products: Get all products.
  • GET /api/products/:id: Get a product.
  • POST /api/products: Create a new product (requires a valid JWT).
  • DELETE /api/products/:id: Delete a product (requires a valid JWT).

Example Usage

  1. Register a new user:

    curl -X POST http://localhost:3000/api/auth/register -d '{"username":"testuser", "password":"testpassword", "email":"test@example.com"}' -H "Content-Type: application/json"
  2. Login to get a JWT:

    curl -X POST http://localhost:3000/api/auth/login -d '{"email":"test@example.com", "password":"testpassword"}' -H "Content-Type: application/json"
  3. Access a protected route:

    curl -H "Authorization: Bearer <JWT>" http://localhost:3000/api/users/1

Replace <JWT> with the token received from the login endpoint.

Conclusion

This example provides a basic setup for a Go Fiber application with JWT authentication. It can be extended and customized further to fit the needs of more complex applications.

References