|
1 | 1 | (ns rocks.mygiftlist.client |
2 | 2 | (:require [rocks.mygiftlist.application :refer [SPA]] |
| 3 | + [rocks.mygiftlist.authentication :as auth] |
3 | 4 | [com.fulcrologic.fulcro.application :as app] |
4 | 5 | [com.fulcrologic.fulcro.components :as comp :refer [defsc]] |
5 | 6 | [com.fulcrologic.fulcro.dom :as dom] |
| 7 | + [com.fulcrologic.fulcro.mutations :refer [defmutation]] |
| 8 | + [clojure.core.async :refer [go <!]] |
| 9 | + [clojure.string :as str] |
6 | 10 | [taoensso.timbre :as log])) |
7 | 11 |
|
8 | | -(defsc Root [this _] |
9 | | - {:query [] |
10 | | - :initial-state {}} |
| 12 | +(defsc CurrentUser [this {:user/keys [id email] |
| 13 | + :ui/keys [loading] :as user}] |
| 14 | + {:query [:user/id :user/email :ui/loading] |
| 15 | + :ident (fn [] [:component/id :current-user]) |
| 16 | + :initial-state {:ui/loading true}} |
| 17 | + (cond |
| 18 | + loading (dom/button :.ui.loading.primary.button) |
| 19 | + (and id email) (dom/button :.ui.primary.button |
| 20 | + {:onClick #(auth/logout)} |
| 21 | + "Logout") |
| 22 | + :else (dom/button :.ui.primary.button |
| 23 | + {:onClick #(auth/login)} |
| 24 | + "Login/Signup"))) |
| 25 | + |
| 26 | +(def ui-current-user (comp/factory CurrentUser)) |
| 27 | + |
| 28 | +(defsc Root [this {:root/keys [current-user]}] |
| 29 | + {:query [{:root/current-user (comp/get-query CurrentUser)}] |
| 30 | + :initial-state {:root/current-user {}}} |
11 | 31 | (dom/div {} |
12 | | - "Hello World")) |
| 32 | + (dom/div {} "Hello World") |
| 33 | + (ui-current-user current-user))) |
| 34 | + |
| 35 | +(defmutation set-current-user [user] |
| 36 | + (action [{:keys [state]}] |
| 37 | + (swap! state assoc-in [:component/id :current-user] (assoc user :ui/loading false)))) |
13 | 38 |
|
14 | 39 | (defn ^:export refresh [] |
15 | 40 | (log/info "Hot code reload...") |
16 | 41 | (app/mount! SPA Root "app")) |
17 | 42 |
|
18 | 43 | (defn ^:export init [] |
19 | 44 | (log/info "Application starting...") |
20 | | - (app/mount! SPA Root "app")) |
| 45 | + (app/mount! SPA Root "app") |
| 46 | + (go |
| 47 | + (<! (auth/create-auth0-client!)) |
| 48 | + (when (str/includes? (.. js/window -location -search) "code=") |
| 49 | + (<! (auth/handle-redirect-callback)) |
| 50 | + (.replaceState js/window.history #js {} js/document.title js/window.location.pathname)) |
| 51 | + (if-let [authenticated (<! (auth/is-authenticated?))] |
| 52 | + (let [{:strs [sub email]} (js->clj (<! (auth/get-user-info)))] |
| 53 | + (comp/transact! SPA [(set-current-user {:user/id sub :user/email email})])) |
| 54 | + (comp/transact! SPA [(set-current-user {})])))) |
0 commit comments