Skip to content

Instantly share code, notes, and snippets.

@saltukalakus
Last active November 24, 2023 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saltukalakus/322ef43a42c91213de191463189d8038 to your computer and use it in GitHub Desktop.
Save saltukalakus/322ef43a42c91213de191463189d8038 to your computer and use it in GitHub Desktop.
Prevent multiple click problem for device code confirmation screen
<!DOCTYPE html>
<html lang="en">
<head>
{%- auth0:head -%}
{% if prompt.screen.name == "device-code-confirmation" %}
<script>
document.addEventListener("DOMContentLoaded", function() {
document.addEventListener("click", function(event) {
// Check if the clicked element is a button within a form
const clickedButton = event.target.closest("form button");
if (clickedButton) {
const form = clickedButton.closest("form");
if (form) {
clickedButton.disabled = true;
// Get the form data
const formData = new FormData(form);
// Add the 'state' parameter to the FormData
const stateParameter = form.querySelector("[name='state']");
if (stateParameter) {
formData.append("state", stateParameter.value);
console.log("State Parameter:", stateParameter.value);
}
// Perform the POST request
const url = `/device/confirmation?state=${encodeURIComponent(formData.get("state"))}`;
fetch(url, {
method: "POST",
body: `state=${encodeURIComponent(formData.get("state"))}&action=${encodeURIComponent(clickedButton.value)}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(response => {
// Redirect to next page
window.location.replace(response.url);
})
.catch(error => {
// Handle errors
console.error(error);
});
}
}
});
});
</script>
{% endif %}
</head>
<body>
{%- auth0:widget -%}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment