33 lines
669 B
Go
33 lines
669 B
Go
|
|
package utils
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
type contextUtil interface {
|
||
|
|
GetUserUuidFromContext(c *gin.Context) (string, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *Handler) GetUserUuidFromContext(c *gin.Context) (string, error) {
|
||
|
|
if c == nil {
|
||
|
|
return "", errors.New("context is nil")
|
||
|
|
}
|
||
|
|
|
||
|
|
uuidRaw, exists := c.Get("userUuid")
|
||
|
|
if !exists {
|
||
|
|
return "", errors.New("user uuid not found in context")
|
||
|
|
}
|
||
|
|
|
||
|
|
userUuidStr, ok := uuidRaw.(string)
|
||
|
|
if !ok {
|
||
|
|
return "", errors.New("user uuid is not a string")
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := uuid.Validate(userUuidStr); err != nil {
|
||
|
|
return "", errors.New("error validating user uuid")
|
||
|
|
}
|
||
|
|
return userUuidStr, nil
|
||
|
|
}
|