Add Stock catalog (items/warehouses) and fix session persistence

Stock is scoped to a catalog + cost centers only, per direction that
physical GRV/stock-take/recipe transactions belong in an external
system (Matrix), not this accounting app. Stock Control/GRN/Variance/
PPV GL accounts are now plain postable accounts rather than locked
control accounts.

Also fixes session loss on page refresh: auth stores now persist to
localStorage, and ProtectedRoute waits for rehydration before deciding
to redirect to login.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JWqiUSojMLUoN7mrJqdEbN
This commit is contained in:
Joshua Oosthuizen
2026-09-04 10:35:04 +02:00
co-authored by Claude Sonnet 5
parent 4acbc03b35
commit b137176c40
35 changed files with 1199 additions and 46 deletions
@@ -1,8 +1,27 @@
import { useEffect, useState } from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import { Box, CircularProgress } from '@mui/material';
import { useAuth } from '../hooks/useAuth';
import { useAuthStore } from '../store/authStore';
export function ProtectedRoute() {
const { isAuthenticated } = useAuth();
// Wait for the persisted session to rehydrate from localStorage before deciding to redirect —
// otherwise a page reload always bounces to /login for a frame before the session loads.
const [hasHydrated, setHasHydrated] = useState(useAuthStore.persist.hasHydrated());
useEffect(() => {
if (hasHydrated) return;
return useAuthStore.persist.onFinishHydration(() => setHasHydrated(true));
}, [hasHydrated]);
if (!hasHydrated) {
return (
<Box sx={{ display: 'flex', minHeight: '100vh', alignItems: 'center', justifyContent: 'center' }}>
<CircularProgress />
</Box>
);
}
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
}
+15 -10
View File
@@ -1,4 +1,5 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
export interface AuthPerson {
id: number;
@@ -16,13 +17,17 @@ interface AuthState {
clearSession: () => void;
}
// Tokens kept in memory only, same rationale as the company app: limits XSS exposure at the
// cost of needing to sign in again on a hard page reload.
export const useAuthStore = create<AuthState>((set) => ({
accessToken: null,
refreshToken: null,
person: null,
setSession: ({ accessToken, refreshToken, person }) => set({ accessToken, refreshToken, person }),
setAccessToken: (accessToken) => set({ accessToken }),
clearSession: () => set({ accessToken: null, refreshToken: null, person: null }),
}));
// Persisted to localStorage so a page reload doesn't force signing in again.
export const useAuthStore = create<AuthState>()(
persist(
(set) => ({
accessToken: null,
refreshToken: null,
person: null,
setSession: ({ accessToken, refreshToken, person }) => set({ accessToken, refreshToken, person }),
setAccessToken: (accessToken) => set({ accessToken }),
clearSession: () => set({ accessToken: null, refreshToken: null, person: null }),
}),
{ name: 'account-manager-auth-session' },
),
);