import { Router } from 'express'; import { paywallService } from '../services/paywall.js'; import { accessService } from '../services/access.js'; const router = Router(); // Get public paywall data for hosted page router.get('/:slugOrId', async (req, res, next) => { try { const { slugOrId } = req.params; const paywall = await paywallService.findBySlugOrId(slugOrId); // Check if user already has access const tokenId = req.cookies?.[`token_id_${paywall.id}`]; let hasAccess = false; let accessInfo = null; if (tokenId) { const accessResult = await accessService.checkAccessByCookie(tokenId, paywall.id); hasAccess = accessResult.hasAccess; if (hasAccess) { accessInfo = { originalUrl: accessResult.paywall.originalUrl, expiresAt: accessResult.accessGrant?.expiresAt, }; } } res.json({ paywall: { id: paywall.id, title: paywall.title, description: paywall.description, coverImageUrl: paywall.coverImageUrl, priceSats: paywall.priceSats, previewMode: paywall.previewMode, previewContent: paywall.previewContent, originalUrlType: paywall.originalUrlType, customSuccessMessage: paywall.customSuccessMessage, customBranding: paywall.customBranding, // Only include original URL if user has access originalUrl: hasAccess ? paywall.originalUrl : null, creator: paywall.creator, }, hasAccess, accessInfo, }); } catch (error) { next(error); } }); // Get paywall for embed (checks origin) router.get('/:id/embed-data', async (req, res, next) => { try { const { id } = req.params; const paywall = await paywallService.findById(id); // Check origin restrictions const origin = req.get('origin') || req.get('referer'); if (paywall.allowedEmbedOrigins && paywall.allowedEmbedOrigins.length > 0) { const allowed = paywall.allowedEmbedOrigins.some(allowedOrigin => { if (!origin) return false; return origin.includes(allowedOrigin); }); if (!allowed && origin) { return res.status(403).json({ error: 'Embedding not allowed from this origin', }); } } if (!paywall.allowEmbed) { return res.status(403).json({ error: 'Embedding is disabled for this paywall', }); } res.json({ id: paywall.id, title: paywall.title, description: paywall.description, coverImageUrl: paywall.coverImageUrl, priceSats: paywall.priceSats, previewMode: paywall.previewMode, previewContent: paywall.previewContent, customBranding: paywall.customBranding, creator: paywall.creator, }); } catch (error) { next(error); } }); export default router;