@micha.bigler/ui-core-micha 2.1.5 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/apiClient.js +21 -12
- package/package.json +1 -1
- package/src/auth/apiClient.jsx +23 -18
package/dist/auth/apiClient.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// src/auth/apiClient.js
|
|
1
|
+
// src/auth/apiClient.js (in your core library)
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
import { CSRF_URL } from "./authConfig";
|
|
4
4
|
const apiClient = axios.create({
|
|
@@ -21,25 +21,34 @@ function redirectToLoginOnce() {
|
|
|
21
21
|
redirectingToLogin = true;
|
|
22
22
|
window.location.assign("/login");
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
apiClient.interceptors.response.use((response) => response, (error) => {
|
|
25
25
|
var _a, _b, _c, _d;
|
|
26
26
|
const status = (_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : null;
|
|
27
|
-
if (status !== 403)
|
|
28
|
-
return false;
|
|
29
27
|
const data = (_d = (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data) !== null && _d !== void 0 ? _d : {};
|
|
30
|
-
const detail = typeof
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
apiClient.interceptors.response.use((response) => response, (error) => {
|
|
35
|
-
var _a, _b;
|
|
36
|
-
const status = (_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : null;
|
|
37
|
-
if (status === 401 || isAuthish403(error)) {
|
|
28
|
+
const detail = typeof data.detail === "string" ? data.detail : "";
|
|
29
|
+
const isStandard401 = status === 401;
|
|
30
|
+
const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
|
|
31
|
+
if (isStandard401 || isDrfAuth403) {
|
|
38
32
|
redirectToLoginOnce();
|
|
39
33
|
}
|
|
40
34
|
return Promise.reject(error);
|
|
41
35
|
});
|
|
36
|
+
// --- NEW HELPER ---
|
|
37
|
+
function getCookie(name) {
|
|
38
|
+
if (!isBrowser() || !document.cookie)
|
|
39
|
+
return null;
|
|
40
|
+
const xsrfCookies = document.cookie.split(';')
|
|
41
|
+
.map(c => c.trim())
|
|
42
|
+
.filter(c => c.startsWith(name + '='));
|
|
43
|
+
if (xsrfCookies.length === 0)
|
|
44
|
+
return null;
|
|
45
|
+
return decodeURIComponent(xsrfCookies[0].split('=')[1]);
|
|
46
|
+
}
|
|
42
47
|
export async function ensureCsrfToken() {
|
|
48
|
+
// OPTIMIZATION: If we already have the token, skip the network request
|
|
49
|
+
if (getCookie("csrftoken")) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
43
52
|
try {
|
|
44
53
|
await apiClient.get(CSRF_URL);
|
|
45
54
|
}
|
package/package.json
CHANGED
package/src/auth/apiClient.jsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// src/auth/apiClient.js
|
|
1
|
+
// src/auth/apiClient.js (in your core library)
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
import { CSRF_URL } from "./authConfig";
|
|
4
4
|
|
|
@@ -16,41 +16,46 @@ function isBrowser() {
|
|
|
16
16
|
|
|
17
17
|
function redirectToLoginOnce() {
|
|
18
18
|
if (!isBrowser()) return;
|
|
19
|
-
|
|
20
19
|
const onLogin = window.location.pathname.startsWith("/login");
|
|
21
20
|
if (onLogin) return;
|
|
22
|
-
|
|
23
21
|
if (redirectingToLogin) return;
|
|
24
22
|
redirectingToLogin = true;
|
|
25
|
-
|
|
26
23
|
window.location.assign("/login");
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
function isAuthish403(error) {
|
|
30
|
-
const status = error?.response?.status ?? null;
|
|
31
|
-
if (status !== 403) return false;
|
|
32
|
-
|
|
33
|
-
const data = error?.response?.data ?? {};
|
|
34
|
-
const detail = typeof data?.detail === "string" ? data.detail : "";
|
|
35
|
-
|
|
36
|
-
// Typical auth/session/CSRF related phrases (keep broad but not too broad)
|
|
37
|
-
return /csrf|authentication|credentials|not authenticated|session/i.test(detail);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
26
|
apiClient.interceptors.response.use(
|
|
41
27
|
(response) => response,
|
|
42
28
|
(error) => {
|
|
43
29
|
const status = error?.response?.status ?? null;
|
|
30
|
+
const data = error?.response?.data ?? {};
|
|
31
|
+
const detail = typeof data.detail === "string" ? data.detail : "";
|
|
44
32
|
|
|
45
|
-
|
|
33
|
+
const isStandard401 = status === 401;
|
|
34
|
+
const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
|
|
35
|
+
|
|
36
|
+
if (isStandard401 || isDrfAuth403) {
|
|
46
37
|
redirectToLoginOnce();
|
|
47
38
|
}
|
|
48
|
-
|
|
49
39
|
return Promise.reject(error);
|
|
50
40
|
}
|
|
51
41
|
);
|
|
52
42
|
|
|
43
|
+
// --- NEW HELPER ---
|
|
44
|
+
function getCookie(name) {
|
|
45
|
+
if (!isBrowser() || !document.cookie) return null;
|
|
46
|
+
const xsrfCookies = document.cookie.split(';')
|
|
47
|
+
.map(c => c.trim())
|
|
48
|
+
.filter(c => c.startsWith(name + '='));
|
|
49
|
+
if (xsrfCookies.length === 0) return null;
|
|
50
|
+
return decodeURIComponent(xsrfCookies[0].split('=')[1]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
53
|
export async function ensureCsrfToken() {
|
|
54
|
+
// OPTIMIZATION: If we already have the token, skip the network request
|
|
55
|
+
if (getCookie("csrftoken")) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
54
59
|
try {
|
|
55
60
|
await apiClient.get(CSRF_URL);
|
|
56
61
|
} catch (err) {
|
|
@@ -59,4 +64,4 @@ export async function ensureCsrfToken() {
|
|
|
59
64
|
}
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
export default apiClient;
|
|
67
|
+
export default apiClient;
|