@micha.bigler/ui-core-micha 2.1.3 → 2.1.5

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,10 +1,43 @@
1
1
  // src/auth/apiClient.js
2
- import axios from 'axios';
3
- import { CSRF_URL } from './authConfig';
2
+ import axios from "axios";
3
+ import { CSRF_URL } from "./authConfig";
4
4
  const apiClient = axios.create({
5
5
  withCredentials: true,
6
- xsrfCookieName: 'csrftoken',
7
- xsrfHeaderName: 'X-CSRFToken',
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 {
@@ -21,6 +21,6 @@ disabled = false, initialIdentifier = '', }) {
21
21
  const supportsPasskey = !!onPasskeyLogin &&
22
22
  typeof window !== 'undefined' &&
23
23
  !!window.PublicKeyCredential;
24
- return (_jsxs(Box, { sx: { display: 'flex', flexDirection: 'column', gap: 3 }, children: [error && (_jsx(Typography, { color: "error", gutterBottom: true, children: error })), supportsPasskey && (_jsxs(_Fragment, { children: [_jsx(Button, { variant: "contained", fullWidth: true, type: "button", onClick: onPasskeyLogin, disabled: disabled, children: t('Auth.LOGIN_USE_PASSKEY_BUTTON') }), _jsx(Divider, { sx: { my: 2 }, children: t('Auth.LOGIN_OR') })] })), _jsxs(Box, { component: "form", onSubmit: handleSubmit, sx: { display: 'flex', flexDirection: 'column', gap: 2 }, children: [_jsx(TextField, { label: t('Auth.EMAIL_LABEL'), type: "email", required: true, fullWidth: true, value: identifier, onChange: (e) => setIdentifier(e.target.value), disabled: disabled }), _jsx(TextField, { label: t('Auth.LOGIN_PASSWORD_LABEL'), type: "password", required: true, fullWidth: true, value: password, onChange: (e) => setPassword(e.target.value), disabled: disabled }), _jsx(Button, { type: "submit", variant: "contained", fullWidth: true, disabled: disabled, children: t('Auth.PAGE_LOGIN_TITLE') })] }), _jsxs(Box, { children: [_jsx(Divider, { sx: { my: 2 }, children: t('Auth.LOGIN_OR') }), _jsx(SocialLoginButtons, { onProviderClick: onSocialLogin })] }), _jsxs(Box, { children: [_jsx(Typography, { variant: "subtitle2", sx: { mb: 1 }, children: t('Auth.LOGIN_ACCOUNT_RECOVERY_TITLE') }), _jsxs(Box, { sx: { display: 'flex', gap: 1, flexWrap: 'wrap' }, children: [onSignUp && (_jsx(Button, { type: "button", variant: "outlined", onClick: onSignUp, disabled: disabled, children: t('Auth.LOGIN_SIGNUP_BUTTON') })), _jsx(Button, { type: "button", variant: "outlined", onClick: onForgotPassword, disabled: disabled, children: t('Auth.LOGIN_FORGOT_PASSWORD_BUTTON') })] })] })] }));
24
+ return (_jsxs(Box, { sx: { display: 'flex', flexDirection: 'column', gap: 3 }, children: [error && (_jsx(Typography, { color: "error", gutterBottom: true, children: error })), supportsPasskey && (_jsxs(_Fragment, { children: [_jsx(Button, { variant: "contained", fullWidth: true, type: "button", onClick: onPasskeyLogin, disabled: disabled, children: t('Auth.LOGIN_USE_PASSKEY_BUTTON') }), _jsx(Divider, { sx: { my: 2 }, children: t('Auth.LOGIN_OR') })] })), _jsxs(Box, { component: "form", onSubmit: handleSubmit, sx: { display: 'flex', flexDirection: 'column', gap: 2 }, children: [_jsx(TextField, { label: t('Auth.EMAIL_LABEL'), type: "email", required: true, fullWidth: true, value: identifier, onChange: (e) => setIdentifier(e.target.value), disabled: disabled }), _jsx(TextField, { label: t('Auth.LOGIN_PASSWORD_LABEL'), type: "password", required: true, fullWidth: true, value: password, onChange: (e) => setPassword(e.target.value), disabled: disabled }), _jsx(Button, { type: "submit", variant: "contained", fullWidth: true, disabled: disabled, children: t('Auth.PAGE_LOGIN_TITLE') })] }), _jsxs(Box, { children: [_jsx(Typography, { variant: "subtitle2", sx: { mb: 1 }, children: t('Auth.LOGIN_ACCOUNT_RECOVERY_TITLE') }), _jsxs(Box, { sx: { display: 'flex', gap: 1, flexWrap: 'wrap' }, children: [onSignUp && (_jsx(Button, { type: "button", variant: "outlined", onClick: onSignUp, disabled: disabled, children: t('Auth.LOGIN_SIGNUP_BUTTON') })), _jsx(Button, { type: "button", variant: "outlined", onClick: onForgotPassword, disabled: disabled, children: t('Auth.LOGIN_FORGOT_PASSWORD_BUTTON') })] })] })] }));
25
25
  }
26
26
  ;
@@ -1018,8 +1018,8 @@ export const authTranslations = {
1018
1018
  "en": "User not logged in."
1019
1019
  },
1020
1020
  "Auth.INVITE_BUTTON": {
1021
- "de": "Einladung senden",
1022
- "fr": "Envoyer l'invitation",
1023
- "en": "Send invitation"
1021
+ "de": "Einladen",
1022
+ "fr": "Inviter",
1023
+ "en": "Invite"
1024
1024
  },
1025
1025
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micha.bigler/ui-core-micha",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "private": false,
@@ -1,13 +1,55 @@
1
1
  // src/auth/apiClient.js
2
- import axios from 'axios';
3
- import { CSRF_URL } from './authConfig';
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: 'csrftoken',
8
- xsrfHeaderName: 'X-CSRFToken',
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;
@@ -104,14 +104,16 @@ export function LoginForm({
104
104
  </Box>
105
105
 
106
106
  {/* Other ways to sign in */}
107
+ {/*
107
108
  <Box>
108
109
  <Divider sx={{ my: 2 }}>
109
110
  {t('Auth.LOGIN_OR')}
110
111
  </Divider>
111
112
  <SocialLoginButtons onProviderClick={onSocialLogin} />
112
113
  </Box>
113
-
114
+ */}
114
115
  {/* Account & Recovery */}
116
+
115
117
  <Box>
116
118
  <Typography variant="subtitle2" sx={{ mb: 1 }}>
117
119
  {t('Auth.LOGIN_ACCOUNT_RECOVERY_TITLE')}
@@ -1066,9 +1066,9 @@ export const authTranslations = {
1066
1066
  "en": "User not logged in."
1067
1067
  },
1068
1068
  "Auth.INVITE_BUTTON": {
1069
- "de": "Einladung senden",
1070
- "fr": "Envoyer l'invitation",
1071
- "en": "Send invitation"
1069
+ "de": "Einladen",
1070
+ "fr": "Inviter",
1071
+ "en": "Invite"
1072
1072
  },
1073
1073
 
1074
1074
  }