import { useNavigate } from 'react-router-dom';
import axios from '../services/api';

function LogoutButton() {
  const navigate = useNavigate();

  const logout = async () => {
    try {
      await axios.post('/logout'); // sendet POST an /api/logout
    } catch (err) {
      console.warn('Logout-Request fehlgeschlagen, ignoriere...');
    }
    localStorage.clear(); // entfernt token + role
    window.location.href = '/login'; // harter Redirect
  };

  return <button onClick={logout}>Logout</button>;
}

export default LogoutButton;
