2025-12-03 21:56:50 +00:00
|
|
|
import { useAuthStore } from '../store/authStore';
|
|
|
|
|
import { authApi } from '../api/authApi';
|
|
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
|
|
export const useAuth = () => {
|
2025-12-13 02:34:34 +00:00
|
|
|
const {
|
|
|
|
|
user,
|
|
|
|
|
accessToken,
|
|
|
|
|
refreshToken,
|
|
|
|
|
isAuthenticated,
|
|
|
|
|
logout: storeLogout,
|
|
|
|
|
} = useAuthStore();
|
2025-12-03 21:56:50 +00:00
|
|
|
|
|
|
|
|
const logout = async () => {
|
|
|
|
|
if (refreshToken) {
|
|
|
|
|
try {
|
|
|
|
|
await authApi.logout(refreshToken);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Logout API failed', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
storeLogout();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Optional: Query to sync user profile if we have a token but maybe stale user data
|
|
|
|
|
// or to verify token validity on app load
|
|
|
|
|
useQuery({
|
|
|
|
|
queryKey: ['auth', 'me'],
|
|
|
|
|
queryFn: authApi.getMe,
|
|
|
|
|
enabled: !!accessToken,
|
|
|
|
|
retry: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
user,
|
|
|
|
|
accessToken,
|
|
|
|
|
refreshToken,
|
|
|
|
|
isAuthenticated,
|
|
|
|
|
logout,
|
|
|
|
|
};
|
2025-12-13 02:34:34 +00:00
|
|
|
};
|