3

Goal

I am attempting to create my first CLI using Node JS following the instructions outlined in a tutorial on Sitepoint (refer: https://www.sitepoint.com/javascript-command-line-interface-cli-node-js/).

For those attempting to ask why I would create it in Node this is an exercise for myself and not looking to optimize my efforts by using another means, but thank you for the consideration

Problem

I am trying to establish a connection to Github allowing users of an organization to put in their credentials (username, password and possibly 2FA code) to connect to an organization account in order to access repositories.

Currently I am able to stuck on the part where I utilize Octokit to pass credentials to github to receive and store its token.

Error: UnhandledPromiseRejectionWarning: HttpError: Invalid one-time password for two-factor authentication

Code:

module.exports = {
  getPath: () => {
    return conf.path;
  },
  getInstance: () => {
    return octokit;
  },
  getStoredGithubToken: () => {
    return conf.get('github.token');
  },
  setGithubCredentials: async () => {
    const credentials = await inquirer.askCredentials();
    _.extend(credentials, {
      async on2fa () {
        const questions = {
          name: 'authorization code',
          type: 'input',
          message: 'Two-factor authentication Code:',
          validate: function (value) {
            if (value.length) {
              return true;
            } else {
              return 'Two-factor authentication Code:';
            }
          }
        }
        return questions;
      }
    });
    module.exports.registerNewToken(credentials);
  },
  registerNewToken: async (credentials) => {
    const status = new Spinner('Authenticating you, please wait...');
    status.start();
    credentials.on2fa();
    try {
      const response = await octokit({
        auth: credentials
      });
      response.repos.listForOrg({
        org: '<organization>',
        type: '<type>'
      }).then(({data,status,headers})=>{
        console.log({data,status,headers});
      })
      const token = response;
      if (token) {
        conf.set('github.token', token);
      } else {
        throw new Error("Missing Token", "GitHub token was not found in the response");
      }
    } catch (err) {
      throw err;
    } finally {
      status.stop();
    }
  }
}
Isaiah Davis
  • 81
  • 2
  • 9

0 Answers0