Skip to content

Commit a21ec10

Browse files
irvingpopclaude
andauthored
Fix registration update API crash: headers already sent (#1977)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9a51647 commit a21ec10

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,9 @@ public/sitemap.xml
9191
/blob-report/
9292
/playwright/.cache/
9393
/playwright/.auth/
94+
95+
# TypeScript build info
96+
tsconfig.tsbuildinfo
97+
98+
# MCP config (local tool settings)
99+
.mcp.json

components/Forms/UpdateProfileForm/UpdateProfileForm.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ function UpdateProfileForm({
8080
}
8181
}
8282

83-
await updateUser(values);
83+
try {
84+
await updateUser(values);
85+
} catch (error) {
86+
const axiosError = error as AxiosError;
87+
if (axiosError.response?.status === 404) {
88+
// Record no longer exists — cookie was cleared by the API, redirect to re-register
89+
push('/join');
90+
return;
91+
}
92+
throw error;
93+
}
8494
};
8595

8696
const goToProfile = async () => {

pages/api/registration/update.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
1212

1313
const email = req.cookies?.opCodeApplicantEmail;
1414

15+
// The cookie is cleared on the final successful step (when all fields are filled).
16+
// Additional PATCH requests can still arrive after that (e.g. user double-clicking),
17+
// so we need to bail out early rather than querying Airtable with an undefined email.
18+
if (!email) {
19+
return res.status(401).json({ message: 'Missing registration cookie' });
20+
}
21+
1522
try {
1623
// Search for a record with the relevant email
1724
const records = await base(AIR_TABLE_TABLE_NAME)
@@ -96,10 +103,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
96103
return res.status(200).json({ message: 'Success' });
97104
}
98105

99-
// No record found, add a new row to the table
100-
return res
101-
.writeHead(404, { Location: '/' })
102-
.json({ message: `No record found for this email (${email})` });
106+
// No record found — clear the stale cookie so the page guard redirects to /
107+
res.setHeader('Set-Cookie', [
108+
`opCodeApplicantEmail=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`,
109+
]);
110+
return res.status(404).json({ message: `No record found for this email (${email})` });
103111
} catch (error) {
104112
console.error('Error with /api/registration/update PATCH request:', error);
105113
return res.status(500).json({ message: 'Server Error' });

0 commit comments

Comments
 (0)