Some checks failed
Deploy with Docker Compose / deploy (push) Failing after 1s
Replace 5 separate REST calls on home page load with a single GraphQL query. Add homeData store that fetches posts, favorites, activities, spotify, and auth in one request. Convert all admin mutations and auth flows to use GraphQL. Add album images to Spotify GraphQL schema. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
53 lines
936 B
Go
53 lines
936 B
Go
package graph
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const (
|
|
userClaimsKey contextKey = "userClaims"
|
|
ginContextKey contextKey = "ginContext"
|
|
)
|
|
|
|
func UserClaimsFromCtx(ctx context.Context) *jwt.MapClaims {
|
|
claims, ok := ctx.Value(userClaimsKey).(*jwt.MapClaims)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return claims
|
|
}
|
|
|
|
func UserIDFromCtx(ctx context.Context) (uint, bool) {
|
|
claims := UserClaimsFromCtx(ctx)
|
|
if claims == nil {
|
|
return 0, false
|
|
}
|
|
idF, ok := (*claims)["id"].(float64)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
return uint(idF), true
|
|
}
|
|
|
|
func IsAdminFromCtx(ctx context.Context) bool {
|
|
claims := UserClaimsFromCtx(ctx)
|
|
if claims == nil {
|
|
return false
|
|
}
|
|
admin, ok := (*claims)["admin"].(bool)
|
|
return ok && admin
|
|
}
|
|
|
|
func GinContextFromCtx(ctx context.Context) *gin.Context {
|
|
gc, ok := ctx.Value(ginContextKey).(*gin.Context)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return gc
|
|
}
|