ss-keel-jwt
ss-keel-jwt provides JWT generation and validation with a ready-to-use Guard implementation. Sign tokens at login, protect routes with the guard, and access the authenticated user anywhere in the handler chain.
Implements: Guard
Planned Installation
Section titled âPlanned Installationâgo get github.com/slice-soft/ss-keel-jwtPlanned Usage
Section titled âPlanned Usageâimport "github.com/slice-soft/ss-keel-jwt"
jwtService := ssjwt.New(ssjwt.Config{ Secret: os.Getenv("JWT_SECRET"), Expiration: 24 * time.Hour,})Generating a Token
Section titled âGenerating a Tokenâ// On logintoken, err := jwtService.Sign(ssjwt.Claims{ Subject: user.ID, Custom: map[string]any{ "role": user.Role, "email": user.Email, },})Protecting Routes
Section titled âProtecting Routesâguard := jwtService.Guard()
// Per-routecore.GET("/profile", profileHandler). Use(guard.Middleware()). WithSecured("bearerAuth")
// Per-groupprotected := app.Group("/api/v1", guard.Middleware())protected.Use(&users.Module{})Accessing the Authenticated User
Section titled âAccessing the Authenticated Userâfunc profileHandler(c *core.Ctx) error { claims, ok := core.UserAs[*ssjwt.Claims](c) if !ok { return core.Unauthorized("not authenticated") }
return c.OK(map[string]any{ "id": claims.Subject, "role": claims.Custom["role"], })}Refresh Tokens
Section titled âRefresh Tokensâ// Generate a refresh token with longer TTLrefreshToken, _ := jwtService.SignRefresh(ssjwt.Claims{ Subject: user.ID,})
// Validate and rotatenewToken, err := jwtService.Refresh(refreshToken)