fix: return html

This commit is contained in:
Max Peintner
2025-03-20 14:45:22 +01:00
parent 2ffca960e2
commit 6efa35f19d

View File

@@ -479,34 +479,37 @@ export async function GET(request: NextRequest) {
SAMLResponse: binding.value.samlResponse, SAMLResponse: binding.value.samlResponse,
}; };
// Convert form data to URL-encoded string const formHtml = `
const formBody = Object.entries(formData) <!DOCTYPE html>
.map( <html lang="en">
([key, value]) => <head>
encodeURIComponent(key) + "=" + encodeURIComponent(value), <meta charset="UTF-8">
) <meta name="viewport" content="width=device-width, initial-scale=1.0">
.join("&"); <title>Redirecting...</title>
</head>
<body>
<form id="samlForm" action="${url}" method="POST">
${Object.entries(formData)
.map(
([key, value]) =>
`<input type="hidden" name="${key}" value="${value}" />`,
)
.join("\n")}
</form>
<script>
// Automatically submit the form
document.getElementById('samlForm').submit();
</script>
</body>
</html>
`;
// Make a POST request to the external URL with the form data // Return the HTML response
const response = await fetch(url, { return new NextResponse(formHtml, {
method: "POST",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "text/html",
}, },
body: formBody,
}); });
// Handle the response from the external URL
if (response.ok) {
return NextResponse.json({
message: "SAML request completed successfully",
});
} else {
return NextResponse.json(
{ error: "Failed to complete SAML request" },
{ status: response.status },
);
}
} else { } else {
console.log( console.log(
"could not create response, redirect user to choose other account", "could not create response, redirect user to choose other account",