@autobusal/providers 1.2.10 → 1.2.12

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/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ All notable changes to `@autobusal/providers` are documented here. This project
4
4
  adheres to [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
5
5
  [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.2.12] - 2026-07-24
8
+
9
+ ### Fixed
10
+
11
+ - The full user profile - name, email, and via the nested admin/operator/agent/subagent/employee/driver/visitor sub-object: phone, address, passport, date of birth, company/NIPT - was persisted verbatim to localStorage, readable at rest by any script on the origin. Only a display-only subset (id, name, name_display, type, status) is now persisted; the full object still lives in-memory during an active session exactly as before. A new bootstrap fetch (wired into Setup) re-hydrates the full profile once per app load using the same reduced signal, guarded so it never fires for a genuine guest.
12
+
13
+ ## [1.2.11] - 2026-07-19
14
+
15
+ ### Fixed
16
+
17
+ - bff-mode 401 no longer logs the user out when the BFF confirms the session is still valid: the apiClient now rejects the failed request (letting react-query retry) instead of tearing down auth. A single account query 401ing (transient race or a flaky upstream call) previously bounced the user to /login even though they were logged in. Bearer mode unchanged.
7
18
  ## [1.2.10] - 2026-07-19
8
19
 
9
20
  ### Fixed
@@ -43,22 +43,21 @@ apiClient.interceptors.response.use(response => (
43
43
  if (error.response) {
44
44
  // if we have a 401, the session is gone, so we go to the logout page.
45
45
  if (error.response.status === 401 && window.location.pathname !== '/account/logout') {
46
- // bff mode: auth is a server-side httpOnly cookie the BFF manages. A
47
- // request that races the login/cookie hand-off (e.g. the account queries
48
- // fired the instant the SPA lands on /account) can 401 transiently while
49
- // the session is actually valid - and a blind logout here would drop the
50
- // user straight back out. So confirm with the BFF first; if we are still
51
- // authenticated, transparently retry the request once instead of logging
52
- // out. bearer mode has no such race (the token is attached synchronously
53
- // from localStorage) and falls straight through to logout.
54
- if (isBffMode && error.config && !error.config._bffAuthRetried) {
46
+ // bff mode: auth is a server-side httpOnly cookie the BFF manages, so a
47
+ // single request's 401 does NOT reliably mean "logged out" the way a
48
+ // bearer 401 does - a query that momentarily races the login/cookie
49
+ // hand-off (or a flaky upstream call) can 401 while the session is still
50
+ // valid. A blind logout here drops the user straight back to /login. So
51
+ // confirm with the BFF: only tear the session down if it agrees we are no
52
+ // longer authenticated; otherwise just reject and let react-query retry -
53
+ // the user stays logged in. bearer mode has no such ambiguity (the token
54
+ // is attached synchronously from localStorage) and logs out immediately.
55
+ if (isBffMode) {
55
56
  try {
56
57
  const { data } = await apiClient.get('/session');
57
58
 
58
59
  if (data?.authenticated) {
59
- error.config._bffAuthRetried = true;
60
-
61
- return await apiClient(error.config);
60
+ return Promise.reject(error);
62
61
  }
63
62
  } catch {
64
63
  // /session unreachable - fall through to logout.
package/Setup/Setup.tsx CHANGED
@@ -1,6 +1,7 @@
1
1
  import Preload from './Preload';
2
2
  import languages from './languages';
3
3
  import analytics from './analytics';
4
+ import useUserBootstrap from './useUserBootstrap';
4
5
  import { useGetSettings, useGetMenu } from '../services';
5
6
 
6
7
  interface Props {
@@ -12,6 +13,7 @@ const Setup = ({ type, children }: Props): JSX.Element => {
12
13
  const { data } = useGetSettings();
13
14
 
14
15
  useGetMenu();
16
+ useUserBootstrap();
15
17
 
16
18
  languages(type, data.preferences.languages.default);
17
19
 
@@ -0,0 +1,52 @@
1
+ import { useEffect } from 'react';
2
+ import { useQuery } from '@tanstack/react-query';
3
+ import apiClient, { isBffMode } from '../Queries/apiClient';
4
+ import { useUserStore } from '../stores/user';
5
+ import { UserData } from '../types/users';
6
+
7
+ // Edited: Ferjolt Ozuni - Date: 2026-07-24
8
+ // The user store now only persists a display-only subset to localStorage
9
+ // (see stores/user.ts) - the full profile (nested role data: phone, address,
10
+ // passport, company, etc.) is no longer restored on a cold page load. This
11
+ // hook re-fetches it once per app mount, so any component reading
12
+ // `data.visitor.phone` etc. gets it back a moment after load instead of
13
+ // instantly - the only user-visible change from this fix.
14
+ //
15
+ // This must NOT fire for a genuine guest: apiClient's response interceptor
16
+ // treats any 401 as "session is gone" and redirects to /account/logout
17
+ // (bearer mode immediately, bff mode after confirming via /session) - firing
18
+ // this unconditionally would send every anonymous visitor there. It only
19
+ // runs when there's an actual signal of a prior login: the bearer token
20
+ // (bearer mode) or the redacted user stub this same store persists (bff
21
+ // mode, where the token itself is an httpOnly cookie invisible to JS).
22
+ const hasLoginSignal = (): boolean => (
23
+ (!isBffMode && localStorage.getItem('token') !== null)
24
+ || localStorage.getItem('user') !== null
25
+ );
26
+
27
+ const useUserBootstrap = (): void => {
28
+ const user = useUserStore();
29
+
30
+ const enabled = hasLoginSignal();
31
+
32
+ const { data } = useQuery<UserData>({
33
+ queryKey: ['account-bootstrap'],
34
+ queryFn: async () => (
35
+ await apiClient
36
+ .get('/api/account/refresh')
37
+ .then(response => response.data)
38
+ ),
39
+ enabled,
40
+ retry: false,
41
+ staleTime: Infinity
42
+ });
43
+
44
+ useEffect(() => {
45
+ if (data) {
46
+ user.save(data);
47
+ }
48
+ // eslint-disable-next-line react-hooks/exhaustive-deps
49
+ }, [data]);
50
+ };
51
+
52
+ export default useUserBootstrap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autobusal/providers",
3
- "version": "1.2.10",
3
+ "version": "1.2.12",
4
4
  "author": "Ferjolt Ozuni",
5
5
  "type": "module",
6
6
  "main": "index.ts"
package/stores/user.ts CHANGED
@@ -7,19 +7,45 @@ export interface UserStore {
7
7
  remove: () => void
8
8
  }
9
9
 
10
- const initial = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') ?? '{}') : undefined;
10
+ // Edited: Ferjolt Ozuni - Date: 2026-07-24
11
+ // Bug: the full UserData object - name, email, and (via the nested
12
+ // admin/operator/agent/subagent/employee/driver/visitor sub-object) phone,
13
+ // address, passport, date of birth, company/NIPT - was persisted verbatim to
14
+ // localStorage, readable at rest by any script on the origin (any XSS,
15
+ // a malicious browser extension, disk/backup access on a shared machine).
16
+ // Fix: only a display-only subset - enough to paint the UI immediately on
17
+ // load - is ever written to disk. The full object still lives in the
18
+ // in-memory store during an active session exactly as before (nothing
19
+ // changes for any component reading `data.visitor.phone` etc. right after
20
+ // login/activate/refresh); it's just never written to disk. A bootstrap
21
+ // fetch (see Setup/useUserBootstrap.ts) re-hydrates the full in-memory
22
+ // object shortly after app load using this same reduced signal.
23
+ type PersistedUser = Pick<UserData, 'id' | 'name' | 'name_display' | 'type' | 'status'>;
24
+
25
+ const STORAGE_KEY = 'user';
26
+
27
+ const redact = (user: UserData): PersistedUser => ({
28
+ id: user.id,
29
+ name: user.name,
30
+ name_display: user.name_display,
31
+ type: user.type,
32
+ status: user.status
33
+ });
34
+
35
+ const stored = localStorage.getItem(STORAGE_KEY);
36
+ const initial: PersistedUser | undefined = stored ? JSON.parse(stored) : undefined;
11
37
 
12
38
  export const useUserStore = create<UserStore>((set) => ({
13
- data: initial,
39
+ data: initial as UserData | undefined,
14
40
  save: (user) => set(() => {
15
- localStorage.setItem('user', JSON.stringify(user));
41
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(redact(user)));
16
42
 
17
43
  return {
18
44
  data: user
19
45
  };
20
46
  }),
21
47
  remove: () => set(() => {
22
- localStorage.removeItem('user');
48
+ localStorage.removeItem(STORAGE_KEY);
23
49
 
24
50
  return {
25
51
  data: undefined