Files
Notizarr/apps/web/src/hooks/useSession.ts
T
2026-09-07 09:54:30 +00:00

26 lines
808 B
TypeScript

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { apiFetch, apiPost } from '../lib/api'
import type { SessionResponse } from '../types'
export function useSession() {
return useQuery({
queryKey: ['session'],
queryFn: () => apiFetch<SessionResponse>('/api/v1/session'),
staleTime: 60_000,
})
}
export function useLogout() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: () => apiPost('/api/v1/auth/logout'),
// Full reload rather than just invalidating the session query — clears
// any in-memory query cache holding data from the now-logged-out user,
// and re-runs App.tsx's session check from a clean slate.
onSuccess: () => {
queryClient.clear()
window.location.assign('/')
},
})
}