JWT authentication service written in Go.
# Build and run
go build -o go-jwt ./cmd/api/
./go-jwt| Variable | Default | Description |
|---|---|---|
PORT |
8080 |
HTTP listen port |
DATABASE_URL |
postgres://postgres:postgres@localhost:5432/gojwt?sslmode=disable |
PostgreSQL connection string |
REDIS_ADDR |
localhost:6379 |
Redis address |
REDIS_PASSWORD |
— | Redis password (optional) |
REDIS_DB |
0 |
Redis database number |
JWT_SECRET |
required | HMAC-SHA256 signing key |
ACCESS_TOKEN_TTL |
15m |
Access token lifetime |
REFRESH_TOKEN_TTL |
168h |
Refresh token / session lifetime |
BCRYPT_COST |
12 |
Bcrypt hashing cost (4–31) |
LOG_LEVEL |
info |
Log level (debug, info, warn, error) |
LOG_FORMAT |
json |
Log format (json, text) |
SHUTDOWN_TIMEOUT |
10s |
Graceful shutdown deadline |
Configure via .env file or environment variables.
curl -X POST http://localhost:8080/api/v1/signup \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"secret123"}'
# 201 {"message":"user created"}curl -X POST http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"secret123"}'
# 200 {"access_token":"...","refresh_token":"...","session_id":"..."}
# Sets refresh_token cookiecurl http://localhost:8080/api/v1/validate \
-H "Authorization: Bearer <access_token>"
# 200 {"message":"success","user":{"id":1,"email":"user@example.com"}}curl -X POST http://localhost:8080/api/v1/refresh \
-H "Content-Type: application/json" \
-d '{"session_id":"<session_id>","refresh_token":"<refresh_token>"}'
# 200 {"access_token":"...","refresh_token":"...","session_id":"..."}
# Old refresh token is invalidated (rotation)curl -X POST http://localhost:8080/api/v1/logout \
-H "Authorization: Bearer <access_token>" \
-H "Content-Type: application/json" \
-d '{"session_id":"<session_id>"}'
# 200 {"message":"logged out"}curl http://localhost:8080/health
# 200 {"status":"ok"}| Concern | Library |
|---|---|
| HTTP framework | gin-gonic/gin |
| ORM | gorm.io/gorm |
| JWT | golang-jwt/jwt/v5 |
| Password hashing | golang.org/x/crypto/bcrypt |
| Redis client | redis/go-redis/v9 |
| Config | spf13/viper |
| CLI | spf13/cobra |
| Logging | uber-go/zap |
| DI / lifecycle | uber-go/fx |