Buddy session

This commit is contained in:
kagel 2016-08-22 22:38:10 +03:00
parent baf07a21ec
commit 8cd7f7705c
9 changed files with 65 additions and 22 deletions

View File

@ -1,5 +1,13 @@
html, body { html, body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
height: 100%; height: 100%;
padding-top: 0px; padding-top: 0px;
}
.profile-link {
display: inline-block;
padding-top: .425rem;
padding-bottom: .425rem;
padding-right: 1em;
color: rgba(0,0,0,.3);
} }

View File

@ -22,6 +22,11 @@
var context = "{{servlet-context}}"; var context = "{{servlet-context}}";
var csrfToken = "{{csrf-token}}"; var csrfToken = "{{csrf-token}}";
var authorizeUrl = "{{authorize-url}}"; var authorizeUrl = "{{authorize-url}}";
var user = "{{login}}";
if (user === "") {
user = null;
}
var token = "{{token}}";
</script> </script>
{% script "/js/app.js" %} {% script "/js/app.js" %}
</body> </body>

View File

@ -15,6 +15,11 @@
:address nil :address nil
:created (new Date)}))) :created (new Date)})))
(defn get-user
[login]
(jdbc/with-db-connection [con-db *db*]
(db/get-user con-db {:login login})))
(defn exists? (defn exists?
[login] [login]
(jdbc/with-db-connection [con-db *db*] (jdbc/with-db-connection [con-db *db*]

View File

@ -4,11 +4,12 @@
[ring.util.http-response :as response] [ring.util.http-response :as response]
[clojure.java.io :as io])) [clojure.java.io :as io]))
(defn home-page [] (defn home-page [{login :login token :token}]
(layout/render "home.html")) (layout/render "home.html" {:login login :token token}))
(defroutes home-routes (defroutes home-routes
(GET "/" [] (home-page)) (GET "/" {{identity :identity} :session}
(home-page identity))
(GET "/logout" [] (home-page nil))
(GET "/docs" [] (-> (response/ok (-> "docs/docs.md" io/resource slurp)) (GET "/docs" [] (-> (response/ok (-> "docs/docs.md" io/resource slurp))
(response/header "Content-Type" "text/plain; charset=utf-8")))) (response/header "Content-Type" "text/plain; charset=utf-8"))))

View File

@ -5,15 +5,19 @@
[commiteth.github.core :as github] [commiteth.github.core :as github]
[commiteth.db.users :as users] [commiteth.db.users :as users]
[commiteth.layout :refer [error-page]] [commiteth.layout :refer [error-page]]
[ring.util.response :as response])) [ring.util.http-response :refer [content-type ok]]
[ring.util.response :as response]
[commiteth.layout :refer [render]]
[cheshire.core :refer [generate-string]]))
(defn- create-user (defn- get-or-create-user
[token] [token]
(let [user (github/get-user token) (let [user (github/get-user token)
{email :email {email :email
name :name name :name
login :login} user] login :login} user]
(if-not (users/exists? login) (or
(users/get-user login)
(users/create-user login name email token)))) (users/create-user login name email token))))
(defroutes redirect-routes (defroutes redirect-routes
@ -25,6 +29,6 @@
(:body (:body
(error-page {:status 401 (error-page {:status 401
:title error})) :title error}))
(do (let [user (get-or-create-user access-token)]
(create-user access-token) (-> (response/redirect "/")
(response/redirect "/")))))) (assoc :session {:identity user})))))))

View File

@ -18,6 +18,18 @@
{:href uri {:href uri
:on-click #(reset! collapsed? true)} title]])) :on-click #(reset! collapsed? true)} title]]))
(defn login-link []
(let [user (rf/subscribe [:user])]
(if-let [login (:login @user)]
[:li.pull-right
[:span.profile-link (str "Logged in as " login)]
[:a.btn.btn-primary.btn-sm {:href "/logout"} "Logout"]]
[:li.pull-right
[:a.btn.btn-social.btn-github
{:href js/authorizeUrl}
[:i.fa.fa-github]
"Sign in with GitHub"]])))
(defn navbar [] (defn navbar []
(r/with-let [collapsed? (r/atom true)] (r/with-let [collapsed? (r/atom true)]
[:nav.navbar.navbar-light.bg-faded [:nav.navbar.navbar-light.bg-faded
@ -27,16 +39,13 @@
(when-not @collapsed? {:class "in"}) (when-not @collapsed? {:class "in"})
[:a.navbar-brand {:href "#/"} "commiteth"] [:a.navbar-brand {:href "#/"} "commiteth"]
[:ul.nav.navbar-nav [:ul.nav.navbar-nav
[nav-link "#/" "Home" :home collapsed?]]]])) [nav-link "#/" "Home" :home collapsed?]
[login-link]]]]))
(defn home-page [] (defn home-page []
[:div.container [:div.container
[:div.jumbotron [:div.jumbotron
[:h1 "Welcome to commitETH"] [:h3 "Welcome to commitETH"]]])
[:p [:a.btn.btn-block.btn-social.btn-github
{:href js/authorizeUrl}
[:i.fa.fa-github]
"Sign in with GitHub"]]]])
(def pages (def pages
{:home #'home-page}) {:home #'home-page})
@ -69,8 +78,13 @@
(defn mount-components [] (defn mount-components []
(r/render [#'page] (.getElementById js/document "app"))) (r/render [#'page] (.getElementById js/document "app")))
(defn load-user []
(when-let [login js/user]
(rf/dispatch [:set-active-user {:login login :token js/token}])))
(defn init! [] (defn init! []
(rf/dispatch-sync [:initialize-db]) (rf/dispatch-sync [:initialize-db])
(load-interceptors!) (load-interceptors!)
(hook-browser-navigation!) (hook-browser-navigation!)
(load-user)
(mount-components)) (mount-components))

View File

@ -1,4 +1,5 @@
(ns commiteth.db) (ns commiteth.db)
(def default-db (def default-db
{:page :home}) {:page :home
:user nil})

View File

@ -11,3 +11,8 @@
:set-active-page :set-active-page
(fn [db [_ page]] (fn [db [_ page]]
(assoc db :page page))) (assoc db :page page)))
(reg-event-db
:set-active-user
(fn [db [_ user]]
(assoc db :user user)))

View File

@ -7,6 +7,6 @@
(:page db))) (:page db)))
(reg-sub (reg-sub
:docs :user
(fn [db _] (fn [db _]
(:docs db))) (:user db)))