authenticated-fetch

A function that wraps a standard fetch call with logic to inject the current user's authentication token.

Installation#

npm install @iqmetrix/authenticated-fetch

Interface#

This function has the same function signature as native fetch

Example#

import authenticatedFetch from '@iqmetrix/authenticated-fetch';
import getEnvironmentSuffix from "@iqmetrix/get-environment-suffix";
const env = await getEnvironmentSuffix();
// same function signature as native fetch
authenticatedFetch(`https://accounts${env}.iqmetrix.net/v1/me`)
.then(response => response.json())
.then(console.log)
.catch(console.error);

See tests for more examples.

If the function cannot acquire a token, or a 401 response is received, the user is automatically redirected to the login page. Redirection blocks promise resolution, so there will not be an opportunity to handle these cases.

Sources#

Last updated on by Olga Tkatska