Fiber with JWT
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
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git
cd recipes/jwt -
Install dependencies:
go mod tidy
Running the Application
-
Run the application:
go run main.go
-
The server will start on
http://localhost:3000
.
Endpoints
Method | URL | Description |
---|---|---|
POST | /login | Authenticates a user and returns a JWT |
GET | /restricted | Accesses 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.