Skip to main content

Fiber with JWT

Github StackBlitz

This example demonstrates how to use JSON Web Tokens (JWT) for authentication in a Fiber application.

Prerequisites

  • Go 1.16 or higher
  • Go modules

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/jwt
  2. Install dependencies:

    go mod tidy

Running the Application

  1. Run the application:

    go run main.go
  2. The server will start on http://localhost:3000.

Endpoints

MethodURLDescription
POST/loginAuthenticates a user and returns a JWT
GET/restrictedAccesses a restricted route with JWT

Example Requests

Login

curl -X POST http://localhost:3000/login -d '{"username": "user", "password": "pass"}' -H "Content-Type: application/json"

Access Restricted Route

curl -X GET http://localhost:3000/restricted -H "Authorization: Bearer <your_jwt_token>"

Postman Collection

You can find Postman examples here.