Advertisement
banana111

client-side login of my real time chat

Dec 9th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. socket.on("create_password", ()=> {
  2.     console.log("starting pass");
  3.     CreatePassword();
  4. })
  5.  
  6. socket.on("login_password", ()=> {
  7.     LoginPassword();
  8. })
  9.  
  10. socket.on("correct_password", ()=> {
  11.     EndLoginPassword();
  12. })
  13.  
  14. socket.on("incorrect_password", ()=> {
  15.     console.log("recived inccorect pass");
  16.     IncorrectPassword();
  17. })
  18.  
  19. socket.on("end_login", () => {
  20.     EndLogin();
  21. });
  22.  
  23. console.log("test");
  24.  
  25. function LIstenMessage(){
  26.     document.addEventListener('keydown', function(event) {
  27.         DoMessage(event.key, this_user);
  28.     });
  29. }
  30.  
  31. function StartLogin(){
  32.     if(localStorage.username){
  33.         this_user.username = localStorage.username;
  34.         AllreadyLogedin();
  35.     }else{
  36.         UsernameLogin();
  37.     }
  38. }
  39.  
  40. function AllreadyLogedin(){
  41.     $("#login_window").modal();
  42.     $("#username_container").remove();
  43.     $("div p").html("Pozdravljeni nazaj ");
  44.     $("div h6").html(this_user.username+" !");
  45.     $("div h4").html("Login - "+this_user.username);
  46.     EndLogin();
  47. }
  48.  
  49. write_field_id="login_username";
  50. function UsernameLogin(){
  51.     console.log("test1");
  52.     $("#login_window").modal();
  53.     $("body").keydown(function (event) {
  54.         console.log("test 2");
  55.         WriteToField(event.key, document.getElementById(write_field_id));
  56.     });
  57. }
  58.  
  59.  
  60.  
  61. function CreatePassword(){
  62.     $("div p").html("To uporabniško ime še ne obstaja! Prosim ustvarite si nov račun in vpišite svoje novo poljubno ");
  63.     $("div h6").html("geslo:");
  64.     $("div h4").html("Login - "+this_user.username);
  65.     password_container = $(`<div id='password_container' class='input-group col-md-9 pl-0 mt-4'>
  66.                             <input type='password' id='create_password' name='create_password' class='form-control form-control-lg'>
  67.                         </div>`);
  68.            
  69.     $(".modal-body").append(password_container);
  70.    
  71.     write_field_id = "create_password";
  72. }
  73.  
  74. function RepeatPassword(){
  75.     if($("#matching_error_container")){
  76.         $("#matching_error_container").remove();
  77.     }
  78.     repeat_password_container = $(`<div id='repeat_password_container' class='input-group col-md-11 pl-0 mt-4'>
  79.                                     <input type='password' id='create_repeat_password' name='create_repeat_password' class='form-control form-control-lg'>
  80.                                     <h3 for="create_username" class="ml-4 my-auto">ponovi geslo</h3>
  81.                                </div>`);
  82.  
  83.     $(".modal-body").append(repeat_password_container);
  84.     write_field_id = "create_repeat_password";
  85. }
  86.  
  87. function PasswordNotMatching(){
  88.     $("#repeat_password_container").remove();
  89.     $("input[name='create_password']").val("");
  90.     matching_error = $(`<div id='matching_error_container' class='input-group col-md-11 pl-0 mt-4'>
  91.                             <h4 class="ml-4 my-auto text-danger">Gesli se nista ujemali, prosim vpišite svoje poljubno geslo še enkrat</h4>
  92.                         </div>`);
  93.  
  94.     $(".modal-body").append(matching_error);
  95.     write_field_id = "create_password";
  96. }
  97.  
  98. function EndCreatePassword(){
  99.     $("#password_container").remove();
  100.     $("#repeat_password_container").remove();
  101.     $("div p").html("Vaš račun je bil ustvarjen pod uporabniškim imenom: ");
  102.     $("div h6").html(this_user.username);
  103. }
  104.  
  105. function LoginPassword(){
  106.     $("#text_container").remove();
  107.     text_container = $(`<div id='text_container'>
  108.                                 <p style="display: inline;" class="login_text ">Pozdravljeni nazaj </p>
  109.                                 <h6 style="display: inline;" class="login_text">`+this_user.username+`</h6>
  110.                                 <p style="display: inline;" class="login_text "> ! Prosim vpišite svoje </p>
  111.                                 <h6 style="display: inline;" class="login_text">geslo:</h6>
  112.                         </div>`);
  113.     $(".modal-body").append(text_container);
  114.     $("div h4").html("Login - "+this_user.username);
  115.    
  116.     password_container = $(`<div id='password_container' class='input-group col-md-9 pl-0 mt-4'>
  117.                             <input type='password' id='login_password' name='login_password' class='form-control form-control-lg'>
  118.                         </div>`);
  119.            
  120.     $(".modal-body").append(password_container);
  121.    
  122.     write_field_id = "login_password";
  123. }
  124.  
  125. function IncorrectPassword(){
  126.     $("input[name='login_password']").val("");
  127.     console.log("nsadsad");
  128.     if(!$("#incorrect_password_container").length){
  129.         console.log("ni mi več!");
  130.         text_container = $(`<div id='incorrect_password_container' class='input-group col-md-11 pl-0 mt-4'>
  131.                                 <h4 class="ml-4 my-auto text-danger">Vnešeno geslo je bilo napačno za uporabniško ime: </h4>
  132.                                 <h4 class="ml-4 my-auto text-danger">`+this_user.username+`. Prosim poskusite še enkrat</h4>
  133.                             </div>`);
  134.         $(".modal-body").append(text_container);
  135.     }
  136.    
  137. }
  138.  
  139. function EndLoginPassword(){
  140.     if($("#incorrect_password_container").length){
  141.         $("#incorrect_password_container").remove();
  142.     }
  143.     $("#password_container").remove();
  144.     $("#text_container").remove();
  145.     text_container = $(`<div id='text_container'>
  146.                                 <p style="display: inline;" class="login_text ">Prijava Uspešna! Veliko zabave </p>
  147.                                 <h6 style="display: inline;" class="login_text">`+this_user.username+`</h6>
  148.                         </div>`);
  149.     $(".modal-body").append(text_container);
  150.     EndLogin();
  151.     localStorage.setItem("username", this_user.username);
  152. }
  153.  
  154. function EndLogin(){
  155.     setTimeout(function(){$(".modal").modal("hide"); LIstenMessage();}, 2000);
  156.     $("body").unbind("keydown");
  157.     socket.emit("logged_in", this_user.username);
  158. }
  159.  
  160. StartLogin();
  161.  
  162. function WriteToField(data, field){
  163.     allowed_keys = " ¸1234567890'+qwertzuiopšđasdfghjklčćžyxcvbnm,.-<¨!\"#$%&/()=?*QWERTZUIOPŠĐASDFGHJKLČĆŽ>YXCVBNM;:_¨~ˇ^˘°˛`˙´˝¨¸|€÷×[]łŁß¤@{}<>";
  164.     if(allowed_keys.includes(data)||data==""){
  165.         field.value+=data;
  166.     }
  167.     else if(data == "Enter"){
  168.         if($(field).attr("id")=="login_username"){
  169.             console.log("bout to send username");
  170.             socket.emit("username",field.value);
  171.             this_user.username = field.value;
  172.             $("#username_container").remove();
  173.             //$("body").unbind("keydown");
  174.         }
  175.         if($(field).attr("id")=="create_password"){
  176.             RepeatPassword();
  177.         }
  178.         if($(field).attr("id")=="create_repeat_password"){
  179.             if ($("input[name='create_password']").val() != $("input[name='create_repeat_password']").val()){
  180.                 PasswordNotMatching();
  181.             }else{
  182.                 EndCreatePassword();
  183.                 send_data = [];
  184.                 send_data.push(this_user.username);
  185.                 send_data.push(field.value);
  186.                 socket.emit("save_password",send_data);
  187.             }
  188.         }
  189.         if($(field).attr("id")=="login_password"){
  190.             send_data = [];
  191.             send_data.push(this_user.username);
  192.             send_data.push(field.value);
  193.             socket.emit("check_password", send_data);
  194.         }
  195.     }
  196.     else if(data == "Backspace"){
  197.         if(field.value!=""){
  198.             cur_data = field.value;
  199.             field.value = cur_data.substring(0,cur_data.length-1);
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement