useLogout
The useLogout hook provides methods to sign out the user and end their session.
Basic Usage
import { useLogout } from 'authsafe-react';
function LogoutButton() {
const { logout } = useLogout();
return <button onClick={logout}>Logout</button>;
}Return Values
| Property | Type | Description |
|---|---|---|
logout | () => Promise<void> | Function to trigger logout |
isLoading | boolean | Whether a logout operation is in progress |
error | Error | null | Error object if logout failed |
Post-Logout Redirect
You can specify where to redirect the user after logout:
import { useLogout } from 'authsafe-react';
import { useNavigate } from 'react-router-dom';
function LogoutButton() {
const { logout, isLoading } = useLogout();
const navigate = useNavigate();
const handleLogout = async () => {
await logout();
navigate('/');
};
return (
<button onClick={handleLogout} disabled={isLoading}>
{isLoading ? 'Logging out...' : 'Logout'}
</button>
);
}Type Definition
interface IUseLogout {
logout: () => Promise<void>;
isLoading: boolean;
error: Error | null;
}