UNPKG

@stacks/auth

Version:

Authentication for Stacks apps.

36 lines 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchAppManifest = exports.getAuthRequestFromURL = void 0; const jsontokens_1 = require("jsontokens"); const common_1 = require("@stacks/common"); function getAuthRequestFromURL() { const location = (0, common_1.getGlobalObject)('location', { throwIfUnavailable: true, usageDesc: 'getAuthRequestFromURL', }); const params = new URLSearchParams(location?.search); return params.get('authRequest')?.replaceAll(`${common_1.BLOCKSTACK_HANDLER}:`, '') ?? null; } exports.getAuthRequestFromURL = getAuthRequestFromURL; async function fetchAppManifest(authRequest, fetchFn = (0, common_1.createFetchFn)()) { if (!authRequest) { throw new Error('Invalid auth request'); } const payload = (0, jsontokens_1.decodeToken)(authRequest).payload; if (typeof payload === 'string') { throw new Error('Unexpected token payload type of string'); } const manifestURI = payload.manifest_uri; try { const response = await fetchFn(manifestURI); const responseText = await response.text(); const responseJSON = JSON.parse(responseText); return { ...responseJSON, manifestURI }; } catch (error) { console.log(error); throw new Error('Could not fetch manifest.json'); } } exports.fetchAppManifest = fetchAppManifest; //# sourceMappingURL=provider.js.map