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 {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
height: 100%;
padding-top: 0px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
height: 100%;
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 csrfToken = "{{csrf-token}}";
var authorizeUrl = "{{authorize-url}}";
var user = "{{login}}";
if (user === "") {
user = null;
}
var token = "{{token}}";
</script>
{% script "/js/app.js" %}
</body>

View File

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

View File

@ -4,11 +4,12 @@
[ring.util.http-response :as response]
[clojure.java.io :as io]))
(defn home-page []
(layout/render "home.html"))
(defn home-page [{login :login token :token}]
(layout/render "home.html" {:login login :token token}))
(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))
(response/header "Content-Type" "text/plain; charset=utf-8"))))

View File

@ -5,15 +5,19 @@
[commiteth.github.core :as github]
[commiteth.db.users :as users]
[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]
(let [user (github/get-user token)
{email :email
name :name
login :login} user]
(if-not (users/exists? login)
(or
(users/get-user login)
(users/create-user login name email token))))
(defroutes redirect-routes
@ -25,6 +29,6 @@
(:body
(error-page {:status 401
:title error}))
(do
(create-user access-token)
(response/redirect "/"))))))
(let [user (get-or-create-user access-token)]
(-> (response/redirect "/")
(assoc :session {:identity user})))))))

View File

@ -18,6 +18,18 @@
{:href uri
: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 []
(r/with-let [collapsed? (r/atom true)]
[:nav.navbar.navbar-light.bg-faded
@ -27,16 +39,13 @@
(when-not @collapsed? {:class "in"})
[:a.navbar-brand {:href "#/"} "commiteth"]
[:ul.nav.navbar-nav
[nav-link "#/" "Home" :home collapsed?]]]]))
[nav-link "#/" "Home" :home collapsed?]
[login-link]]]]))
(defn home-page []
[:div.container
[:div.jumbotron
[:h1 "Welcome to commitETH"]
[:p [:a.btn.btn-block.btn-social.btn-github
{:href js/authorizeUrl}
[:i.fa.fa-github]
"Sign in with GitHub"]]]])
[:h3 "Welcome to commitETH"]]])
(def pages
{:home #'home-page})
@ -69,8 +78,13 @@
(defn mount-components []
(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! []
(rf/dispatch-sync [:initialize-db])
(load-interceptors!)
(hook-browser-navigation!)
(load-user)
(mount-components))

View File

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

View File

@ -11,3 +11,8 @@
:set-active-page
(fn [db [_ 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)))
(reg-sub
:docs
:user
(fn [db _]
(:docs db)))
(:user db)))