package routes import ( "fmt" "github.com/aziis98/lupus-lite/auth" "github.com/aziis98/lupus-lite/model" "github.com/gofiber/fiber/v2" ) const UserKey = "github.com/aziis98/lupus-lite/user" func requestUser(c *fiber.Ctx) *model.User { return c.Locals(UserKey).(*model.User) } func RequireLoggedMiddleware(auth auth.AuthService) fiber.Handler { return func(c *fiber.Ctx) error { token := c.Cookies("sid") if token == "" { return fmt.Errorf(`request has no session token`) } username, err := auth.UserForSession(token) if err != nil { return err } user, err := auth.GetUser(username) if err != nil { return err } c.Locals(UserKey, &user) return c.Next() } }