init
This commit is contained in:
51
network/http/context/context.go
Normal file
51
network/http/context/context.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user