From b096863842fa145ef638d599565e7d7eda1326a0 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Wed, 11 Jan 2023 14:09:08 -0800 Subject: [PATCH] Should (mostly) fix the new history page. --- src/clj/auto_ap/handler.clj | 17 ++- src/clj/auto_ap/routes/auth.clj | 19 +-- src/clj/auto_ap/ssr/admin.clj | 126 ++++++++---------- .../views/components/admin/side_bar.cljs | 2 +- 4 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/clj/auto_ap/handler.clj b/src/clj/auto_ap/handler.clj index 23b3cbf7..683c1023 100644 --- a/src/clj/auto_ap/handler.clj +++ b/src/clj/auto_ap/handler.clj @@ -2,21 +2,21 @@ (:require [amazonica.core :refer [defcredential]] [auto-ap.client-routes :as client-routes] - [auto-ap.ssr.admin :as ssr-admin] [auto-ap.routes.auth :as auth] [auto-ap.routes.exports :as exports] + [auto-ap.routes.ezcater :as ezcater] [auto-ap.routes.graphql :as graphql] [auto-ap.routes.invoices :as invoices] [auto-ap.routes.queries :as queries] - [auto-ap.routes.ezcater :as ezcater] [auto-ap.routes.yodlee2 :as yodlee2] + [auto-ap.ssr.admin :as ssr-admin] [bidi.bidi :as bidi] + [buddy.auth.backends.session :refer [session-backend]] [buddy.auth.backends.token :refer [jws-backend]] [buddy.auth.middleware :refer [wrap-authentication wrap-authorization]] [clojure.string :as str] [clojure.tools.logging :as log] - #_{:clj-kondo/ignore [:refer-all]} - [compojure.core :refer [defroutes GET routes context ANY]] + [compojure.core :refer [ANY context defroutes GET routes]] [compojure.route :as route] [config.core :refer [env]] [mount.core :as mount] @@ -24,6 +24,7 @@ [ring.middleware.multipart-params :as mp] [ring.middleware.params :refer [wrap-params]] [ring.middleware.reload :refer [wrap-reload]] + [ring.middleware.session :refer [wrap-session]] [ring.util.response :as response] [unilog.context :as lc])) @@ -93,8 +94,12 @@ (def app (-> #'app-routes (wrap-logging) - (wrap-authorization auth-backend) - (wrap-authentication auth-backend) + (wrap-authorization auth-backend + ) + (wrap-authentication auth-backend + (session-backend)) + + (wrap-session) (wrap-reload) (wrap-params) (mp/wrap-multipart-params) diff --git a/src/clj/auto_ap/routes/auth.clj b/src/clj/auto_ap/routes/auth.clj index 2b743ac7..01decba7 100644 --- a/src/clj/auto_ap/routes/auth.clj +++ b/src/clj/auto_ap/routes/auth.clj @@ -39,22 +39,25 @@ :user/provider-id (:id profile) :user/role :user-role/none :user/name (:name profile)}) + auth {:user (:name profile) + :exp (time/plus (time/now) (time/days 30)) + :user/clients (map (fn [c] + (select-keys c [:client/code :db/id :client/name :client/locations])) + (:user/clients user)) + :user/role (name (:user/role user)) + :user/name (:name profile)} ] (log/info "authenticated as user" user) ;; TODO - these namespaces are not being transmitted/deserialized properly + (if (and token user) - (let [jwt (jwt/sign {:user (:name profile) - :exp (time/plus (time/now) (time/days 30)) - :user/clients (map (fn [c] - (select-keys c [:client/code :db/id :client/name :client/locations])) - (:user/clients user)) - :user/role (name (:user/role user)) - :user/name (:name profile)} + (let [jwt (jwt/sign auth (:jwt-secret env) {:alg :hs512})] {:status 301 - :headers {"Location" (str "/?jwt=" jwt)}}) + :headers {"Location" (str "/?jwt=" jwt)} + :session {:identity auth}}) {:status 401 :body "Couldn't authenticate"})) (catch Exception e diff --git a/src/clj/auto_ap/ssr/admin.clj b/src/clj/auto_ap/ssr/admin.clj index 2ab8462f..46163bd1 100644 --- a/src/clj/auto_ap/ssr/admin.clj +++ b/src/clj/auto_ap/ssr/admin.clj @@ -11,47 +11,42 @@ [datomic.api :as d] [hiccup2.core :as hiccup])) -(defn setup-script [request] - (str - " -window.onload = function () { - document.body.addEventListener(\"htmx:configRequest\", function(event) { - event.detail.headers[\"Authorization\"] = \"Token \" + localStorage.getItem(\"jwt\") ; +(defn html-page [hiccup] + {:status 200 + :headers {"Content-Type" "text/html"} + :body (str (hiccup/html + {} + hiccup))}) - console.log(event.detail.headers[\"Authorization\"]); - }); -}")) - -(defn base-page [request contents] - [:html.has-navbar-fixed-top - [:head - [:meta {:charset "utf-8"}] - [:meta {:http-equiv "X-UA-Compatible", :content "IE=edge"}] - [:meta {:name "viewport", :content "width=device-width, initial-scale=1"}] - [:title "Integreat"] - [:link {:rel "stylesheet", :href "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css", :integrity "sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=", :crossorigin "anonymous"}] - [:link {:href "/css/font.min.css", :rel "stylesheet"}] - [:link {:rel "stylesheet", :href "/css/bulma.min.css"}] - [:link {:rel "stylesheet", :href "/css/bulma-calendar.min.css"}] - [:link {:rel "stylesheet", :href "/css/bulma-badge.min.css"}] - [:link {:rel "stylesheet", :href "/css/react-datepicker.min.inc.css"}] - [:link {:rel "stylesheet", :href "/css/animate.css"}] - [:link {:rel "stylesheet", :href "/finance-font/style.css"}] - [:link {:rel "stylesheet", :href "/css/main.css"}] - [:link {:rel "stylesheet", :href "https://unpkg.com/placeholder-loading/dist/css/placeholder-loading.min.css"}] - [:script {:src "https://unpkg.com/hyperscript.org@0.9.7"}] - [:script {:src "https://unpkg.com/htmx.org@1.8.4" - :integrity "sha384-wg5Y/JwF7VxGk4zLsJEcAojRtlVp1FKKdGy1qN+OMtdq72WRvX/EdRdqg/LOhYeV" - :crossorigin= "anonymous"}] - [:script {:type "text/javascript", :src "https://cdn.yodlee.com/fastlink/v4/initialize.js", :async "async" }] - [:script {:type "text/javascript"} - (hiccup/raw (setup-script request))]] - - - [:body - [:div {:id "app"} - [:div - [:nav {:class "navbar has-shadow is-fixed-top is-grey"} +(defn base-page [contents] + (html-page + [:html.has-navbar-fixed-top + [:head + [:meta {:charset "utf-8"}] + [:meta {:http-equiv "X-UA-Compatible", :content "IE=edge"}] + [:meta {:name "viewport", :content "width=device-width, initial-scale=1"}] + [:title "Integreat"] + [:link {:rel "stylesheet", :href "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css", :integrity "sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=", :crossorigin "anonymous"}] + [:link {:href "/css/font.min.css", :rel "stylesheet"}] + [:link {:rel "stylesheet", :href "/css/bulma.min.css"}] + [:link {:rel "stylesheet", :href "/css/bulma-calendar.min.css"}] + [:link {:rel "stylesheet", :href "/css/bulma-badge.min.css"}] + [:link {:rel "stylesheet", :href "/css/react-datepicker.min.inc.css"}] + [:link {:rel "stylesheet", :href "/css/animate.css"}] + [:link {:rel "stylesheet", :href "/finance-font/style.css"}] + [:link {:rel "stylesheet", :href "/css/main.css"}] + [:link {:rel "stylesheet", :href "https://unpkg.com/placeholder-loading/dist/css/placeholder-loading.min.css"}] + [:script {:src "https://unpkg.com/hyperscript.org@0.9.7"}] + [:script {:src "https://unpkg.com/htmx.org@1.8.4" + :integrity "sha384-wg5Y/JwF7VxGk4zLsJEcAojRtlVp1FKKdGy1qN+OMtdq72WRvX/EdRdqg/LOhYeV" + :crossorigin= "anonymous"}] + [:script {:type "text/javascript", :src "https://cdn.yodlee.com/fastlink/v4/initialize.js", :async "async" }]] + + + [:body + [:div {:id "app"} + [:div + [:nav {:class "navbar has-shadow is-fixed-top is-grey"} [:div {:class "container"} [:div {:class "navbar-brand"} @@ -59,28 +54,28 @@ window.onload = function () { [:img {:src "/img/logo.png"}]]] [:div.navbar-menu {:id "navMenu"} [:div.navbar-start - [:a.navbar-item {:href "/"} + [:a.navbar-item {:href "/"} "Home" ] - [:a.navbar-item {:href "/invoices/"} + [:a.navbar-item {:href "/invoices/"} "Invoices" ] - [:a.navbar-item {:href "/payments/"} + [:a.navbar-item {:href "/payments/"} "Payments" ] [:a.navbar-item {:href "/pos/sales-orders/"} "POS" ] - [:a.navbar-item {:href "/transactions/"} + [:a.navbar-item {:href "/transactions/"} "Transactions" ] - [:a.navbar-item {:href "/ledger/"} + [:a.navbar-item {:href "/ledger/"} "Ledger" ]]]]] - [:div {:class "columns has-shadow", :id "mail-app", :style "margin-bottom: 0px; height: calc(100vh - 46px);"} - [:aside {:class "column aside menu is-2 "} - [:div {:class "main left-nav"} - [:div]]] - [:div {:class "column messages hero ", :id "message-feed", :style "overflow: auto;"} - [:div {:class "inbox-messages"} - contents]]] - [:div] - [:div {:id "dz-hidden"}]]]]]) + [:div {:class "columns has-shadow", :id "mail-app", :style "margin-bottom: 0px; height: calc(100vh - 46px);"} + [:aside {:class "column aside menu is-2 "} + [:div {:class "main left-nav"} + [:div]]] + [:div {:class "column messages hero ", :id "message-feed", :style "overflow: auto;"} + [:div {:class "inbox-messages"} + contents]]] + [:div] + [:div {:id "dz-hidden"}]]]]])) (defn html-response [hiccup] {:status 200 @@ -158,7 +153,6 @@ window.onload = function () { table]]) (defn history-search [{:keys [form-params params] identity :identity :as request}] - (assert-admin identity) (log/info ::request request) @@ -207,10 +201,10 @@ window.onload = function () { (for [[tx a c] history] [:tr [:td (:db/id tx)] - [:td (-> (:db/txInstant tx) - coerce/to-date-time - atime/localize - (atime/unparse atime/standard-time))] + [:td (some-> (:db/txInstant tx) + coerce/to-date-time + atime/localize + (atime/unparse atime/standard-time))] [:td (str (:audit/user tx))] [:td (namespace a) ": " (name a)] [:td @@ -230,12 +224,11 @@ window.onload = function () { (alog/info ::trace :bge best-guess-entity :headers (:headers request)) - (html-response - (if (get (:headers request) "hx-request") - table - (base-page request - (page-template :table table - :entity-id entity-id))))) + (if (get (:headers request) "hx-request") + (html-response + table) + (base-page (page-template :table table + :entity-id entity-id)))) (catch NumberFormatException e (html-response (str [:div.notification.is-danger.is-light @@ -268,8 +261,7 @@ window.onload = function () { "Cannot parse the entity-id " entity-id ". It should be a number."])))) (defn history [{:keys [identity] :as request}] - (html-response - (base-page request (page-template )))) + (base-page (page-template ))) (defroutes admin-routes (routes diff --git a/src/cljs/auto_ap/views/components/admin/side_bar.cljs b/src/cljs/auto_ap/views/components/admin/side_bar.cljs index 4f7b42ac..b8257fe1 100644 --- a/src/cljs/auto_ap/views/components/admin/side_bar.cljs +++ b/src/cljs/auto_ap/views/components/admin/side_bar.cljs @@ -46,7 +46,7 @@ [:span {:class "icon icon-cog-play-1" :style {:font-size "25px"}}] [:span {:class "name"} "Rules"]]] [:li.menu-item - [:a {:href (str "/admin/history?jwt=" (.getItem js/localStorage "jwt")) :class (str "item" (active-when ap = :admin-history))} + [:a {:href (str "/admin/history") :class (str "item" (active-when ap = :admin-history))} [:span {:class "icon icon-cog-play-1" :style {:font-size "25px"}}] [:span {:class "name"} "History "]]] [:ul ]]