@micha.bigler/ui-core-micha 2.1.3 → 2.1.4
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
CHANGED
|
@@ -1,10 +1,43 @@
|
|
|
1
1
|
// src/auth/apiClient.js
|
|
2
|
-
import axios from
|
|
3
|
-
import { CSRF_URL } from
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import { CSRF_URL } from "./authConfig";
|
|
4
4
|
const apiClient = axios.create({
|
|
5
5
|
withCredentials: true,
|
|
6
|
-
xsrfCookieName:
|
|
7
|
-
xsrfHeaderName:
|
|
6
|
+
xsrfCookieName: "csrftoken",
|
|
7
|
+
xsrfHeaderName: "X-CSRFToken",
|
|
8
|
+
});
|
|
9
|
+
let redirectingToLogin = false;
|
|
10
|
+
function isBrowser() {
|
|
11
|
+
return typeof window !== "undefined";
|
|
12
|
+
}
|
|
13
|
+
function redirectToLoginOnce() {
|
|
14
|
+
if (!isBrowser())
|
|
15
|
+
return;
|
|
16
|
+
const onLogin = window.location.pathname.startsWith("/login");
|
|
17
|
+
if (onLogin)
|
|
18
|
+
return;
|
|
19
|
+
if (redirectingToLogin)
|
|
20
|
+
return;
|
|
21
|
+
redirectingToLogin = true;
|
|
22
|
+
window.location.assign("/login");
|
|
23
|
+
}
|
|
24
|
+
function isAuthish403(error) {
|
|
25
|
+
var _a, _b, _c, _d;
|
|
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
|
+
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 (data === null || data === void 0 ? void 0 : data.detail) === "string" ? data.detail : "";
|
|
31
|
+
// Typical auth/session/CSRF related phrases (keep broad but not too broad)
|
|
32
|
+
return /csrf|authentication|credentials|not authenticated|session/i.test(detail);
|
|
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)) {
|
|
38
|
+
redirectToLoginOnce();
|
|
39
|
+
}
|
|
40
|
+
return Promise.reject(error);
|
|
8
41
|
});
|
|
9
42
|
export async function ensureCsrfToken() {
|
|
10
43
|
try {
|
|
@@ -1018,8 +1018,8 @@ export const authTranslations = {
|
|
|
1018
1018
|
"en": "User not logged in."
|
|
1019
1019
|
},
|
|
1020
1020
|
"Auth.INVITE_BUTTON": {
|
|
1021
|
-
"de": "
|
|
1022
|
-
"fr": "
|
|
1023
|
-
"en": "
|
|
1021
|
+
"de": "Einladen",
|
|
1022
|
+
"fr": "Inviter",
|
|
1023
|
+
"en": "Invite"
|
|
1024
1024
|
},
|
|
1025
1025
|
};
|
package/package.json
CHANGED
package/src/auth/apiClient.jsx
CHANGED
|
@@ -1,13 +1,55 @@
|
|
|
1
1
|
// src/auth/apiClient.js
|
|
2
|
-
import axios from
|
|
3
|
-
import { CSRF_URL } from
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import { CSRF_URL } from "./authConfig";
|
|
4
4
|
|
|
5
5
|
const apiClient = axios.create({
|
|
6
6
|
withCredentials: true,
|
|
7
|
-
xsrfCookieName:
|
|
8
|
-
xsrfHeaderName:
|
|
7
|
+
xsrfCookieName: "csrftoken",
|
|
8
|
+
xsrfHeaderName: "X-CSRFToken",
|
|
9
9
|
});
|
|
10
10
|
|
|
11
|
+
let redirectingToLogin = false;
|
|
12
|
+
|
|
13
|
+
function isBrowser() {
|
|
14
|
+
return typeof window !== "undefined";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function redirectToLoginOnce() {
|
|
18
|
+
if (!isBrowser()) return;
|
|
19
|
+
|
|
20
|
+
const onLogin = window.location.pathname.startsWith("/login");
|
|
21
|
+
if (onLogin) return;
|
|
22
|
+
|
|
23
|
+
if (redirectingToLogin) return;
|
|
24
|
+
redirectingToLogin = true;
|
|
25
|
+
|
|
26
|
+
window.location.assign("/login");
|
|
27
|
+
}
|
|
28
|
+
|
|
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
|
+
apiClient.interceptors.response.use(
|
|
41
|
+
(response) => response,
|
|
42
|
+
(error) => {
|
|
43
|
+
const status = error?.response?.status ?? null;
|
|
44
|
+
|
|
45
|
+
if (status === 401 || isAuthish403(error)) {
|
|
46
|
+
redirectToLoginOnce();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return Promise.reject(error);
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
|
|
11
53
|
export async function ensureCsrfToken() {
|
|
12
54
|
try {
|
|
13
55
|
await apiClient.get(CSRF_URL);
|
|
@@ -17,4 +59,4 @@ export async function ensureCsrfToken() {
|
|
|
17
59
|
}
|
|
18
60
|
}
|
|
19
61
|
|
|
20
|
-
export default apiClient;
|
|
62
|
+
export default apiClient;
|
|
@@ -1066,9 +1066,9 @@ export const authTranslations = {
|
|
|
1066
1066
|
"en": "User not logged in."
|
|
1067
1067
|
},
|
|
1068
1068
|
"Auth.INVITE_BUTTON": {
|
|
1069
|
-
"de": "
|
|
1070
|
-
"fr": "
|
|
1071
|
-
"en": "
|
|
1069
|
+
"de": "Einladen",
|
|
1070
|
+
"fr": "Inviter",
|
|
1071
|
+
"en": "Invite"
|
|
1072
1072
|
},
|
|
1073
1073
|
|
|
1074
1074
|
}
|