Skip to content

Latest commit

 

History

History

gin

Folders and files

README.md

examples/gin: full auth API with Gin

A runnable HTTP server that wires AuthCore into Gin. Same routes as the Fiber example, different framework.

This example is its own Go module (separate go.mod) so the root library has no runtime dependency on Gin. Run it from this directory.

Run

cd examples/gin
go run .

Server listens on :3000.

Routes

Method Path Auth Purpose
POST /register none Hash + store the new user's password
POST /login none Verify password, issue an access + refresh pair
GET /me Authorization: Bearer <access> Verify the access token, return claims
POST /refresh refresh token in body Rotate the pair, replace the stored hash

Try it

# 1. Register
curl -X POST localhost:3000/register \
  -H 'content-type: application/json' \
  -d '{"email":"ana@example.com","password":"Str0ng-P@ssword!"}'

# 2. Login: capture the tokens
curl -X POST localhost:3000/login \
  -H 'content-type: application/json' \
  -d '{"email":"ana@example.com","password":"Str0ng-P@ssword!"}'

# 3. Protected route
curl localhost:3000/me -H 'authorization: Bearer <access-token>'

# 4. Rotate
curl -X POST localhost:3000/refresh \
  -H 'content-type: application/json' \
  -d '{"refresh_token":"<refresh-token>"}'

What it shows

  • Provider passed to multiple modules once at startup.
  • pwdMod.Hash / pwdMod.Verify for registration + login.
  • jwtMod.CreateTokens with typed UserClaims.
  • jwtMod.VerifyAccessToken in a Gin middleware for /me.
  • jwtMod.HashRefreshToken → DB lookup → jwtMod.VerifyRefreshTokenHash → jwtMod.RotateTokens: the full anti-reuse rotation pattern.

User storage is an in-memory map for brevity. Swap it for your real database.