Skip to content

henriqueatila/golang-gin-best-practices / skills/golang-gin-auth

golang-gin-auth

Verified

JWT auth and RBAC for Go Gin APIs. Use when adding login, signup, JWT tokens, role checks, protected routes, or password hashing to a Gin app.

references/rbac-route-wiring.md

View on GitHub
# RBAC — Complete Route Wiring

See also: `rbac-middleware.md`, `rbac-resource-and-impersonation.md`, `rbac-hierarchy-and-tenant.md`

## Route Registration

```go
func registerRoutes(r *gin.Engine, ...) {
    api := r.Group("/api/v1")
    api.POST("/auth/login", authHandler.Login)
    api.POST("/auth/refresh", authHandler.Refresh)
    api.POST("/users", userHandler.Create)

    authed := api.Group("")
    authed.Use(middleware.Auth(tokenCfg, logger))
    {
        authed.POST("/auth/logout", authHandler.Logout)
        authed.GET("/users/me", userHandler.GetMe)
        authed.PUT("/users/:id", userHandler.Update) // resource-level check inside handler

        posts := authed.Group("/posts")
        posts.Use(middleware.RequireMinRole("user"))
        { posts.GET("", postHandler.List); posts.POST("", postHandler.Create) }

        moderation := authed.Group("/moderation")
        moderation.Use(middleware.RequireMinRole("moderator"))
        { moderation.PUT("/posts/:id/hide", postHandler.Hide) }

        admin := authed.Group("/admin")
        admin.Use(middleware.RequireRole("admin"))
        {
            admin.GET("/users", userHandler.List)
            admin.DELETE("/users/:id", userHandler.Delete)
            admin.POST("/impersonate", adminHandler.Impersonate)
        }
    }
}
```

**Key principles:**
- Auth middleware is always first in the protected group
- Role/permission middleware comes immediately after Auth
- Resource-level checks belong in the handler, not middleware
- Use `RequireMinRole` for hierarchy, `RequireAnyRole` for explicit sets, `RequireRole` for exact match
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 323kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 323kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 323kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 323kUpdated Claude CodeCodexVerifiedInstall