atlasLogin.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. function doLogin() {
  2. var userName = $("#username").val().trim(), passwd = $("#password").val().trim();
  3. if ("" === userName || "" === passwd) return $("#errorBox").show(), $("#signInLoading").hide(),
  4. $("#signIn").removeAttr("disabled"), $("#errorBox .errorMsg").text("The username or password you entered is blank.."),
  5. !1;
  6. var baseUrl = getBaseUrl();
  7. $.ajax({
  8. data: {
  9. j_username: userName,
  10. j_password: passwd
  11. },
  12. url: baseUrl + "/j_spring_security_check",
  13. type: "POST",
  14. headers: {
  15. "cache-control": "no-cache"
  16. },
  17. success: function() {
  18. redirect(baseUrl);
  19. },
  20. error: function(jqXHR, textStatus, err) {
  21. if ($("#signIn").removeAttr("disabled"), $("#signInLoading").css("visibility", "hidden"),
  22. jqXHR.status && 412 == jqXHR.status) $("#errorBox").hide(), $("#errorBoxUnsynced").show(); else {
  23. try {
  24. var resp = JSON.parse(jqXHR.responseText);
  25. resp.msgDesc.startsWith("Username not found") || resp.msgDesc.startsWith("Wrong password") ? $("#errorBox .errorMsg").text("Invalid User credentials. Please try again.") : resp.msgDesc.startsWith("User role credentials is not set properly") ? $("#errorBox .errorMsg").text("User role or credentials is not set properly") : $("#errorBox .errorMsg").text("Error while authenticating");
  26. } catch (err) {
  27. $("#errorBox .errorMsg").text("Something went wrong");
  28. }
  29. $("#errorBox").show(), $("#errorBoxUnsynced").hide();
  30. }
  31. }
  32. });
  33. }
  34. function redirect(baseUrl) {
  35. $.ajax({
  36. url: baseUrl + "api/atlas/admin/session",
  37. success: function(data) {
  38. var PRIMARY_UI = "v2", indexpath = "/n/index.html";
  39. data && data["atlas.ui.default.version"] && (PRIMARY_UI = data["atlas.ui.default.version"]),
  40. "v2" !== PRIMARY_UI && (indexpath = "/index.html"), "v1" === window.localStorage.last_ui_load ? indexpath = "/index.html" : "v2" === window.localStorage.last_ui_load && (indexpath = "/n/index.html"),
  41. indexpath = baseUrl + indexpath, location.hash.length > 2 && (indexpath += location.hash),
  42. window.location.replace(indexpath);
  43. },
  44. error: function() {
  45. window.location.replace("index.html");
  46. }
  47. });
  48. }
  49. function getBaseUrl() {
  50. return window.location.pathname.replace(/\/[\w-]+.(jsp|html)|\/+$/gi, "");
  51. }
  52. Array.prototype.indexOf || (Array.prototype.indexOf = function(obj, start) {
  53. for (var i = start || 0; i < this.length; i++) if (this[i] == obj) return i;
  54. return -1;
  55. }), String.prototype.startsWith || (String.prototype.startsWith = function(str, matchStr) {
  56. return 0 === str.lastIndexOf(matchStr, 0);
  57. }), $(function() {
  58. "placeholder" in HTMLInputElement.prototype || $("#username , #password").placeholder(),
  59. $("#signIn").on("click", function() {
  60. return $("#signIn").attr("disabled", !0), $("#signInLoading").css("visibility", "visible"),
  61. doLogin(), !1;
  62. }), $("#loginForm").each(function() {
  63. $("input").keypress(function(e) {
  64. 10 != e.which && 13 != e.which || doLogin();
  65. });
  66. }), $("#loginForm li[class^=control-group] > input").on("change", function(e) {
  67. "" === e.target.value ? $(e.target).parent().addClass("error") : $(e.target).parent().removeClass("error");
  68. }), $("#password").on("keyup", function() {
  69. "" === this.value.trim() ? $(".show-password ").hide() : $(".show-password ").show();
  70. });
  71. var showPassword = !1;
  72. $(".show-password").on("click", function() {
  73. showPassword = !showPassword, $("#password").attr("type", showPassword ? "text" : "password"),
  74. $(".show-password").toggleClass("fa-eye-slash fa-eye");
  75. });
  76. });