$(function () {
  if(Cookies.get('token')) {
    window.location.href = '/dash';
  }

  // ----- Inputs
  function err(input, msg) {
    var eElem = input.next()[0];

    if (eElem) {
      eElem.remove();
    }

    if (msg) {
      input.addClass('is-danger');
      input.after('<span class="help is-danger">' + msg + '</span>');
    } else {
      input.removeClass('is-danger');
    }
  }

  var usernameInput = $('#username');

  function checkUsername() {
    if (!usernameInput.val().trim()) {
      err(usernameInput, 'A username is required');
      return false;
    }
    err(usernameInput, '');
    return true;
  }
  usernameInput.blur(function () {
    checkUsername();
  });
  usernameInput.focus(function () {
    err(usernameInput, '');
  });

  var passwordInput = $('#password');

  function checkPassword() {
    if (!passwordInput.val().trim()) {
      err(passwordInput, 'A password is required');
      return false;
    }
    err(passwordInput, '');
    return true;
  }
  passwordInput.blur(function () {
    checkPassword();
  });
  passwordInput.focus(function () {
    err(passwordInput, '');
  });

  // ----- message
  var message = $('#message');

  function mess(msg) {
    message.show();

    $('#message-content').html(msg);
  }
  $('#message-hide').click(function () {
    message.hide();
  });

  // ----- form
  var submit = $('#submit');
  $('#signin').submit(function (event) {
    event.preventDefault();

    if (!checkUsername() || !checkPassword()) {
      return;
    }

    submit.attr('disabled', true).addClass('is-loading');

    $.ajax({
        type: "POST",
        url: 'http://127.0.0.1:8080/user',
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify({
          name: usernameInput.val().trim(),
          password: passwordInput.val().trim()
        })
      })
      .done(function (data) {

        // auto login
        $.ajax({
            type: "POST",
            url: 'http://127.0.0.1:8080/auth',
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify({
              username: usernameInput.val().trim(),
              password: passwordInput.val().trim()
            })
          })
          .done(function (data) {
            Cookies.set('token', data.token);
            window.location.href = '/dash';
          })
          .fail(function () {
            window.location.href = '/';
          });
      })
      .fail(function (jqXHR) {
        if (jqXHR.status === 401) {
          mess('Bad username or password');
          return;
        }
        if (jqXHR.status === 422) {
          err(usernameInput, 'This username is already taken');
          return;
        }
        mess('Oops, something went wrong');
      })
      .always(function () {
        submit.attr('disabled', false).removeClass('is-loading');
      });
  });
});

@@ -13,16 +13,17 @@
 ) %7B%0A  if
+ 
 (Cookies
@@ -461,21 +461,31 @@
 ) %7B%0A    
-if (!
+var username = 
 username
@@ -494,32 +494,51 @@
 put.val().trim()
+;%0A    if (!username
 ) %7B%0A      err(us
@@ -595,32 +595,145 @@
 rn false;%0A    %7D%0A
+    if (username.length %3C 3) %7B%0A      err(usernameInput, 'Your username is too short');%0A      return false;%0A    %7D%0A
     err(username
@@ -970,21 +970,31 @@
 ) %7B%0A    
-if (!
+var password = 
 password
@@ -1011,16 +1011,35 @@
 ).trim()
+;%0A    if (!password
 ) %7B%0A    
@@ -1104,32 +1104,145 @@
 rn false;%0A    %7D%0A
+    if (username.length %3C 3) %7B%0A      err(usernameInput, 'Your password is too short');%0A      return false;%0A    %7D%0A
     err(password

update signup error message