(ns auto-ap.routes.utils
  (:require [buddy.auth :refer [authenticated?]]
            [clojure.spec.alpha :as s]))

(defn wrap-secure [handler]
  (fn [request]
    (if (authenticated? request)
      (handler request)
      {:status 401
       :body "not authenticated"})))

(defn wrap-spec [handler spec]
  (fn [request]
    (if (not (s/valid? spec (:edn-params request)))
      {:status 400
       :body (pr-str (s/explain-data spec (:edn-params request)))
       :headers {"Content-Type" "application/edn"}}
      (handler request))))
