"use strict"; $(document).ready(function () { $("#login,#resend-code").click(login); $(document).keypress(function (e) { if (e.which == 13) { if ($("#password").is(":focus")) { login(); } if ($("#forgot-password-email").is(":focus")) { forgotPassword(); } } }); $(".switch-forms").click(function () { $(".form").toggle(); $("#change-password-form").hide(); }); $("#forgot-password").click(forgotPassword); $("#submit-forgot-password").click(submitForgotPassword); $("#change-password").click(changePassword); }); var authResponse = null; function login() { if (utils.basicValidate("#login-form")) { $.ajax({ url: utils.serviceUrl() + "/Authenticate", type: "POST", data: { password: $("#password").val(), email: $("#email").val() }, success: function success(res) { if (res.status == "ok") { if (res.data.user.requirePasswordChange == 1) { authResponse = res.data; return showChangePassword(); } localStorage.setItem("authTokenGUID", res.data.authTokenGUID); localStorage.setItem("user", JSON.stringify(res.data.user)); if (res.data.user.isSuperAdmin) { window.location = "/admin.html"; } else if (res.data.user.isAdmin) { window.location = "/dashboard.html"; } else { window.location = "/training.html"; } } else { utils.notify("Incorrect email or password", "error"); } } }); } } function showChangePassword() { $(".login-content").hide(); $("#change-password-form").show(); } function changePassword() { if (utils.basicValidate("#change-password-form")) { $.ajax({ url: utils.serviceUrl() + "/ChangePassword", type: "POST", data: { authTokenGUID: authResponse.authTokenGUID, email: $("#email").val(), oldPassword: $("#password").val(), password: $("#new-password").val() }, success: function success(res) { utils.notify("Password set. Logging in..."); res.data = authResponse; localStorage.setItem("authTokenGUID", res.data.authTokenGUID); localStorage.setItem("user", JSON.stringify(res.data.user)); if (res.data.user.isSuperAdmin) { window.location = "/admin.html"; } else if (res.data.user.isAdmin) { window.location = "/dashboard.html"; } else { window.location = "/training.html"; } } }); } } function forgotPassword() { if (utils.basicValidate($("#forgot-password-email").parent())) { $.ajax({ url: utils.serviceUrl() + "/RequestForgotPassword", type: "POST", data: { email: $("#forgot-password-email").val() }, success: function success(res) { utils.notify("A code to reset your password has been sent to your email. Enter the code here to reset your password."); $("#forgot-password-form input").prop("required", true).parent().show(); $("#submit-forgot-password").parent().show(); } }); } } function submitForgotPassword() { if (utils.basicValidate("#forgot-password-form")) { $.ajax({ url: utils.serviceUrl() + "/ForgotPassword", type: "POST", data: { email: $("#forgot-password-email").val(), code: $("#password-code").val(), password: $("#forgot-password-password").val() }, success: function success(res) { if (res.message) { utils.notify(res.message, "error"); } else { utils.notify("Password reset! Logging in..."); setTimeout(function () { $("#email").val($("#forgot-password-email").val()), $("#password").val($("#forgot-password-password").val()); $("#login").click(); }); } }, error: function error() { utils.notify("Could not find user.", "error"); } }); } }