omnivore/packages/web/lib/logout.ts

26 lines
722 B
TypeScript
Raw Permalink Normal View History

2024-08-21 08:58:29 +00:00
import { useQueryClient } from '@tanstack/react-query'
import { logoutMutation } from './networking/mutations/logoutMutation'
2024-08-21 09:38:30 +00:00
import { useCallback } from 'react'
import { useRouter } from 'next/router'
2024-08-21 09:38:30 +00:00
export const useLogout = () => {
const router = useRouter()
2024-08-21 09:49:19 +00:00
const queryClient = useQueryClient()
2024-08-21 09:38:30 +00:00
const logout = useCallback(async () => {
await logoutMutation()
try {
const result = await logoutMutation()
if (!result) {
throw new Error('Logout failed')
}
queryClient.clear()
console.log('cleared the query client')
router.push('/login')
} catch (err) {
console.log('error on logout: ', err)
router.push('/')
}
2024-08-21 09:38:30 +00:00
}, [])
return { logout }
}