112 lines
2.9 KiB
TypeScript
112 lines
2.9 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { useNavigate } from '@tanstack/react-router'
|
|
import { toast } from 'sonner'
|
|
import { authApi } from '@api/auth'
|
|
import { useAuthStore } from '@stores/auth-store'
|
|
import type { ChangePasswordData, LoginCredentials } from '@api/auth'
|
|
|
|
export const authKeys = {
|
|
all: ['auth'] as const,
|
|
status: () => [...authKeys.all, 'status'] as const,
|
|
check: () => [...authKeys.all, 'check'] as const,
|
|
}
|
|
|
|
export function useAuthStatus() {
|
|
const { setNeedsSetup, setIsLoading } = useAuthStore()
|
|
|
|
return useQuery({
|
|
queryKey: authKeys.status(),
|
|
queryFn: async () => {
|
|
const status = await authApi.getStatus()
|
|
setNeedsSetup(status.needsSetup)
|
|
setIsLoading(false)
|
|
return status
|
|
},
|
|
staleTime: 1000 * 60 * 5,
|
|
})
|
|
}
|
|
|
|
export function useAuthCheck() {
|
|
const { setUser } = useAuthStore()
|
|
|
|
return useQuery({
|
|
queryKey: authKeys.check(),
|
|
queryFn: async () => {
|
|
const response = await authApi.check()
|
|
setUser(response.user)
|
|
return response
|
|
},
|
|
retry: false,
|
|
staleTime: 1000 * 60 * 5,
|
|
})
|
|
}
|
|
|
|
export function useLogin() {
|
|
const queryClient = useQueryClient()
|
|
const navigate = useNavigate()
|
|
const { setUser } = useAuthStore()
|
|
|
|
return useMutation({
|
|
mutationFn: (data: LoginCredentials) => authApi.login(data),
|
|
onSuccess: (response) => {
|
|
setUser(response.user)
|
|
queryClient.invalidateQueries({ queryKey: authKeys.all })
|
|
toast.success('Login successful')
|
|
navigate({ to: '/admin' })
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || 'Login failed')
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useSetup() {
|
|
const queryClient = useQueryClient()
|
|
const navigate = useNavigate()
|
|
const { setUser, setNeedsSetup } = useAuthStore()
|
|
|
|
return useMutation({
|
|
mutationFn: (data: LoginCredentials) => authApi.setup(data),
|
|
onSuccess: (response) => {
|
|
setUser(response.user)
|
|
setNeedsSetup(false)
|
|
queryClient.invalidateQueries({ queryKey: authKeys.all })
|
|
toast.success('Setup complete')
|
|
navigate({ to: '/admin' })
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || 'Setup failed')
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useLogout() {
|
|
const queryClient = useQueryClient()
|
|
const navigate = useNavigate()
|
|
const { reset } = useAuthStore()
|
|
|
|
return useMutation({
|
|
mutationFn: () => authApi.logout(),
|
|
onSuccess: () => {
|
|
reset()
|
|
queryClient.clear()
|
|
toast.success('Logged out')
|
|
navigate({ to: '/login' })
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || 'Logout failed')
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useChangePassword() {
|
|
return useMutation({
|
|
mutationFn: (data: ChangePasswordData) => authApi.changePassword(data),
|
|
onSuccess: () => {
|
|
toast.success('Password changed successfully')
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || 'Failed to change password')
|
|
},
|
|
})
|
|
}
|