// src/client/processes/auth.ts
// Authentication process - manages login/logout flow
import { useUserStore } from '../entities/user';

export const useAuthProcess = () => {
  const userStore = useUserStore();

  const login = async (username: string, password: string) => {
    await userStore.login({ username, password });
  };

  const logout = async () => {
    try {
      // Пытаемся корректно завершить сессию (отправить запрос на бэк)
      await userStore.logout();
    } catch (e) {
      console.error('Logout error:', e);
    } finally {
      // ГАРАНТИРОВАННО переходим на логин, даже если API вернул ошибку
      window.location.href = '/login';
    }
  };

  return {
    login,
    logout,
  };
};