javascriptjqueryjquery-terminal

Jquery Terminal register & login


I have question, i want user register then login but here it's error anyone can help me ? i saw the documentation . here is link of the documentation jquery termi[jquery terminal]1nal

here is my script:

if (command == 'register') {

        term.push(function(command, term) {
            term.pause();

            $.ajax({
                    url: "register.php",
                    type: "post",
                    data: {data_register : command },
                    success: function (msg) {
                       term.echo(yellow('Data Saved Successfully !'));

                       term.resume();
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                       term.resume();
                    }

                });

        }, 
        {

          prompt: " > "
        });

    } else if (command == 'login'){
      login: function(user, password, callback) {
        if (user == 'demo' && password == 'secret') {
            callback('SECRET TOKEN');
        } else {
            callback(null);
        }
      }
    } 

this line's error :

else if (command == 'login'){
          login: function(user, password, callback) {
            if (user == 'demo' && password == 'secret') {
                callback('SECRET TOKEN');
            } else {
                callback(null);
            }
          }
        } 

Thank you


Solution

  • You have invalid JavaScript you have label and function declaration, which should be put in an object like this:

    var x = {
       login: function() {
       }
    };
    

    which should be part of object in second argument which is options object.

    .terminal(function(command) {
    
       if (command === 'register') {
       }
    }, {
       login: function(...) {
          ...
       }
    });
    

    and for your exact problem on having login command, that login you in, you need to invoke login method like this:

    } else if (command == 'login'){
       term.login(function(user, password, callback) {
          // to get the token you should send user and password to the server
          if (user == 'demo' && password == 'secret') {
            callback('SECRET TOKEN');
          } else {
            callback(null);
          }
       });
    } else if (term.token()) {
       // here are commands that will only work after user login
       // but you need to know that user may add token in developer tools
       // so you should use additional check if token is valid by sending token
       // to the server, or if you're invoking command on the server then just
       // send the token and validate it there
    } else {
       term.error('invalid command');
    }