package graph import ( "context" "adam-french.co.uk/backend/services" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // AuthContextMiddleware never rejects: a bad or missing token leaves the // context anonymous and the directives decide. No refresh fallback. func AuthContextMiddleware(auth *services.Auth, db *gorm.DB) gin.HandlerFunc { return func(c *gin.Context) { ctx := context.WithValue(c.Request.Context(), ginContextKey, c) accessToken, err := c.Cookie("access_token") if err == nil { _, claims, err := auth.AuthenticateTokenWithClaims(db, accessToken) if err == nil { ctx = context.WithValue(ctx, userClaimsKey, claims) } } c.Request = c.Request.WithContext(ctx) c.Next() } }