deckhearth/pages/api/invite/decline.js
varutasu da50d78406
fix(security): drop wildcard CORS + redundant OPTIONS from 24 API routes (P0 #5)
Closes P0 #5 from PARTIAL to RESOLVED. Sweeps the remaining 24 pages/api/** handlers that carried the identical scaffolded wildcard-CORS + OPTIONS preflight pattern (Brief 4 cleaned login + register; this finishes the job). Adds a blocking forbidden-cors-headers CI job modeled on forbidden-endpoints to lock the cleanup against future regression. 25 files changed (+29/-261). Local: lint 128 baseline, vitest 21/21, zero CORS matches, YAML valid. CI: Playwright smoke 3/3 in 3.3s against post-removal preview (login/verify flow still works), new forbidden-cors-headers job passes in 4s, all gates green. PR #19 architect-commit ec22b70, implementer-commit a843736.
2026-05-24 20:41:38 -05:00

53 lines
1.5 KiB
JavaScript

import { sql } from '@vercel/postgres';
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const { token } = req.body;
if (!token) {
return res.status(400).json({ error: 'Invitation token is required' });
}
// Find the invitation
const invitationResult = await sql`
SELECT cp.*, c.name as collection_name
FROM collection_permissions cp
JOIN collections c ON cp.collection_id = c.id
WHERE cp.invite_token = ${token} AND cp.status = 'pending'
`;
if (invitationResult.rows.length === 0) {
return res.status(404).json({ error: 'Invalid or expired invitation' });
}
const invitation = invitationResult.rows[0];
// Decline the invitation by deleting the permission record
await sql`
DELETE FROM collection_permissions
WHERE invite_token = ${token}
`;
// Log activity
await sql`
INSERT INTO collection_activity (collection_id, user_id, action, details)
VALUES (${invitation.collection_id}, ${invitation.user_id}, 'invitation_declined', ${JSON.stringify({ token })})
`;
res.status(200).json({
message: 'Invitation declined successfully',
collection: {
id: invitation.collection_id,
name: invitation.collection_name
}
});
} catch (error) {
console.error('Error declining invitation:', error);
res.status(500).json({ error: 'Internal server error' });
}
}