Guest User

Untitled

a guest
Mar 15th, 2018
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function(){
  2.     $('#username').on('focus',function(){
  3.         $('#error-messages').html('');
  4.     });
  5.     $('#password').on('focus',function(){
  6.         $('#error-messages').html('');
  7.     });
  8.  
  9.     $('#login-btn').on('click', function() {
  10.         let valid = true;  
  11.  
  12.         if (valid && $('#username').val() == '') {
  13.             $('#error-messages').html('please enter your username');
  14.             valid = false;
  15.         }
  16.    
  17.         if (valid && $('#password').val() == '') {
  18.             $('#error-messages').html('please enter your password');
  19.             valid = false;
  20.         }    
  21.         return valid;
  22.     });
  23.  
  24.     $('.form-horizontal').on('submit',function(e){
  25.         e.preventDefault();
  26.         $.ajax({url:'/login',
  27.             method:'POST',
  28.             contentType:'application/json',
  29.             data:JSON.stringify({
  30.                 username:$('#username').val(),
  31.                 password:$('#password').val()
  32.             }),
  33.             success:function(response){
  34.                 $('#password').val('');
  35.                 if(response.type){
  36.                     $('#error-messages').html(response.message);
  37.                 }
  38.                 else if(response.task){
  39.                     $('#error-messages').html('Login Success with ID '+response.id);
  40.                 }
  41.             }
  42.         });
  43.     });
  44. });
  45.  
  46.  
  47. app.post('/login',function (req,res){
  48.     let loginObject=new controller();
  49.     loginObject.verifyLogin(req.body.username,req.body.password,function(data){
  50.         if(data == 201){
  51.             console.log('Invalid Login Credentials');
  52.             res.json({
  53.                 message:'Username and Password Not Found',
  54.                 type:'ERROR',
  55.                 code:data
  56.             });
  57.         }
  58.         else if(data.task){
  59.             console.log(data);
  60.             res.json(data);
  61.         }
  62.         else{
  63.             console.log(data);
  64.             res.json({
  65.                 message:'Username and Password Not Found',
  66.                 type:'ERROR',
  67.                 code:data
  68.             });
  69.         }
  70.     });
  71. });
Add Comment
Please, Sign In to add comment