@micha.bigler/ui-core-micha 2.1.6 → 2.1.8

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.
@@ -1,4 +1,3 @@
1
- // src/auth/apiClient.js
2
1
  import axios from "axios";
3
2
  import { CSRF_URL } from "./authConfig";
4
3
  const apiClient = axios.create({
@@ -10,41 +9,69 @@ let redirectingToLogin = false;
10
9
  function isBrowser() {
11
10
  return typeof window !== "undefined";
12
11
  }
12
+ // WICHTIG: Liste aller Routen, die OHNE Login funktionieren müssen.
13
+ // Beginnt der Pfad mit einem dieser Strings, wird kein Auto-Redirect ausgelöst.
14
+ const PUBLIC_PATHS = [
15
+ "/login",
16
+ "/signup",
17
+ "/reset-request-password", // Request Page
18
+ "/invite", // Invite Link (/invite/:uid/:token)
19
+ "/reset", // Reset Link (/reset/:uid/:token)
20
+ "/welcome" // Optional: Falls Welcome auch öffentlich ist
21
+ ];
13
22
  function redirectToLoginOnce() {
14
23
  if (!isBrowser())
15
24
  return;
16
- const onLogin = window.location.pathname.startsWith("/login");
17
- if (onLogin)
25
+ const currentPath = window.location.pathname;
26
+ // 1. Check: Sind wir auf einer öffentlichen Seite?
27
+ const isPublicPage = PUBLIC_PATHS.some(path => currentPath.startsWith(path));
28
+ // Wenn ja: NICHT weiterleiten. Der 401 Fehler wird an die Komponente durchgereicht.
29
+ if (isPublicPage)
18
30
  return;
19
31
  if (redirectingToLogin)
20
32
  return;
21
33
  redirectingToLogin = true;
22
- window.location.assign("/login");
34
+ // Wir speichern die aktuelle Seite, um nach dem Login zurückzukehren
35
+ // (außer wir sind schon auf Login)
36
+ if (!currentPath.startsWith("/login")) {
37
+ // Optional: ?next=... Logik
38
+ // window.location.assign(`/login?next=${encodeURIComponent(currentPath)}`);
39
+ window.location.assign("/login");
40
+ }
41
+ else {
42
+ window.location.assign("/login");
43
+ }
23
44
  }
24
45
  apiClient.interceptors.response.use((response) => response, (error) => {
25
46
  var _a, _b, _c, _d;
26
47
  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
48
  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
49
  const detail = typeof data.detail === "string" ? data.detail : "";
29
- // 1. Fall: Klassisches 401 (Unauthorized)
30
50
  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
51
  const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
35
52
  if (isStandard401 || isDrfAuth403) {
36
53
  redirectToLoginOnce();
37
54
  }
38
- // Alle anderen Fehler (echte 403er, 500er, 404er) werden durchgereicht
39
- // und müssen von der UI (dem Aufrufer) behandelt werden.
40
55
  return Promise.reject(error);
41
56
  });
57
+ function getCookie(name) {
58
+ if (!isBrowser() || !document.cookie)
59
+ return null;
60
+ const xsrfCookies = document.cookie.split(';')
61
+ .map(c => c.trim())
62
+ .filter(c => c.startsWith(name + '='));
63
+ if (xsrfCookies.length === 0)
64
+ return null;
65
+ return decodeURIComponent(xsrfCookies[0].split('=')[1]);
66
+ }
42
67
  export async function ensureCsrfToken() {
68
+ if (getCookie("csrftoken")) {
69
+ return;
70
+ }
43
71
  try {
44
72
  await apiClient.get(CSRF_URL);
45
73
  }
46
74
  catch (err) {
47
- // eslint-disable-next-line no-console
48
75
  console.warn("CSRF Initialization failed", err);
49
76
  }
50
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micha.bigler/ui-core-micha",
3
- "version": "2.1.6",
3
+ "version": "2.1.8",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "private": false,
@@ -1,4 +1,3 @@
1
- // src/auth/apiClient.js
2
1
  import axios from "axios";
3
2
  import { CSRF_URL } from "./authConfig";
4
3
 
@@ -14,16 +13,41 @@ function isBrowser() {
14
13
  return typeof window !== "undefined";
15
14
  }
16
15
 
16
+ // WICHTIG: Liste aller Routen, die OHNE Login funktionieren müssen.
17
+ // Beginnt der Pfad mit einem dieser Strings, wird kein Auto-Redirect ausgelöst.
18
+ const PUBLIC_PATHS = [
19
+ "/login",
20
+ "/signup",
21
+ "/reset-request-password", // Request Page
22
+ "/invite", // Invite Link (/invite/:uid/:token)
23
+ "/reset", // Reset Link (/reset/:uid/:token)
24
+ "/welcome" // Optional: Falls Welcome auch öffentlich ist
25
+ ];
26
+
17
27
  function redirectToLoginOnce() {
18
28
  if (!isBrowser()) return;
19
29
 
20
- const onLogin = window.location.pathname.startsWith("/login");
21
- if (onLogin) return;
30
+ const currentPath = window.location.pathname;
31
+
32
+ // 1. Check: Sind wir auf einer öffentlichen Seite?
33
+ const isPublicPage = PUBLIC_PATHS.some(path => currentPath.startsWith(path));
34
+
35
+ // Wenn ja: NICHT weiterleiten. Der 401 Fehler wird an die Komponente durchgereicht.
36
+ if (isPublicPage) return;
22
37
 
23
38
  if (redirectingToLogin) return;
39
+
24
40
  redirectingToLogin = true;
25
-
26
- window.location.assign("/login");
41
+
42
+ // Wir speichern die aktuelle Seite, um nach dem Login zurückzukehren
43
+ // (außer wir sind schon auf Login)
44
+ if (!currentPath.startsWith("/login")) {
45
+ // Optional: ?next=... Logik
46
+ // window.location.assign(`/login?next=${encodeURIComponent(currentPath)}`);
47
+ window.location.assign("/login");
48
+ } else {
49
+ window.location.assign("/login");
50
+ }
27
51
  }
28
52
 
29
53
  apiClient.interceptors.response.use(
@@ -33,29 +57,32 @@ apiClient.interceptors.response.use(
33
57
  const data = error?.response?.data ?? {};
34
58
  const detail = typeof data.detail === "string" ? data.detail : "";
35
59
 
36
- // 1. Fall: Klassisches 401 (Unauthorized)
37
60
  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
61
  const isDrfAuth403 = status === 403 && detail === "Authentication credentials were not provided.";
43
62
 
44
63
  if (isStandard401 || isDrfAuth403) {
45
64
  redirectToLoginOnce();
46
65
  }
47
-
48
- // Alle anderen Fehler (echte 403er, 500er, 404er) werden durchgereicht
49
- // und müssen von der UI (dem Aufrufer) behandelt werden.
50
66
  return Promise.reject(error);
51
67
  }
52
68
  );
53
69
 
70
+ function getCookie(name) {
71
+ if (!isBrowser() || !document.cookie) return null;
72
+ const xsrfCookies = document.cookie.split(';')
73
+ .map(c => c.trim())
74
+ .filter(c => c.startsWith(name + '='));
75
+ if (xsrfCookies.length === 0) return null;
76
+ return decodeURIComponent(xsrfCookies[0].split('=')[1]);
77
+ }
78
+
54
79
  export async function ensureCsrfToken() {
80
+ if (getCookie("csrftoken")) {
81
+ return;
82
+ }
55
83
  try {
56
84
  await apiClient.get(CSRF_URL);
57
85
  } catch (err) {
58
- // eslint-disable-next-line no-console
59
86
  console.warn("CSRF Initialization failed", err);
60
87
  }
61
88
  }