// -------------------------- // User Login // -------------------------- $(document).ready(function(){ $('#loginForm').on('submit', function(e){ e.preventDefault(); // clear alert $('.alert').hide() setButtonLoading($('#loginForm').find('.s-btn')[0]); setTimeout(()=>{ loginUser() }, 1000); }); }) function loginUser() { $.ajax({ type: "POST", url: "/account/validate-login-data", data: $('#loginForm').serialize(), dataType: "json", success: function (res) { clearButtonLoading($('#loginForm').find('.s-btn')[0], 'Sign in'); if (res.status == 'login_error'){ $('.alert').html(res.message); $('.alert').show(); } if(res.status == 'login_ok'){ // checkLoginSession_SendCode(); $('#loginForm').unbind('submit').submit(); } } }); } // Check session trust device and authenticate var loginVerifyToken function checkLoginSession_SendCode() { trustDeviceCookie = $.cookie('trustDevice'); // get cookie object if (trustDeviceCookie == undefined) $('#verifyLogin').modal('show'); else { $('#loginForm').unbind('submit').submit() } } // ----------------------- // Get login code // ----------------------- var period; function getLoginVerificationCode() { const email = $('#id_email').val(); period = 40; $('.gc-txt').html(''); // send email $.ajax({ type: "POST", url: "/account/send-login-verification-code", data: {'email': email }, dataType: "json", success: function (res) { loginVerifyToken = res.code; $('.gc-txt').html('Code Sent'); $('.gc-txt').removeClass('primary-color'); $('.gc-txt').addClass('grey-color'); // === // $('.lc-alert').removeClass('alert-danger'); $('.lc-alert').addClass('alert-success'); $('.lc-alert').html(` Enter the 6-Digit code sent to ${email.split('@')[0].slice(0,3) + '***' + '@' + email.split('@')[1]}. `); $('.lc-alert').show(); // ==/ setTimer(); } }); } // Set timer to resend Code function setTimer(){ setInterval(()=>{ $('.gc-txt').html('Resend in ' + period+'s'); $('.gc-txt').attr('onclick', ''); if (period < 1){ $('.gc-txt').html('Resend Code'); $('.gc-txt').attr('onclick', 'getLoginVerificationCode()'); $('.gc-txt').removeClass('grey-color'); $('.gc-txt').addClass('primary-color'); clearInterval(setTimer); } period -= 1; }, 1000) } // Verify code and login $('#loginOtpForm').on('submit', (e)=>{ e.preventDefault(); let cleanCode = $('#code-input').val(); // compare codes and login if (cleanCode != loginVerifyToken){ $('.lc-alert').removeClass('alert-success'); $('.lc-alert').addClass('alert-danger'); $('.lc-alert').html( 'The code you entered is incorrect. Please check your email for code' ); $('.lc-alert').show() } else if (cleanCode == loginVerifyToken) { // get the trust device value if ($('#trustDeviceInp').val() == 'true') { $.cookie('trustDevice', 'valid', { expires: 60, path: '/' }); } $('#loginForm').unbind('submit').submit(); } }) // Toggle Trust Device function trustDevice(e){ if (e.classList.contains('checked')){ $(e).removeClass('fa-check-square primary-color checked'); $(e).addClass('fa-square-o black-text'); $('#trustDeviceInp').val('false'); } else{ $(e).removeClass('fa-square-o black-text'); $(e).addClass('fa-check-square primary-color checked'); $('#trustDeviceInp').val('true'); } } // ------/ $('#code-input').on('input', ()=>{$('.lc-alert').hide()}) // ------/ // -------------------------------------- // Registration // -------------------------------------- // Create User var emailVerifyToken $('#signupForm').on('submit', (e)=>{ e.preventDefault(); $('.alert').hide(); if ($('#id_password').val().length < 8) { $('.alert').html("Password must be at least 8 characters"); $('.alert').show(); return } setButtonLoading($('#signupForm').find('button')[0]); setTimeout(() => checkSignupEmail(), 1500) }) function checkSignupEmail(){ // let sBtn = $('#signupForm').find('button')[0]; $.ajax({ type: "POST", url: "/account/check-signup-email", data: {'email': $('#id_email').val()}, dataType: "json", success: function (res) { clearButtonLoading($('#signupForm').find('button')[0], 'Continue') if (res.status == 'email_error') { $('.alert').html(res.message); $('.alert').show(); } if (res.status == 'email_ok') { emailVerifyToken = res.token $('.su-f-1').hide(); $('.su-f-2').show('drop', {direction:'right'}, 300); $('#userToken').val(''); $('.cd-err-txt').hide(); } } }); } // validate email token let sBtn = $('#suTokenForm').find('button')[0]; $('#signupOtpForm').on('submit', (e)=>{ e.preventDefault(); $(sBtn).hide(); $('.lpb2').show(); tokenEntered = $('#userToken').val(); tokenValid = validateSignupEmailToken(tokenEntered); if (!tokenValid) { $('.cd-err-txt').html('Code entered is incorrect!'); $('.cd-err-txt').show(); } else if (tokenValid) $('#signupForm').unbind('submit').submit(); }) $('#userToken').on('input',()=>{$('.cd-err-txt').hide()}) function validateSignupEmailToken(token){ if (token == emailVerifyToken) return true if (token != emailVerifyToken) return false } function retryReg(){ $('.su-f-2').hide(); $('.su-f-1').show('drop', {direction:'left'}, 300); }