initial
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useAuthStore } from '../store/authStore';
|
||||
import { authApi } from '../api/authApi';
|
||||
|
||||
/** Current authenticated user (server state via React Query). */
|
||||
export function useAuthUser() {
|
||||
const user = useAuthStore((s) => s.user);
|
||||
const isAuthenticated = user !== null;
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: ['auth', 'me'],
|
||||
queryFn: authApi.getMe,
|
||||
enabled: isAuthenticated,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
});
|
||||
|
||||
return {
|
||||
user: query.data ?? user,
|
||||
isLoading: query.isLoading,
|
||||
isError: query.isError,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { useAuthStore } from '../store/authStore';
|
||||
import type { LoginCredentials } from '../types';
|
||||
|
||||
export function useLogin() {
|
||||
const login = useAuthStore((s) => s.login);
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (credentials: LoginCredentials) => login(credentials.email, credentials.password),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { useAuthStore } from '../store/authStore';
|
||||
|
||||
export function useLogout() {
|
||||
const logout = useAuthStore((s) => s.logout);
|
||||
|
||||
return useMutation({ mutationFn: () => logout() });
|
||||
}
|
||||
Reference in New Issue
Block a user