Files
mtusi2026lab3/app/internal/common/locals.go
T
2026-10-30 18:11:30 +03:00

30 lines
578 B
Go

package common
import (
"github.com/gofiber/fiber/v3"
"github.com/google/uuid"
)
const (
KeyUserID = "userID"
KeySessionToken = "sessionToken"
)
func UserID(c fiber.Ctx) (uuid.UUID, bool) {
userID, ok := c.Locals(KeyUserID).(uuid.UUID)
return userID, ok
}
func MustUserID(c fiber.Ctx) uuid.UUID {
userID, ok := UserID(c)
if !ok {
panic("common: authenticated user ID is missing from the request context")
}
return userID
}
func SessionToken(c fiber.Ctx) (string, bool) {
token, ok := c.Locals(KeySessionToken).(string)
return token, ok && token != ""
}