Guest User

Untitled

a guest
Oct 5th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. var socket = io.connect("http://localhost:8001");
  2.  
  3. socket.on("connect", function() {
  4. console.log("Connected.");
  5. });
  6.  
  7. socket.on("disconnect", function() {
  8. window.location.href = "../login.html";
  9. });
  10.  
  11. socket.on("connect_error", function() {
  12. window.location.href = "../login.html";
  13. });
  14.  
  15. socket.on("recovery", function(data) {
  16. switch (data.type) {
  17. case 0:
  18. $(".recovery .question").fadeIn();
  19. $("div.question span.question").text(data.question);
  20. break;
  21.  
  22. case 2:
  23. $(".recovery .question").hide();
  24. $(".recovery .password").show();
  25. sessionStorage.setItem("resetCode", data.resetCode);
  26. break;
  27.  
  28. case 3:
  29. displayInfo(data.info);
  30. break;
  31.  
  32. case 5:
  33. $("div.login").show();
  34. $("div.register").hide();
  35. $("div.recovery").hide();
  36. $("div.login input[name=username]").focus();
  37. localStorage.removeItem("resetCode");
  38. $("input[name=password]").css("border", "1px solid #eee").css("color", "#eee");
  39. break;
  40. }
  41. });
  42.  
  43. socket.on("login", function(data) {
  44. if (data.result === 0) {
  45. localStorage.setItem("hasAccount", true);
  46. localStorage.setItem("isRemembering", isRemembering());
  47.  
  48. if (isRemembering()) {
  49. localStorage.setItem("username", data.username);
  50. } else {
  51. sessionStorage.setItem("username", data.username);
  52. }
  53.  
  54. sessionStorage.setItem("code", data.code);
  55. window.location.href = "http://localhost/index.html";
  56. } else {
  57. displayInfo(data.info);
  58. }
  59. });
  60.  
  61. function getPasswordStrength(password) {
  62. let strength = 0,
  63. checks = {
  64. isSufficientLength: password.length >= 8,
  65. hasUppercase: password.match(new RegExp('[A-Z]')),
  66. hasLowercase: password.match(new RegExp('[a-z]')),
  67. hasNumber: password.match(new RegExp('[0-9]')),
  68. hasSpecialCharacter: password.match(new RegExp(/[-!@#$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/))
  69. };
  70.  
  71. if (password !== "") {
  72. for (let check in checks) {
  73. if (checks[check]) strength += 1;
  74. }
  75. }
  76.  
  77. return strength;
  78. }
  79.  
  80. function checkPasswordStrength(password) {
  81. let strength = getPasswordStrength(password),
  82. field = $("input[name=password]"),
  83. minimumStrength = 3;
  84.  
  85. if (strength >= minimumStrength) {
  86. // TODO
  87. }
  88.  
  89. // Set border and color of password box to signify strength.
  90. switch (strength) {
  91. case 1: field.css("border", "1px solid #B64E4E").css("color", "#B64E4E"); break;
  92. case 2: field.css("border", "1px solid #B6864E").css("color", "#B6864E"); break;
  93. case 3: field.css("border", "1px solid #B6B44E").css("color", "#B6B44E"); break;
  94. case 4: field.css("border", "1px solid #57B64E").css("color", "#57B64E"); break;
  95. case 5: field.css("border", "1px solid #4E72B6").css("color", "#4E72B6"); break;
  96.  
  97. default: field.css("border", "1px solid #eee").css("color", "#eee"); break;
  98. }
  99. }
  100.  
  101. function isRemembering() {
  102. if ($("span.remember").text().includes("○")) return false;
  103.  
  104. return true;
  105. }
  106.  
  107. function displayInfo(message) {
  108. $("p.info").html(message);
  109. $("p.info").slideDown();
  110. setTimeout(function() {
  111. $("p.info").slideUp();
  112. }, 3000);
  113. }
  114.  
  115. function hasAccount() {
  116. if (localStorage.getItem('hasAccount')) {
  117. $("div.login").show();
  118. $("div.register").hide();
  119. return true;
  120. } else {
  121. $("div.login").hide();
  122. $("div.register").show();
  123. return false;
  124. }
  125.  
  126. return null;
  127. }
  128.  
  129. function login() {
  130. if ($("div.login input[name=username]").val() !== "" &&
  131. $("div.login input[name=password]").val() !== "") {
  132.  
  133. console.log("Attempting login, please wait.");
  134. socket.emit("login", {
  135. username: $("div.login input[name=username]").val(),
  136. password: $("div.login input[name=password]").val()
  137. });
  138. } else {
  139. displayInfo("Username or password cannot be empty.");
  140. }
  141. }
  142.  
  143. function register() {
  144. if ($("div.register input[name=username]").val() !== "" &&
  145. $("div.register input[name=password]").val() !== "") {
  146.  
  147. if ($("div.register input[name=password").val()
  148. === $("div.register input[name=repeatPassword]").val()) {
  149.  
  150. socket.emit("register", {
  151. username: $("div.register input[name=username]").val(),
  152. password: $("div.register input[name=password]").val(),
  153. confirm: $("div.register input[name=repeatPassword]").val(),
  154. question: $("div.register input[name=question]").val(),
  155. answer: $("div.register input[name=answer]").val()
  156. });
  157.  
  158. } else {
  159. displayInfo("Passwords do not match.");
  160. }
  161.  
  162. } else {
  163. displayInfo("Username or password cannot be empty.");
  164. }
  165. }
  166.  
  167. hasAccount();
  168.  
  169. $(".register input[name=password]").keyup(function() {
  170. checkPasswordStrength($(this).val());
  171. });
  172.  
  173. $("div.register input[name=answer]").keyup(function(e) {
  174. if (e.which == 13) register();
  175. });
  176.  
  177. $(".login input[name=password]").keyup(function(e) {
  178. if (e.which == 13) login();
  179. });
  180.  
  181. $("div.login button.login").click(function() {
  182. login();
  183. });
  184.  
  185. $("div.register button.register").click(function() {
  186. register();
  187. });
  188.  
  189. $(".double-button .login").click(function() {
  190. $("div.login").show();
  191. $("div.register").hide();
  192. $("div.recovery").hide();
  193. $("input[name=password]").css("border", "1px solid #eee").css("color", "#eee");
  194. });
  195.  
  196. $(".double-button .register").click(function() {
  197. $("div.login").hide();
  198. $("div.register").show();
  199. $("div.recovery").hide();
  200. if ($(".register input[name=password]").val() !== "") {
  201. checkPasswordStrength($(".register input[name=password]").val());
  202. }
  203. });
  204.  
  205. $("span.forgot").click(function() {
  206. $("div.login").hide();
  207. $("div.register").hide();
  208. $("div.recovery").show();
  209. });
  210.  
  211. $(".get-question").click(function() {
  212. socket.emit("recovery",
  213. {type: 0, username: $("div.recovery input[name=username]").val()});
  214. });
  215.  
  216. $(".submit-recovery").click(function() {
  217. socket.emit("recovery",
  218. {type: 1, username: $("div.recovery input[name=username]").val(),
  219. answer: $("div.recovery input[name=answer]").val()});
  220. });
  221.  
  222. $(".set-password").click(function() {
  223. if ($("div.password input[name=password]").val() !== "") {
  224. socket.emit("recovery",
  225. {type: 4, username: $("div.recovery input[name=username]").val(),
  226. password: $("div.password input[name=password]").val(),
  227. resetCode: sessionStorage.getItem("resetCode")});
  228. } else {
  229. displayInfo("The password cannot be nothing.");
  230. }
  231. });
  232.  
  233. $("span.remember").click(function() {
  234. if ($(this).text().includes("○")) {
  235. $(this).text("● Remember me?");
  236. } else {
  237. $(this).text("○ Remember me?");
  238. }
  239. });
Add Comment
Please, Sign In to add comment