useMMAuth
Provides access to configured URLs and a request convenience function that makes fetch requests with MachineMetrics authorization headers.
Usage
import { useMMAuth } from '@machinemetrics/mm-react-tools';
function MyWidget() {
const {
urls,
request,
clientId,
isAuthenticated,
logout,
accessToken,
jwtRef,
natsCredsRef,
} = useMMAuth();
// Example usage
const fetchMachines = async () => {
const machines = await request(`${urls.apiUrl}/machines`);
return machines;
};
if (!isAuthenticated) {
return <div>Please log in to view machines</div>;
}
return (
<div>
<h1>Machines</h1>
{/* Use the fetched data */}
</div>
);
}
Properties
Property | Description |
---|---|
urls | Object containing URLs for appUrl , apiUrl , graphQLUrl , loginUrl , and natsUrl |
request | Function that wraps fetch(url, options) with automatic MachineMetrics authorization headers and application/json content type |
clientId | OAuth client ID of the application |
isAuthenticated | Boolean indicating user's authentication status |
logout | Function to log the user out |
accessToken | Access token for MachineMetrics API calls |
jwtRef | Ref containing the current JWT for GraphQL requests (refreshed every 4.5 minutes) |
natsCredsRef | Ref containing the current JWT for NATS communication (refreshed every 9.5 minutes) |
Example
const { urls, request } = useMMAuth();
// Make an API request
const machines = await request(`${urls.apiUrl}/machines`);