File tree 5 files changed +45
-21
lines changed
5 files changed +45
-21
lines changed Original file line number Diff line number Diff line change 1
1
(ns parts.frontend.api.core
2
2
(: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]))
9
5
10
6
(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 )
13
9
req))
14
10
15
11
(defn GET [endpoint params]
47
43
(go
48
44
(let [response (<! (POST " /auth/login" credentials))]
49
45
(when (= 200 (:status response))
50
- (js/localStorage.setItem " auth-token " ( get-in response [ :body :token ] )))
46
+ (utils/save-tokens ( :body response )))
51
47
response)))
52
48
53
49
(defn logout []
Original file line number Diff line number Diff line change
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" )))
Original file line number Diff line number Diff line change 3
3
[uix.core :refer [defui $ use-state]]
4
4
[cljs.core.async :refer [<!]]
5
5
[parts.frontend.context :as ctx]
6
- [parts.frontend.utils.csrf :as csrf ]
6
+ [parts.frontend.api.utils :as utils ]
7
7
[parts.frontend.components.modal :refer [modal]])
8
8
(:require-macros
9
9
[cljs.core.async.macros :refer [go]]))
40
40
($ :div {:class " error-message" }
41
41
error))
42
42
43
- (when-let [token (csrf /get-token )]
43
+ (when-let [csrf- token (utils /get-csrf -token )]
44
44
($ :input
45
45
{:type " hidden"
46
46
:id " __anti-forgery-token"
47
47
:name " __anti-forgery-token"
48
- :value token}))
48
+ :value csrf- token}))
49
49
50
50
($ :div {:class " form-group" }
51
51
($ :label {:for " email" } " Email:" )
Original file line number Diff line number Diff line change 2
2
(:require
3
3
[clojure.core.async :refer [<!]]
4
4
[parts.frontend.api.core :as api]
5
+ [parts.frontend.api.utils :as utils]
5
6
[uix.core :refer [$ defui use-state use-effect use-context]])
6
7
(:require-macros
7
8
[cljs.core.async.macros :refer [go]]))
40
41
(use-effect
41
42
(fn []
42
43
(println " [auth-provider] checking for token" )
43
- (if (api /get-auth-token )
44
+ (if (utils /get-tokens )
44
45
(do
45
- (println " [auth-provider] token found" )
46
+ (println " [auth-provider] token found, already signed in " )
46
47
(fetch-user! ))
47
48
(set-loading false )))
48
49
[fetch-user!])
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments