use user from session

This commit is contained in:
john 2025-05-20 10:06:18 +02:00
parent 5f47162a50
commit 700eaf3eb2
16 changed files with 148 additions and 107 deletions

View file

@ -0,0 +1,28 @@
import { useEffect } from 'react'
import { useUser } from './app/user/user.ts'
import { AuthService } from './app/auth/authService.ts'
// Starts a loop that pings the server to keep the session alive, while also getting any updates on the user profile
export function useRefreshSessionLoop(authService: AuthService) {
const user = useUser()
const userId = user?.userId ?? null
useEffect(() => {
if (userId == null) {
return
}
const timeouts: number[] = []
timeouts.push(
setTimeout(async function refreshUser() {
await authService.refreshUser(userId)
timeouts.push(setTimeout(refreshUser, 60_000))
}),
)
return () => {
timeouts.forEach(clearTimeout)
}
}, [authService, userId])
}