Public Access
refactor(session): one session store for login, logout and 401
This commit is contained in:
1 parent
2a22789435
commit
cbafa3d540
23 files changed
+246
-280
No files matched your search
+5
-15
@@ -1,6 +1,6 @@
|
||||
import {Link, Route, Routes, useLocation, useNavigate} from 'react-router-dom';
|
||||
import {useTranslation} from 'react-i18next';
|
||||
import {useEffect, useState} from 'react';
|
||||
import {useEffect} from 'react';
|
||||
|
||||
import LandingPage from './pages/LandingPage';
|
||||
import LoginPage from './pages/LoginPage';
|
||||
@@ -23,26 +23,19 @@ import {Toaster} from '@/components/ui/sonner';
|
||||
import {TooltipProvider} from '@/components/ui/tooltip';
|
||||
import PageBackground from './components/PageBackground.jsx';
|
||||
import {usePreferences} from './context/PreferencesContext.jsx';
|
||||
import {useCurrentUserActions} from './context/CurrentUserContext.jsx';
|
||||
import {clearSession, hasSession} from './utils/auth.js';
|
||||
import {clearSession} from './utils/auth.js';
|
||||
import useSession from './hooks/useSession.js';
|
||||
import {logout} from './utils/api.js';
|
||||
|
||||
function App() {
|
||||
const {t} = useTranslation();
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
||||
const {appearance, syncPreferences, resetAppearance} = usePreferences();
|
||||
const {setUser} = useCurrentUserActions();
|
||||
const {active: isAuthenticated} = useSession();
|
||||
const {appearance, syncPreferences} = usePreferences();
|
||||
|
||||
// Hooks for routing
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
// No local expiry check here: a 401 from the backend ends the session (useSessionExpired), and a browser
|
||||
// clock that runs ahead would otherwise end it silently.
|
||||
setIsAuthenticated(hasSession());
|
||||
}, [location]);
|
||||
|
||||
// On login and on page load; the cached look shows until Auth answers.
|
||||
useEffect(() => {
|
||||
if (isAuthenticated) syncPreferences();
|
||||
@@ -52,9 +45,6 @@ function App() {
|
||||
const handleLogout = async () => {
|
||||
await logout();
|
||||
clearSession();
|
||||
resetAppearance();
|
||||
setUser(null);
|
||||
setIsAuthenticated(false);
|
||||
navigate('/login');
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user