apps.downloads.$token
Source Summary import { json, type LoaderFunctionArgs } from '@remix-run/node'; import { useLoaderData } from '@remix-run/react'; import { getEntitlementByTo...
Source Summary
import { json, type LoaderFunctionArgs } from '@remix-run/node'; import { useLoaderData } from '@remix-run/react'; import { getEntitlementByToken, getAssetsByRelease } from '. /db.
Imported Context
import { json, type LoaderFunctionArgs } from '@remix-run/node'; import { useLoaderData } from '@remix-run/react'; import { getEntitlementByToken, getAssetsByRelease } from '../db.server'; import { getSignedUrl } from '../storage.server';
export async function loader({ params }: LoaderFunctionArgs) { const token = params.token || ''; const entitlement = getEntitlementByToken(token); if (!entitlement) { return json({ error: 'Invalid token' }, { status: 404 }); }
const expiresAtMs = Date.parse(String(entitlement.expires_at || '')); if (!Number.isFinite(expiresAtMs)) { return json({ error: 'Invalid token' }, { status: 404 }); } if (expiresAtMs <= Date.now()) { return json({ error: 'Expired token' }, { status: 410 }); }
if (!entitlement.release_id || entitlement.release_id === 'unknown-release') { return json({ error: 'Invalid entitlement release mapping' }, { status: 404 }); }
const assets = getAssetsByRelease(entitlement.release_id); if (!assets.length) { return json({ error: 'No downloadable assets available for this entitlement' }, { status: 404 }); }
const urls = await Promise.all( assets.map(async (asset: any) => ({ key: asset.key, label: asset.variant_label, url: await getSignedUrl(asset.key, 24 * 60 * 60), })) );
return json({ entitlement, urls }); }
export default function DownloadPage() { const data = useLoaderData<typeof loader>(); if ('error' in data) { return <div>Invalid or expired link.</div>; } return ( <div style={{ fontFamily: 'system-ui', padding: '2rem' }}> <h1>Your Digital Downloads</h1> <p>Links will expire in 24 hours.</p> <ul> {data.urls.map((item: { key: string; url: string; label: string }) => ( <li key={item.key}> <a href={item.url} rel="noreferrer"> {item.label} </a> </li> ))} </ul> </div> ); }
Provenance
- Source file:
../../app/app/routes/apps.downloads.$token.tsx - Source URL: https://github.com/maggielerman/digital-download-shopify-app/blob/main/app/app/routes/apps.downloads.%24token.tsx