use user from session
This commit is contained in:
parent
5f47162a50
commit
700eaf3eb2
16 changed files with 148 additions and 107 deletions
40
src/App.tsx
40
src/App.tsx
|
@ -10,39 +10,17 @@ 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 { useUser } from './app/user/userStore.ts'
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { initUser } from './app/user/user.ts'
|
||||
import { useRefreshSessionLoop } from './useRefreshSessionLoop.ts'
|
||||
|
||||
const postService = new PostsService()
|
||||
const mediaService = new MediaService()
|
||||
const authService = new AuthService()
|
||||
|
||||
initUser()
|
||||
|
||||
export default function App() {
|
||||
const postService = new PostsService()
|
||||
const mediaService = new MediaService()
|
||||
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])
|
||||
useRefreshSessionLoop(authService)
|
||||
|
||||
return (
|
||||
<BrowserRouter>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue