Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
29 lines
719 B
Go
29 lines
719 B
Go
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()
|
|
}
|
|
}
|