Skip to content
isdmxPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-jwt

JWT authentication service written in Go.

Quick start

# Build and run
go build -o go-jwt ./cmd/api/
./go-jwt

Environment variables

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.

API

Sign Up

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"}

Login

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 cookie

Validate token

curl http://localhost:8080/api/v1/validate \
  -H "Authorization: Bearer <access_token>"
# 200 {"message":"success","user":{"id":1,"email":"user@example.com"}}

Refresh tokens

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)

Logout

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"}

Health

curl http://localhost:8080/health
# 200 {"status":"ok"}

Architecture

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages