Line-Notify / public / script.js
script.js
Raw
const authButton = document.getElementById("authButton");
const authContainer = document.getElementById("userForm");
const successContainer = document.getElementById("successContainer");
const fullNameInput = document.getElementById("fullName");
const nicknameInput = document.getElementById("nickname");
const termsCheckbox = document.getElementById("termsCheckbox");

authButton.addEventListener("click", (event) => {
  event.preventDefault(); // Prevent form submission

  // Check if form fields are filled and terms are accepted
  const isFormValid =
    fullNameInput.value.trim() !== "" &&
    nicknameInput.value.trim() !== "" &&
    termsCheckbox.checked;

  if (isFormValid) {
    // Redirect to Line Notify OAuth Authorization URL with nickname as STATE_VALUE
    const fullName = encodeURIComponent(fullNameInput.value.trim());
    const nickname = encodeURIComponent(nicknameInput.value.trim());
    window.location.href = `https://lsp-line-token.firebaseapp.com/auth?state=${nickname}&fullName=${fullName}`;
  } else {
    // Show an error message or validation feedback
    alert("กรุณากรอกข้อมูลให้ครบถ้วนและยอมรับเงื่อนไขการใช้งาน");
  }
});

// Check URL for code parameter
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get("code");
if (code) {
  // If code exists, hide auth form and show success message
  authContainer.style.display = "none";
  successContainer.style.display = "block";
} else {
  // If code doesn't exist, show auth form and hide success message
  authContainer.style.display = "block";
  successContainer.style.display = "none";
}