/
nsstnc
/
lab3
Обзор
Документация
Войти
/
nsstnc
/
lab3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/hooks/useCurrentUser.ts
25 строк
728 B
nsstnc
Обновил авторизацию
21 ноя 2025, 18:13
21 ноя 2025, 18:13
e8cb2e7
Код
Авторство
О чём код?
import { useEffect } from 'react' import { useQuery } from '@tanstack/react-query' import { getCurrentUser } from '@/api/auth' import { useAuthStore } from '@/store/authStore' export const useCurrentUser = () => { const { accessToken, setUser, logout, setLoading } = useAuthStore() const { data: user, error, isLoading } = useQuery({ queryKey: ['currentUser'], queryFn: getCurrentUser, enabled: !!accessToken, retry: false, }) useEffect(() => { if (user) setUser(user) if (error || (!accessToken && !isLoading)) { logout() setLoading(false) } }, [user, error, accessToken, isLoading]) return { user: useAuthStore(s => s.user), isLoading: useAuthStore(s => s.isLoading) } }