Skip to content

Commit a595b60

Browse files
committed
feat(api): add FE api utils namespace
for token and local storage management
1 parent 71eb8e9 commit a595b60

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

src/main/parts/frontend/api/core.cljs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
(ns parts.frontend.api.core
22
(:require [cljs.core.async :refer [<! go]]
3-
[cljs-http.client :as http]))
4-
5-
;; Helpers
6-
7-
(defn get-auth-token []
8-
(js/localStorage.getItem "auth-token"))
3+
[cljs-http.client :as http]
4+
[parts.frontend.api.utils :as utils]))
95

106
(defn add-auth-header [req]
11-
(if-let [token (js/localStorage.getItem "auth-token")]
12-
(assoc-in req [:headers "Authorization"] (str "Bearer " token))
7+
(if-let [header (utils/get-auth-header)]
8+
(assoc-in req [:headers "Authorization"] header)
139
req))
1410

1511
(defn GET [endpoint params]
@@ -47,7 +43,7 @@
4743
(go
4844
(let [response (<! (POST "/auth/login" credentials))]
4945
(when (= 200 (:status response))
50-
(js/localStorage.setItem "auth-token" (get-in response [:body :token])))
46+
(utils/save-tokens (:body response)))
5147
response)))
5248

5349
(defn logout []
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(ns parts.frontend.api.utils
2+
(:require [cognitect.transit :as transit]))
3+
4+
(def ^:private token-storage-key "parts-auth-tokens")
5+
(def ^:private user-email-key "parts-user-email")
6+
7+
(defn save-tokens
8+
"Save authentication tokens to local storage"
9+
[tokens]
10+
(.setItem js/localStorage token-storage-key (js/JSON.stringify (clj->js tokens))))
11+
12+
(defn get-tokens
13+
"Get authentication tokens from local storage"
14+
[]
15+
(when-let [tokens-str (.getItem js/localStorage token-storage-key)]
16+
(js->clj (.parse js/JSON tokens-str) :keywordize-keys true)))
17+
18+
(defn clear-tokens
19+
"Clear authentication tokens from local storage"
20+
[]
21+
(.removeItem js/localStorage token-storage-key)
22+
(.removeItem js/localStorage user-email-key))
23+
24+
(defn get-auth-header
25+
"Get the Authorization header for authenticated requests"
26+
[]
27+
(when-let [tokens (get-tokens)]
28+
(str (:token_type tokens) " " (:access_token tokens))))
29+
30+
(defn get-csrf-token
31+
"Get the CSRF token from the meta tag"
32+
[]
33+
(when-let [meta-tag (.querySelector js/document "meta[name='csrf-token']")]
34+
(.getAttribute meta-tag "content")))

src/main/parts/frontend/components/login_modal.cljs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[uix.core :refer [defui $ use-state]]
44
[cljs.core.async :refer [<!]]
55
[parts.frontend.context :as ctx]
6-
[parts.frontend.utils.csrf :as csrf]
6+
[parts.frontend.api.utils :as utils]
77
[parts.frontend.components.modal :refer [modal]])
88
(:require-macros
99
[cljs.core.async.macros :refer [go]]))
@@ -40,12 +40,12 @@
4040
($ :div {:class "error-message"}
4141
error))
4242

43-
(when-let [token (csrf/get-token)]
43+
(when-let [csrf-token (utils/get-csrf-token)]
4444
($ :input
4545
{:type "hidden"
4646
:id "__anti-forgery-token"
4747
:name "__anti-forgery-token"
48-
:value token}))
48+
:value csrf-token}))
4949

5050
($ :div {:class "form-group"}
5151
($ :label {:for "email"} "Email:")

src/main/parts/frontend/context.cljs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require
33
[clojure.core.async :refer [<!]]
44
[parts.frontend.api.core :as api]
5+
[parts.frontend.api.utils :as utils]
56
[uix.core :refer [$ defui use-state use-effect use-context]])
67
(:require-macros
78
[cljs.core.async.macros :refer [go]]))
@@ -40,9 +41,9 @@
4041
(use-effect
4142
(fn []
4243
(println "[auth-provider] checking for token")
43-
(if (api/get-auth-token)
44+
(if (utils/get-tokens)
4445
(do
45-
(println "[auth-provider] token found")
46+
(println "[auth-provider] token found, already signed in")
4647
(fetch-user!))
4748
(set-loading false)))
4849
[fetch-user!])

src/main/parts/frontend/utils/csrf.cljs

-7
This file was deleted.

0 commit comments

Comments
 (0)