From 0d62cec021750e42e1b14f6a0d33db998fbffe4a Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 15 May 2025 20:09:08 +0200 Subject: Change oauth flow + create api route to create new category --- src/chef/pages/admin.clj | 21 +++++++++++++-------- src/chef/pages/admin/api.clj | 13 +++++++++++++ src/chef/pages/home.clj | 12 ++++-------- src/chef/routes.clj | 12 +++++++++--- src/chef/utils.clj | 3 ++- 5 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 src/chef/pages/admin/api.clj diff --git a/src/chef/pages/admin.clj b/src/chef/pages/admin.clj index 9aaade3..fb6eeee 100644 --- a/src/chef/pages/admin.clj +++ b/src/chef/pages/admin.clj @@ -17,15 +17,20 @@ [:input {:type :text :placeholder "Frage" :style {:display :inline-block :width :auto}}] ; TODO: only show when category has children - [:img {:src "/static/icons/plus.svg" :height "30em" - :style {:vertical-align :middle - :margin-left "1em"}}]]] + [:img {:src "/static/icons/plus.svg" :height "30em" + :style {:vertical-align :middle + :margin-left "1em"} + :hx-post "/api/admin/create-category" + :hx-swap "none"}]]] [:h2 "Rezepte"] [:i "Coming soon..."])) (defn handler [req] - (cutils/auth-only req - (-> (render) - html/html - str - ruresp/response))) + (let [access-token (get-in req [:oauth2/access-tokens :auth]) + resp (-> (render) + html/html + str + ruresp/response)] + (if (some? access-token) + (assoc resp :session (assoc (:session req) :oauth-token access-token)) + (cutils/auth-only req resp)))) diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj new file mode 100644 index 0000000..3e2ccbd --- /dev/null +++ b/src/chef/pages/admin/api.clj @@ -0,0 +1,13 @@ +(ns chef.pages.admin.api + (:require [chef.utils :as cutils] + [chef.database :as cdb] + [next.jdbc :as jdbc] + [honey.sql :as sql] + [ring.util.response :as ruresp])) + +(defn create-category [req] + (cutils/auth-only req + (jdbc/execute! @cdb/db (sql/format {:insert-into [:categories] + :values [{:name "New category"}]})) + (-> (ruresp/created "Created.") + (ruresp/header "Refresh" "0")))) diff --git a/src/chef/pages/home.clj b/src/chef/pages/home.clj index 2060668..748c60c 100644 --- a/src/chef/pages/home.clj +++ b/src/chef/pages/home.clj @@ -27,11 +27,7 @@ [:div {:id "search-results"}]])) (defn handler [req] - (let [access-token (get-in req [:oauth2/access-tokens :auth]) - resp (-> (render) - html/html - str - ruresp/response)] - (if (some? access-token) - (assoc resp :session (assoc (:session req) :oauth-token access-token)) - resp))) + (-> (render) + html/html + str + ruresp/response)) diff --git a/src/chef/routes.clj b/src/chef/routes.clj index 27d6dfd..8ade8d1 100644 --- a/src/chef/routes.clj +++ b/src/chef/routes.clj @@ -9,14 +9,20 @@ [chef.pages.home :as cphome] [chef.pages.admin :as cpadmin] - [chef.components.search :as ccsearch])) + [chef.components.search :as ccsearch] + + [chef.pages.admin.api :as cpaapi])) (def router [["/" {:get {:handler cphome/handler}}] ["/admin/" {:get {:handler cpadmin/handler}}] ["/static/*" (rring/create-resource-handler)] ["/components" - ["/search" {:get {:handler ccsearch/handler}}]]]) + ["/search" {:get {:handler ccsearch/handler}}]] + + ["/api" + ["/admin" + ["/create-category" {:post {:handler cpaapi/create-category}}]]]]) (def ring-handler (delay (-> router rring/router @@ -28,7 +34,7 @@ :scopes (cstr/split (env/env "OAUTH_SCOPES") #",") :launch-uri "/auth" :redirect-uri "/auth/callback" - :landing-uri "/" + :landing-uri "/admin" :pkce? true}}) rmparams/wrap-params rmsession/wrap-session))) diff --git a/src/chef/utils.clj b/src/chef/utils.clj index 3170be9..f909c52 100644 --- a/src/chef/utils.clj +++ b/src/chef/utils.clj @@ -14,4 +14,5 @@ `(if (some? (get-in ~request [:session :oauth-token])) (do ~@body) ~(-> (ruresp/response "Unauthorized.") - (ruresp/status 401)))) + (ruresp/status 302) + (ruresp/header "Location" "/auth")))) -- cgit v1.2.3