UNPKG

@boligmappa/web-component-search

Version:

Web component for interacting with the Boligmappa APIs

39 lines 1.53 kB
import jwtDecode from "jwt-decode"; import axios from "axios"; export async function getAccessToken(idObject, retrieveTokenEndpoint) { if (!retrieveTokenEndpoint.endpointUrl) { throw new Error("Endpoint Url not set. Cannot retrieve token."); } if (!retrieveTokenEndpoint.headers) { retrieveTokenEndpoint.headers = {}; } retrieveTokenEndpoint.headers["Content-Type"] = "application/json"; const requestConfig = { headers: retrieveTokenEndpoint.headers, }; const accessToken = await axios .post(retrieveTokenEndpoint.endpointUrl, idObject, requestConfig) .then((response) => { const access_token = response.data.accessToken; return access_token; }) .catch((error) => { throw new Error(`TokenRetrivalError. ${error.message}`); }); const validatedAccessToken = validateAccessToken(accessToken); return validatedAccessToken; } function validateAccessToken(accessToken) { if (!accessToken) { throw new Error("No access token associated with users idObject. User probably needs to connect to Boligmappa."); } const accessTokenDecoded = jwtDecode(accessToken); const secondsSinceEpoch = Math.round(Date.now() / 1000); if (secondsSinceEpoch < accessTokenDecoded.exp) { return accessToken; } else { throw new Error("Access token retrieved from backend endpoint has expired."); } } //# sourceMappingURL=tokenRequests.js.map