@micha.bigler/ui-core-micha 2.1.11 → 2.1.13

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.
@@ -42,14 +42,51 @@ function redirectToLoginOnce() {
42
42
  window.location.assign("/login");
43
43
  }
44
44
  }
45
+ function extractAuthSignal(data) {
46
+ if (!data) {
47
+ return { code: null, i18nKey: null };
48
+ }
49
+ if (typeof data === "string") {
50
+ try {
51
+ const parsed = JSON.parse(data);
52
+ return extractAuthSignal(parsed);
53
+ }
54
+ catch (_a) {
55
+ return { code: null, i18nKey: null };
56
+ }
57
+ }
58
+ if (typeof data !== "object") {
59
+ return { code: null, i18nKey: null };
60
+ }
61
+ if (typeof data.code === "string" || typeof data.i18nKey === "string") {
62
+ return {
63
+ code: typeof data.code === "string" ? data.code : null,
64
+ i18nKey: typeof data.i18nKey === "string" ? data.i18nKey : null,
65
+ };
66
+ }
67
+ if (Array.isArray(data.errors) && data.errors.length > 0) {
68
+ const first = data.errors[0] || {};
69
+ return {
70
+ code: typeof first.code === "string" ? first.code : null,
71
+ i18nKey: typeof first.i18nKey === "string" ? first.i18nKey : null,
72
+ };
73
+ }
74
+ if (data.detail && typeof data.detail === "object") {
75
+ return {
76
+ code: typeof data.detail.code === "string" ? data.detail.code : null,
77
+ i18nKey: typeof data.detail.i18nKey === "string" ? data.detail.i18nKey : null,
78
+ };
79
+ }
80
+ return { code: null, i18nKey: null };
81
+ }
45
82
  apiClient.interceptors.response.use((response) => response, (error) => {
46
83
  var _a, _b, _c, _d;
47
84
  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;
48
85
  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 : {};
49
- const detail = typeof data.detail === "string" ? data.detail : "";
50
- const isStandard401 = status === 401;
51
- const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
52
- if (isStandard401 || isDrfAuth403) {
86
+ const { code, i18nKey } = extractAuthSignal(data);
87
+ const isAuthStatus = status === 401 || status === 403;
88
+ const isNotAuthenticated = code === "not_authenticated" || i18nKey === "auth.not_authenticated";
89
+ if (isAuthStatus && isNotAuthenticated) {
53
90
  redirectToLoginOnce();
54
91
  }
55
92
  return Promise.reject(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micha.bigler/ui-core-micha",
3
- "version": "2.1.11",
3
+ "version": "2.1.13",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "private": false,
@@ -50,17 +50,61 @@ function redirectToLoginOnce() {
50
50
  }
51
51
  }
52
52
 
53
+ function extractAuthSignal(data) {
54
+ if (!data) {
55
+ return { code: null, i18nKey: null };
56
+ }
57
+
58
+ if (typeof data === "string") {
59
+ try {
60
+ const parsed = JSON.parse(data);
61
+ return extractAuthSignal(parsed);
62
+ } catch {
63
+ return { code: null, i18nKey: null };
64
+ }
65
+ }
66
+
67
+ if (typeof data !== "object") {
68
+ return { code: null, i18nKey: null };
69
+ }
70
+
71
+ if (typeof data.code === "string" || typeof data.i18nKey === "string") {
72
+ return {
73
+ code: typeof data.code === "string" ? data.code : null,
74
+ i18nKey: typeof data.i18nKey === "string" ? data.i18nKey : null,
75
+ };
76
+ }
77
+
78
+ if (Array.isArray(data.errors) && data.errors.length > 0) {
79
+ const first = data.errors[0] || {};
80
+ return {
81
+ code: typeof first.code === "string" ? first.code : null,
82
+ i18nKey: typeof first.i18nKey === "string" ? first.i18nKey : null,
83
+ };
84
+ }
85
+
86
+ if (data.detail && typeof data.detail === "object") {
87
+ return {
88
+ code: typeof data.detail.code === "string" ? data.detail.code : null,
89
+ i18nKey: typeof data.detail.i18nKey === "string" ? data.detail.i18nKey : null,
90
+ };
91
+ }
92
+
93
+ return { code: null, i18nKey: null };
94
+ }
95
+
53
96
  apiClient.interceptors.response.use(
54
97
  (response) => response,
55
98
  (error) => {
56
99
  const status = error?.response?.status ?? null;
57
100
  const data = error?.response?.data ?? {};
58
- const detail = typeof data.detail === "string" ? data.detail : "";
101
+ const { code, i18nKey } = extractAuthSignal(data);
59
102
 
60
- const isStandard401 = status === 401;
61
- const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
103
+ const isAuthStatus = status === 401 || status === 403;
104
+ const isNotAuthenticated =
105
+ code === "not_authenticated" || i18nKey === "auth.not_authenticated";
62
106
 
63
- if (isStandard401 || isDrfAuth403) {
107
+ if (isAuthStatus && isNotAuthenticated) {
64
108
  redirectToLoginOnce();
65
109
  }
66
110
  return Promise.reject(error);
@@ -87,4 +131,4 @@ export async function ensureCsrfToken() {
87
131
  }
88
132
  }
89
133
 
90
- export default apiClient;
134
+ export default apiClient;