node.jscommand-line-interfacevorpal.js

Is it possible to only allow vorpal commands after a user has provided correct details


I have a command that a user will provide their authentication details to and then once that is confirmed and valid, they can use any of the other commands.

Ideally I would like to go like this

$ app auth
  # User provides auth details
$ app another_command
  # Can now acces commands

However if a user tries to access another_command before signing in it should show them a message saying to run the auth command.

Whats the best way of doing this in Vorpal


Solution

  • I would just make an external method that would be called that does all of the heavy lifting, and throw it into the command.validate method on each Vorpal action.

    Sometihng like this should get you started:

    function authFunction() {
      return app.authenticated;
    }
    
    vorpal.command('foo')
      .validate(authFunction)
      .action(action);