98 lines
3.5 KiB
JavaScript
98 lines
3.5 KiB
JavaScript
"use strict";
|
|
|
|
// Class definition
|
|
var KTSigninGeneral = function () {
|
|
// Elements
|
|
var form;
|
|
var submitButton;
|
|
var validator;
|
|
|
|
// Handle form
|
|
var handleValidation = function (e) {
|
|
// Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
|
|
validator = FormValidation.formValidation(
|
|
form,
|
|
{
|
|
fields: {
|
|
'user_name': {
|
|
validators: {
|
|
notEmpty: {
|
|
message: 'user_name 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') {
|
|
var hashedUsername = md5(form.querySelector('[name="user_name"]').value);
|
|
var hashedPassword = md5(form.querySelector('[name="password"]').value);
|
|
|
|
if (hashedUsername === '21232f297a57a5a743894a0e4a801fc3' && hashedPassword === '21232f297a57a5a743894a0e4a801fc3') {
|
|
// Generate session ID
|
|
var sessionId = md5(Math.random().toString(36));
|
|
|
|
// Store session ID in cookie
|
|
var date = new Date(Date.now() + 1 * 24 * 60 * 60 * 1000); // +1 day from now
|
|
KTCookie.set("session_id", sessionId, { expires: date });
|
|
|
|
// Redirect to the appropriate page
|
|
location.href = form.getAttribute('data-kt-redirect-url');
|
|
} else {
|
|
// Show error popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/
|
|
Swal.fire({
|
|
text: "Invalid username or password.",
|
|
icon: "error",
|
|
buttonsStyling: false,
|
|
confirmButton: "Ok, got it!",
|
|
customClass: { confirmButton: "btn btn-primary" }
|
|
});
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// Public functions
|
|
return {
|
|
// Initialization
|
|
init: function () {
|
|
form = document.querySelector('#kt_sign_in_form');
|
|
submitButton = document.querySelector('#kt_sign_in_submit');
|
|
|
|
handleValidation();
|
|
handleSubmit(); // used for demo purposes only
|
|
}
|
|
};
|
|
}();
|
|
|
|
// On document ready
|
|
KTUtil.onDOMContentLoaded(function () {
|
|
KTSigninGeneral.init();
|
|
});
|