From 89f6e08c4fab10cde8535402d4572f4dc149af51 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Wed, 21 Mar 2018 14:22:02 +0100 Subject: [PATCH] css experiments, neo4j --- project.clj | 3 +- resources/public/css/app.css | 70 ++++++++++++++++++++++++++++++------ src/wanijo/auth/handler.clj | 21 +++++++---- src/wanijo/auth/routes.clj | 3 +- src/wanijo/auth/view.clj | 28 +++++++-------- src/wanijo/handler.clj | 27 ++++++-------- src/wanijo/home/routes.clj | 0 src/wanijo/neo4j.clj | 16 +++++++++ src/wanijo/view.clj | 51 +++++++++++++++----------- 9 files changed, 149 insertions(+), 70 deletions(-) create mode 100644 src/wanijo/home/routes.clj create mode 100644 src/wanijo/neo4j.clj diff --git a/project.clj b/project.clj index 617b6bb..9b48b07 100644 --- a/project.clj +++ b/project.clj @@ -6,7 +6,8 @@ [compojure "1.5.1"] [ring/ring-defaults "0.2.1"] [clojurewerkz/neocons "3.2.0"] - [hiccup "1.0.5"]] + [hiccup "1.0.5"] + [buddy "2.0.0"]] :plugins [[lein-ring "0.9.7"]] :ring {:handler wanijo.handler/app} :profiles diff --git a/resources/public/css/app.css b/resources/public/css/app.css index 58e2f76..755090e 100644 --- a/resources/public/css/app.css +++ b/resources/public/css/app.css @@ -2,6 +2,17 @@ body { font-family: sans-serif; } +a:link, +a:visited { + color: blue; + text-decoration: none; +} + +a:hover, +a:active { + text-decoration: underline; +} + .grid { display: grid; grid-template-columns: 20% 70% 10%; @@ -33,27 +44,64 @@ header, footer, main, nav, aside { header { grid-area: header; + display: grid; + grid-template-columns: 20% 80%; + align-items: center; + border-left: 1rem solid #ccc; +} + +.app-title { + grid-column: 1; + font-size: 1.5rem; +} + +.app-title__hello { + font-size: 1rem; + margin-left: 1rem; + font-weight: normal; + font-style: italic; +} + +.header-content { + grid-column: 2; +} + +.header-content__link { + padding-top: .5rem; + display: inline-block; } footer { grid-area: footer; } -.form-group { - display: flex; - margin-bottom: 1rem; +form { + display: grid; + grid-template-columns: 30% 70%; + grid-auto-flow: row; +} + +form label, +form input, +form select, +form textarea, +form section.flash { + margin-bottom: .7rem; + padding: .2rem .5rem; } -.form-group label { - width: 30%; +form section.flash { + grid-column: 1/3; + border: 1px solid #ccc; + border-left-width: 1rem; + padding: .7rem; } -.form-group input, -.form-group select { - width: 70%; - padding: .1rem; +form label { + grid-column: 1; } -.form-group input[type=submit] { - width: auto; +form input, +form select { + grid-column: 2; } diff --git a/src/wanijo/auth/handler.clj b/src/wanijo/auth/handler.clj index c72a783..e3f8492 100644 --- a/src/wanijo/auth/handler.clj +++ b/src/wanijo/auth/handler.clj @@ -1,11 +1,20 @@ (ns wanijo.auth.handler - (:require [ring.util.response :as rur])) + (:require [ring.util.response :as rur] + [wanijo.neo4j :as neo] + [buddy.hashers :as bh])) -(defn login-check [req] - (let [{{:keys [uname pw]} :params} req] - (if (and (= uname "admin") - (= pw "pw")) +(defn login-check! [req] + (let [{{:keys [uname pw]} :params} req + unode (neo/find-user! uname) + pwmatch (when-let [hash (:pw unode)] + (bh/check pw hash))] + (if pwmatch (-> (rur/redirect "/") - (assoc-in [:session :ident] uname)) + (assoc-in [:session :ident] uname) + (assoc-in [:session :unode] unode)) (-> (rur/redirect "/login") (assoc :flash :invalid-credentials))))) + +(defn logout [req] + (-> (rur/redirect "/login") + (assoc :session nil))) diff --git a/src/wanijo/auth/routes.clj b/src/wanijo/auth/routes.clj index ad7d7b9..22ec939 100644 --- a/src/wanijo/auth/routes.clj +++ b/src/wanijo/auth/routes.clj @@ -5,4 +5,5 @@ (defroutes auth-routes (GET "/login" [] auth-view/login) - (POST "/login-check" [] auth-handler/login-check)) + (POST "/login-check" [] auth-handler/login-check!) + (GET "/logout" [] auth-handler/logout)) diff --git a/src/wanijo/auth/view.clj b/src/wanijo/auth/view.clj index b0fd145..71d0e79 100644 --- a/src/wanijo/auth/view.clj +++ b/src/wanijo/auth/view.clj @@ -6,17 +6,17 @@ (defn login [req] (layout :authed? false - :content [[:h1 "Login"] - (hf/form-to - [:post "/login-check"] - [:dif.form-group - (:flash req)] - [:div.form-group - (hf/label "uname" "Username") - [:input#uname {:name "uname"}]] - [:div.form-group - (hf/label "pw" "Password") - (hf/password-field {:id "pw"} "pw")] - [:div.form-group - (hf/submit-button "Login")] - (anti-forgery-field))])) + :content + [[:h1 "Kama ken"] + (hf/form-to + [:post "/login-check"] + (when (:flash req) [:section.flash (:flash req)]) + ;; + (hf/label "uname" "Nimi") + (hf/text-field {:required "required"} "uname") + ;; + (hf/label "pw" "Toki Pimeja") + (hf/password-field {:required "required"} "pw") + ;; + (hf/submit-button "Kama") + (anti-forgery-field))])) diff --git a/src/wanijo/handler.clj b/src/wanijo/handler.clj index d607b23..0151fb1 100644 --- a/src/wanijo/handler.clj +++ b/src/wanijo/handler.clj @@ -7,28 +7,21 @@ [wanijo.auth.routes :refer [auth-routes]] [wanijo.view :refer [layout]])) -(defn- home [_] - (layout :title "home")) +(defn- home [req] + (layout :title "home" :session (:session req))) + +(defn- wrap-login-redirect [handler] + (fn [req] + (if-not (get-in req [:session :ident]) + (rur/redirect "/login") + (handler req)))) (defroutes app-routes (routes auth-routes) - (GET "/" [] home) + (wrap-login-redirect + (GET "/" [] home)) (route/not-found "Not Found")) -(defn- wrap-login-redirect [handler] - (fn [req] - (let [ident (get-in req [:session :ident]) - uri (:uri req)] - (println ident (:session req)) - (if (and (nil? ident) - (not (or (clojure.string/ends-with? uri ".css") - (clojure.string/ends-with? uri ".js") - (= uri "/login-check") - (= uri "/login")))) - (rur/redirect "/login") - (handler req))))) - (def app (-> app-routes - wrap-login-redirect (wrap-defaults site-defaults))) diff --git a/src/wanijo/home/routes.clj b/src/wanijo/home/routes.clj new file mode 100644 index 0000000..e69de29 diff --git a/src/wanijo/neo4j.clj b/src/wanijo/neo4j.clj new file mode 100644 index 0000000..1d124d4 --- /dev/null +++ b/src/wanijo/neo4j.clj @@ -0,0 +1,16 @@ +(ns wanijo.neo4j + (:require [clojurewerkz.neocons.rest :as nr] + [clojurewerkz.neocons.rest.nodes :as nrn] + [clojurewerkz.neocons.rest.cypher :as nrc])) + +(def conn (nr/connect "http://neo4j:b@localhost:7474/db/data")) + +(defn find-user! [ident] + (some-> + (nrc/tquery + conn + "MATCH (n:user) WHERE n.ident = {ident} RETURN n" + {:ident ident}) + first + (get "n") + :data)) diff --git a/src/wanijo/view.clj b/src/wanijo/view.clj index dbc1b73..73aedc7 100644 --- a/src/wanijo/view.clj +++ b/src/wanijo/view.clj @@ -1,23 +1,34 @@ (ns wanijo.view - (:require [hiccup.page :refer [html5]])) + (:require [hiccup.page :refer [html5 + include-css]])) (defn layout - [& {:keys [authed? content title] - :or {authed? true - content [] - title "wan ijo"}}] - (html5 - [:head - [:meta {:charset "utf-8"}] - [:title title] - [:link {:rel "stylesheet" - :href "/css/app.css"}]] - [:body - [:section.grid - [:header - [:h1 "wan ijo"]] - [:nav (when authed? "nav")] - (vec (concat [:main] content)) - [:aside (when authed? "aside")] - [:footer - [:small "stuff with objects"]]]])) + [& {:keys [content title session] + :or {content [] + title nil + session {}}}] + (let [ident (:ident session) + authed? (some? ident)] + (html5 + [:head + [:meta {:charset "utf-8"}] + [:meta {:name "viewport" + :content "width=device-width,initial-scale=1,shrink-to-fit=no"}] + [:title (str "wan ijo" (when title (str " - " title)))] + (include-css "/css/app.css")] + [:body + [:section.grid + [:header + [:h1.app-title "wan ijo" + (when authed? + [:small.app-title__hello + (str "O, " ident)])] + (when authed? + [:section.header-content + [:a.header-content__link {:href "/logout"} + "Lape"]])] + [:nav (when authed? "nav")] + (vec (concat [:main] content)) + [:aside (when authed? "aside")] + [:footer + [:small "Ilo pali e ijo"]]]])))