"use strict"; // Class definition var KTSigninGeneral = function () { // Elements var form; var submitButton; var validator; // Handle form validation var handleValidation = function () { // Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/ validator = FormValidation.formValidation( form, { fields: { 'email': { validators: { regexp: { regexp: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: 'The value is not a valid email address', }, notEmpty: { message: 'Email address is required' } } }, 'password': { validators: { notEmpty: { message: 'The password is required' } } } }, plugins: { trigger: new FormValidation.plugins.Trigger(), bootstrap: new FormValidation.plugins.Bootstrap5({ rowSelector: '.fv-row', eleInvalidClass: '', // comment to enable invalid state icons eleValidClass: '' // comment to enable valid state icons }) } } ); } var handleSubmit = function (e) { // Handle form submit submitButton.addEventListener('click', function (e) { // Prevent button default action e.preventDefault(); // Validate form validator.validate().then(function (status) { if (status == 'Valid') { // Show loading indication submitButton.setAttribute('data-kt-indicator', 'on'); // Disable button to avoid multiple click submitButton.disabled = true; // Submit form to backend for real authentication form.submit(); } else { // Show error popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/ Swal.fire({ text: "Please fill in all required fields correctly.", icon: "error", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }); } }); }); } var handleSubmitAjax = function (e) { // Handle form submit submitButton.addEventListener('click', function (e) { // Prevent button default action e.preventDefault(); // Validate form validator.validate().then(function (status) { if (status == 'Valid') { // Show loading indication submitButton.setAttribute('data-kt-indicator', 'on'); // Disable button to avoid multiple click submitButton.disabled = true; // Check axios library docs: https://axios-http.com/docs/intro axios.post(submitButton.closest('form').getAttribute('action'), new FormData(form)).then(function (response) { if (response) { form.reset(); // Show message popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/ Swal.fire({ text: "You have successfully logged in!", icon: "success", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }).then(function (result) { if (result.isConfirmed) { const redirectUrl = form.getAttribute('data-kt-redirect-url'); if (redirectUrl) { location.href = redirectUrl; } } }); } else { // Show error popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/ Swal.fire({ text: "Sorry, the email or password is incorrect, please try again.", icon: "error", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }); } }).catch(function (error) { Swal.fire({ text: "Sorry, looks like there are some errors detected, please try again.", icon: "error", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }); }).then(() => { // Hide loading indication submitButton.removeAttribute('data-kt-indicator'); // Enable button submitButton.disabled = false; }); } else { // Show error popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/ Swal.fire({ text: "Sorry, looks like there are some errors detected, please try again.", icon: "error", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }); } }); }); } var handleResendVerification = function() { // Find resend verification form var resendForm = document.querySelector('form[action="/resend-verification"]'); if (resendForm) { console.log('Resend verification form found'); var resendButton = resendForm.querySelector('button[type="submit"]'); if (resendButton) { console.log('Resend verification button found'); resendButton.addEventListener('click', function(e) { console.log('Resend verification button clicked!'); var emailInput = resendForm.querySelector('input[name="email"]'); var emailValue = emailInput ? emailInput.value : 'Not found'; console.log('Resending verification for email:', emailValue); // Show loading indication resendButton.setAttribute('data-kt-indicator', 'on'); resendButton.disabled = true; console.log('Submitting resend verification form...'); // Let the form submit normally to the backend // The page will reload with the flash message }); } else { console.log('Resend verification button not found'); } } else { console.log('Resend verification form not found'); } } var isValidUrl = function(url) { try { new URL(url); return true; } catch (e) { return false; } } // Public functions return { // Initialization init: function () { form = document.querySelector('#kt_sign_in_form'); submitButton = document.querySelector('#kt_sign_in_submit'); handleValidation(); handleResendVerification(); // Add resend verification handling if (isValidUrl(submitButton.closest('form').getAttribute('action'))) { handleSubmitAjax(); // use for ajax submit } else { handleSubmit(); // used for demo purposes only } } }; }(); // On document ready KTUtil.onDOMContentLoaded(function () { KTSigninGeneral.init(); });