@micha.bigler/ui-core-micha 2.1.6 → 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 +16 -7
- package/package.json +1 -1
- package/src/auth/apiClient.jsx +16 -12
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({
|
|
@@ -26,20 +26,29 @@ apiClient.interceptors.response.use((response) => response, (error) => {
|
|
|
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
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 : {};
|
|
28
28
|
const detail = typeof data.detail === "string" ? data.detail : "";
|
|
29
|
-
// 1. Fall: Klassisches 401 (Unauthorized)
|
|
30
29
|
const isStandard401 = status === 401;
|
|
31
|
-
// 2. Fall: Django DRF "Fake" 403
|
|
32
|
-
// DRF sendet 403 statt 401 bei SessionAuth, aber mit diesem spezifischen Text.
|
|
33
|
-
// Wir prüfen exakt auf diesen String, um echte 403 (Permission denied) nicht zu erwischen.
|
|
34
30
|
const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
|
|
35
31
|
if (isStandard401 || isDrfAuth403) {
|
|
36
32
|
redirectToLoginOnce();
|
|
37
33
|
}
|
|
38
|
-
// Alle anderen Fehler (echte 403er, 500er, 404er) werden durchgereicht
|
|
39
|
-
// und müssen von der UI (dem Aufrufer) behandelt werden.
|
|
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,13 +16,10 @@ 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
|
|
|
@@ -33,25 +30,32 @@ apiClient.interceptors.response.use(
|
|
|
33
30
|
const data = error?.response?.data ?? {};
|
|
34
31
|
const detail = typeof data.detail === "string" ? data.detail : "";
|
|
35
32
|
|
|
36
|
-
// 1. Fall: Klassisches 401 (Unauthorized)
|
|
37
33
|
const isStandard401 = status === 401;
|
|
38
|
-
|
|
39
|
-
// 2. Fall: Django DRF "Fake" 403
|
|
40
|
-
// DRF sendet 403 statt 401 bei SessionAuth, aber mit diesem spezifischen Text.
|
|
41
|
-
// Wir prüfen exakt auf diesen String, um echte 403 (Permission denied) nicht zu erwischen.
|
|
42
34
|
const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
|
|
43
35
|
|
|
44
36
|
if (isStandard401 || isDrfAuth403) {
|
|
45
37
|
redirectToLoginOnce();
|
|
46
38
|
}
|
|
47
|
-
|
|
48
|
-
// Alle anderen Fehler (echte 403er, 500er, 404er) werden durchgereicht
|
|
49
|
-
// und müssen von der UI (dem Aufrufer) behandelt werden.
|
|
50
39
|
return Promise.reject(error);
|
|
51
40
|
}
|
|
52
41
|
);
|
|
53
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
|
+
|
|
54
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
|
+
|
|
55
59
|
try {
|
|
56
60
|
await apiClient.get(CSRF_URL);
|
|
57
61
|
} catch (err) {
|