-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement client-side authentication.
- Loading branch information
Showing
8 changed files
with
460 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns rocks.mygiftlist.authentication | ||
(:require ["@auth0/auth0-spa-js" :as create-auth0-client] | ||
[com.wsscode.async.async-cljs :refer [go-promise <!p]] | ||
[rocks.mygiftlist.config :as config])) | ||
|
||
(defonce auth0-client (atom nil)) | ||
|
||
(defn create-auth0-client! [] | ||
(go-promise | ||
(reset! auth0-client | ||
(<!p (create-auth0-client | ||
#js {:domain config/AUTH0_DOMAIN | ||
:client_id config/AUTH0_CLIENT_ID | ||
:audience config/AUTH0_AUDIENCE | ||
:connection config/AUTH0_CONNECTION}))))) | ||
|
||
(defn is-authenticated? [] | ||
(go-promise (<!p (.isAuthenticated @auth0-client)))) | ||
|
||
(defn login [] | ||
(.loginWithRedirect @auth0-client | ||
#js {:redirect_uri (.. js/window -location -origin)})) | ||
|
||
(defn handle-redirect-callback [] | ||
(go-promise (<!p (.handleRedirectCallback @auth0-client)))) | ||
|
||
(defn logout [] | ||
(.logout @auth0-client | ||
#js {:returnTo (.. js/window -location -origin)})) | ||
|
||
(defn get-access-token [] | ||
(go-promise (<!p (.getTokenSilently @auth0-client)))) | ||
|
||
(defn get-user-info [] | ||
(go-promise (<!p (.getUser @auth0-client)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,54 @@ | ||
(ns rocks.mygiftlist.client | ||
(:require [rocks.mygiftlist.application :refer [SPA]] | ||
[rocks.mygiftlist.authentication :as auth] | ||
[com.fulcrologic.fulcro.application :as app] | ||
[com.fulcrologic.fulcro.components :as comp :refer [defsc]] | ||
[com.fulcrologic.fulcro.dom :as dom] | ||
[com.fulcrologic.fulcro.mutations :refer [defmutation]] | ||
[clojure.core.async :refer [go <!]] | ||
[clojure.string :as str] | ||
[taoensso.timbre :as log])) | ||
|
||
(defsc Root [this _] | ||
{:query [] | ||
:initial-state {}} | ||
(defsc CurrentUser [this {:user/keys [id email] | ||
:ui/keys [loading] :as user}] | ||
{:query [:user/id :user/email :ui/loading] | ||
:ident (fn [] [:component/id :current-user]) | ||
:initial-state {:ui/loading true}} | ||
(cond | ||
loading (dom/button :.ui.loading.primary.button) | ||
(and id email) (dom/button :.ui.primary.button | ||
{:onClick #(auth/logout)} | ||
"Logout") | ||
:else (dom/button :.ui.primary.button | ||
{:onClick #(auth/login)} | ||
"Login/Signup"))) | ||
|
||
(def ui-current-user (comp/factory CurrentUser)) | ||
|
||
(defsc Root [this {:root/keys [current-user]}] | ||
{:query [{:root/current-user (comp/get-query CurrentUser)}] | ||
:initial-state {:root/current-user {}}} | ||
(dom/div {} | ||
"Hello World")) | ||
(dom/div {} "Hello World") | ||
(ui-current-user current-user))) | ||
|
||
(defmutation set-current-user [user] | ||
(action [{:keys [state]}] | ||
(swap! state assoc-in [:component/id :current-user] (assoc user :ui/loading false)))) | ||
|
||
(defn ^:export refresh [] | ||
(log/info "Hot code reload...") | ||
(app/mount! SPA Root "app")) | ||
|
||
(defn ^:export init [] | ||
(log/info "Application starting...") | ||
(app/mount! SPA Root "app")) | ||
(app/mount! SPA Root "app") | ||
(go | ||
(<! (auth/create-auth0-client!)) | ||
(when (str/includes? (.. js/window -location -search) "code=") | ||
(<! (auth/handle-redirect-callback)) | ||
(.replaceState js/window.history #js {} js/document.title js/window.location.pathname)) | ||
(if-let [authenticated (<! (auth/is-authenticated?))] | ||
(let [{:strs [sub email]} (js->clj (<! (auth/get-user-info)))] | ||
(comp/transact! SPA [(set-current-user {:user/id sub :user/email email})])) | ||
(comp/transact! SPA [(set-current-user {})])))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
(ns rocks.mygiftlist.config) | ||
|
||
(goog-define AUTH0_DOMAIN "") | ||
(goog-define AUTH0_CLIENT_ID "") | ||
(goog-define AUTH0_AUDIENCE "") | ||
(goog-define AUTH0_CONNECTION "") |