fix user refresher

This commit is contained in:
john 2025-05-19 17:52:18 +02:00
parent 17c9885ccc
commit 5f47162a50
6 changed files with 85 additions and 36 deletions

View file

@ -10,37 +10,60 @@ import LogoutPage from './app/auth/pages/LogoutPage.tsx'
import UnauthorizedHandler from './app/auth/components/UnauthorizedHandler.tsx'
import AdminPage from './app/admin/pages/AdminPage.tsx'
import SignupCodesManagementPage from './app/admin/pages/subpages/SignupCodesManagementPage.tsx'
import RefreshUser from './app/auth/components/RefreshUser.tsx'
import { useUser } from './app/user/userStore.ts'
import { useEffect, useMemo } from 'react'
function App() {
export default function App() {
const postService = new PostsService()
const mediaService = new MediaService()
const authService = new AuthService()
const authService = useMemo(() => new AuthService(), [])
const { user, setUser } = useUser()
const userId = user?.userId ?? null
useEffect(() => {
if (userId == null) {
return
}
const timeouts: number[] = []
timeouts.push(
setTimeout(async function refreshUser() {
const userInfo = await authService.refreshUser(userId)
setUser(userInfo)
timeouts.push(setTimeout(refreshUser, 60_000))
}),
)
return () => {
timeouts.forEach(clearTimeout)
}
}, [authService, setUser, userId])
return (
<BrowserRouter>
<UnauthorizedHandler>
<RefreshUser authService={authService}>
<Routes>
<Routes>
<Route
path={'/'}
element={<HomePage postsService={postService} mediaService={mediaService} />}
/>
<Route path="/u/:username" element={<AuthorPage postsService={postService} />} />
<Route path="/login" element={<LoginPage authService={authService} />} />
<Route path="/logout" element={<LogoutPage authService={authService} />} />
<Route path="/signup/:code?" element={<SignupPage authService={authService} />} />
<Route path={'/admin'} element={<AdminPage />}>
<Route
path={'/'}
element={<HomePage postsService={postService} mediaService={mediaService} />}
path={'codes'}
element={<SignupCodesManagementPage authService={authService} />}
/>
<Route path="/u/:username" element={<AuthorPage postsService={postService} />} />
<Route path="/login" element={<LoginPage authService={authService} />} />
<Route path="/logout" element={<LogoutPage authService={authService} />} />
<Route path="/signup/:code?" element={<SignupPage authService={authService} />} />
<Route path={'/admin'} element={<AdminPage />}>
<Route
path={'codes'}
element={<SignupCodesManagementPage authService={authService} />}
/>
</Route>
</Routes>
</RefreshUser>
</Route>
</Routes>
</UnauthorizedHandler>
</BrowserRouter>
)
}
export default App