/
demo.cljs
179 lines (160 loc) · 5.02 KB
/
demo.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(ns ^:figwheel-hooks com.yetanalytics.re-oidc.demo
"In this example, we initialize re-oidc from a remote config file"
(:require
[goog.dom :as gdom]
[reagent.core :as reagent :refer [atom]]
[reagent.dom :as rdom]
[re-frame.core :as re-frame]
[day8.re-frame.http-fx]
[com.yetanalytics.re-oidc :as re-oidc]
[goog.events :as events]
[clojure.pprint :as pp]
[ajax.core :as ajax]
[goog.string :refer [format]]
goog.string.format))
(defn- push-state
"Push history state to clean up on login/logout"
[path]
(.pushState js/window.history
(clj->js {})
js/document.title
path))
;; config for re-oidc that won't change, isn't loaded dynamically
(def static-config
{:auto-login false
:on-login-success #(push-state "/")
:on-logout-success #(push-state "/")})
;; Init the demo's DB
(re-frame/reg-event-db
::init-db
(fn [_ _]
{}))
;; Fetch the OIDC config from a server
(re-frame/reg-event-fx
::get-oidc-config!
(fn [{:keys [db]} _]
{:http-xhrio {:method :get
:uri "/oidc.json"
:response-format (ajax/json-response-format
{:keywords? true})
:on-success [::recv-oidc-config]
:on-failure [::fail-oidc-config]}}))
;; Receive the OIDC config and initialize
(re-frame/reg-event-fx
::recv-oidc-config
(fn [ctx [_ config]]
{:fx [[:dispatch
;; Initialize OIDC from the remote config
[::re-oidc/init
(assoc static-config
:user-store :local-storage
;; These config options are passed directly to the OIDC client
:oidc-config
config)]]]}))
(re-frame/reg-event-fx
::fail-oidc-config
(fn [ctx [_ {:keys [status]}]]
(.error js/console "Failed to fetch OIDC config, status:" status)
{}))
;; Attempt a call to the API server when logged in
(re-frame/reg-event-fx
::echo-token!
(fn [{{status ::re-oidc/status
:as db} :db} _]
(if (= :loaded status)
(let [{{:keys [access-token]} ::re-oidc/user} db]
{:http-xhrio {:uri "http://0.0.0.0:8081/api"
:method :get
:headers {"Authorization" (format "Bearer %s" access-token)}
:response-format (ajax/json-response-format
{:keywords? true})
:on-success [::recv-token-echo]
:on-failure [::fail-token-echo]}})
(do
(.error js/console "Can't call API server if not logged in!")
{}))))
;; receive token echo from api
(re-frame/reg-event-db
::recv-token-echo
(fn [db [_ token-echo]]
(assoc db ::token-echo token-echo)))
(re-frame/reg-event-fx
::fail-token-echo
(fn [ctx [_ {:keys [status]}]]
(.error js/console "Failed to get token echo from api server, status:" status)
{}))
;; Compose init events for the demo db & getting remote config
(re-frame/reg-event-fx
::init!
(fn [_ _]
{:fx [[:dispatch [::init-db]]
;; Fetch the OIDC config, initializing the UserManager on success
[:dispatch [::get-oidc-config!]]]}))
;; A simple sub to see the DB
(re-frame/reg-sub
::db-debug
(fn [db _]
(-> db
pp/pprint
with-out-str)))
(defn get-app-element []
(gdom/getElement "app"))
(defn process-callbacks!
"Detect post login/logout callbacks and issue route dispatch to re-oidc."
[& _]
(case js/window.location.pathname
"/login-callback" (re-frame/dispatch
[::re-oidc/login-callback
static-config
js/window.location.search])
"/logout-callback" (re-frame/dispatch
[::re-oidc/logout-callback
static-config])
nil))
(defn hello-world []
[:div
[:h2 "DEMO"]
[:p
(if @(re-frame/subscribe [::re-oidc/logged-in?])
"You are logged in"
"You are logged out")]
[:pre @(re-frame/subscribe [::db-debug])]
;; Since the login/logout actions must run after init,
;; you can use the ::re-oidc/status key for things like loading
(case @(re-frame/subscribe [::re-oidc/status])
nil
[:div
[:button "Loading..."]]
:loaded
[:div
[:button
{:on-click #(re-frame/dispatch [::echo-token!])}
"Echo Token"]
[:button
{:on-click #(re-frame/dispatch [::re-oidc/logout])}
"Log out"]]
;; :init/:unloaded
[:div
[:button
{:on-click #(re-frame/dispatch [::re-oidc/login])}
"Log in"]])])
(defn mount [el]
(rdom/render [hello-world] el))
(defn mount-app-element []
(when-let [el (get-app-element)]
(mount el)))
(defn init! []
(re-frame/dispatch-sync [::init!])
(process-callbacks!)
(mount-app-element))
(defonce init
(do
(init!)))
;; specify reload hook with ^:after-load metadata
(defn ^:after-load on-reload []
(println "figwheel reload!")
(mount-app-element)
;; optionally touch your app-state to force rerendering depending on
;; your application
;; (swap! app-state update-in [:__figwheel_counter] inc)
)