use user from session
This commit is contained in:
parent
5f47162a50
commit
700eaf3eb2
16 changed files with 148 additions and 107 deletions
28
src/useRefreshSessionLoop.ts
Normal file
28
src/useRefreshSessionLoop.ts
Normal 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])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue