@micha.bigler/ui-core-micha 1.4.14 → 1.4.17

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,38 +1,23 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  // src/auth/AuthContext.jsx
3
3
  import React, { createContext, useState, useEffect, } from 'react';
4
- import axios from 'axios';
5
- import { CSRF_URL } from './authConfig';
4
+ // REMOVED: import axios from 'axios'; -> Not needed here anymore
5
+ import { ensureCsrfToken } from './apiClient'; // <--- IMPORT ADDED
6
6
  import { fetchCurrentUser, logoutSession, } from './authApi';
7
7
  export const AuthContext = createContext(null);
8
8
  export const AuthProvider = ({ children }) => {
9
9
  const [user, setUser] = useState(null);
10
10
  const [loading, setLoading] = useState(true);
11
- // Einmalige Axios-Basis-Konfiguration
12
- useEffect(() => {
13
- axios.defaults.withCredentials = true;
14
- axios.defaults.xsrfCookieName = 'csrftoken';
15
- axios.defaults.xsrfHeaderName = 'X-CSRFToken';
16
- }, []);
17
11
  useEffect(() => {
18
12
  let isMounted = true;
19
13
  const initAuth = async () => {
20
- var _a;
21
14
  try {
22
- // 1) CSRF-Cookie setzen (Django-View /api/csrf/)
23
- try {
24
- await axios.get(CSRF_URL, { withCredentials: true });
25
- // console.log('CSRF cookie set');
26
- }
27
- catch (err) {
28
- // eslint-disable-next-line no-console
29
- console.error('Error setting CSRF cookie:', err);
30
- }
31
- // 2) aktuellen User laden (falls Session vorhanden)
32
- try {
33
- const data = await fetchCurrentUser();
34
- if (!isMounted)
35
- return;
15
+ // 1) Ensure CSRF cookie exists using the specific client
16
+ await ensureCsrfToken();
17
+ // 2) Load user
18
+ const data = await fetchCurrentUser();
19
+ if (isMounted) {
20
+ // Map data to ensure consistent structure
36
21
  setUser({
37
22
  id: data.id,
38
23
  username: data.username,
@@ -41,38 +26,26 @@ export const AuthProvider = ({ children }) => {
41
26
  last_name: data.last_name,
42
27
  role: data.role,
43
28
  is_superuser: data.is_superuser,
29
+ security_state: data.security_state, // Ensure this is passed if needed
44
30
  });
45
31
  }
46
- catch (err) {
47
- const status = (_a = err.response) === null || _a === void 0 ? void 0 : _a.status;
48
- if (status && status !== 401) {
49
- console.error('Error while fetching current user:', err);
50
- }
51
- else {
52
- console.log('No logged-in user');
53
- }
54
- if (!isMounted)
55
- return;
32
+ }
33
+ catch (err) {
34
+ // Silent failure on 401/403 is expected (user not logged in)
35
+ if (isMounted)
56
36
  setUser(null);
57
- }
58
37
  }
59
38
  finally {
60
- if (isMounted) {
39
+ if (isMounted)
61
40
  setLoading(false);
62
- }
63
41
  }
64
42
  };
65
43
  initAuth();
66
- return () => {
67
- isMounted = false;
68
- };
44
+ return () => { isMounted = false; };
69
45
  }, []);
70
- // Nach erfolgreichem Login das User-Objekt setzen
71
- // (z. B. aus loginWithPassword in authApi)
72
46
  const login = (userData) => {
73
47
  setUser((prev) => (Object.assign(Object.assign({}, prev), userData)));
74
48
  };
75
- // Logout im Backend + lokalen State leeren
76
49
  const logout = async () => {
77
50
  try {
78
51
  await logoutSession();
@@ -0,0 +1,18 @@
1
+ // src/auth/apiClient.js
2
+ import axios from 'axios';
3
+ import { CSRF_URL } from './authConfig';
4
+ const apiClient = axios.create({
5
+ withCredentials: true,
6
+ xsrfCookieName: 'csrftoken',
7
+ xsrfHeaderName: 'X-CSRFToken',
8
+ });
9
+ export async function ensureCsrfToken() {
10
+ try {
11
+ await apiClient.get(CSRF_URL);
12
+ }
13
+ catch (err) {
14
+ // eslint-disable-next-line no-console
15
+ console.warn("CSRF Initialization failed", err);
16
+ }
17
+ }
18
+ export default apiClient;