Files
go_oxspeak_server/network/http/context/context.go
2025-11-11 02:33:05 +01:00

52 lines
987 B
Go

package context
import (
"go_oxspeak_server/models"
"github.com/gin-gonic/gin"
)
// Context wrapper avec des helpers
// NE contient PAS de dépendances globales (DB, Config, etc.)
type Context struct {
*gin.Context
}
func NewContext(c *gin.Context) *Context {
return &Context{Context: c}
}
// Helpers pour accéder aux données de REQUÊTE
func (c *Context) GetCurrentUser() (*models.User, bool) {
value, exists := c.Get("currentUser")
if !exists {
return nil, false
}
user, ok := value.(*models.User)
return user, ok
}
func (c *Context) MustGetCurrentUser() *models.User {
user, ok := c.GetCurrentUser()
if !ok {
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
panic("no authenticated user")
}
return user
}
func (c *Context) GetRequestID() string {
value, exists := c.Get("requestID")
if !exists {
return ""
}
id, _ := value.(string)
return id
}
func (c *Context) IsAuthenticated() bool {
_, exists := c.GetCurrentUser()
return exists
}