UNPKG

@stacks/auth

Version:

Authentication for Stacks apps.

31 lines 1.2 kB
import { decodeToken } from 'jsontokens'; import { BLOCKSTACK_HANDLER, createFetchFn, getGlobalObject } from '@stacks/common'; export function getAuthRequestFromURL() { const location = getGlobalObject('location', { throwIfUnavailable: true, usageDesc: 'getAuthRequestFromURL', }); const params = new URLSearchParams(location?.search); return params.get('authRequest')?.replaceAll(`${BLOCKSTACK_HANDLER}:`, '') ?? null; } export async function fetchAppManifest(authRequest, fetchFn = createFetchFn()) { if (!authRequest) { throw new Error('Invalid auth request'); } const payload = 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'); } } //# sourceMappingURL=provider.js.map