forked from mtusi2026/mtusi2026lab3
30 lines
578 B
Go
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 != ""
|
|
}
|