OAuth in Node.js CLI Apps

OAuth in Node.js CLI Apps

OAuth in Node.js CLI Apps. OAuth is a popular tool for logging in to web apps, but Node makes it easy to use OAuth for CLI apps too. Here's how you can add OAuth support to a Slack CLI app.

I recently wrote about  building a CLI app in Node.js, which showed how to build a CLI app that sent Slack messages from Node.js. But it omitted a key detail:  how to get a Slack OAuth token. And that's a big omission, because getting auth credentials right is typically the hardest part of working with any API. In this article, I'll describe how you can use  Express and  open to get an OAuth token for a CLI app.

Review

In the  previous tutorial, I explained how to add a login command to your Slack CLI that you could run using node ./index.js login, or as slack-cli login after bundling for npm. Below is how you can implement this command using  yargs:

const Configstore = require('configstore');
const { prompt } = require('enquirer');
const yargs = require('yargs');

// `slack-cli` is the name of the file that configstore will use.
const config = new Configstore('slack-cli');

yargs.command(
  'login',
  'Set your bot token',
  () => {},
  async function handler(argv) {
    // Open an enquirer prompt to ask for the user's token
    const { token } = await prompt({
      type: 'password',
      name: 'token',
      message: 'Enter your Slack bot token:'
    });

    // Persist the token to the user's disk using configstore
    config.set({ token });
    console.log('Token stored successfully!');
  });

This login command is simple, but it puts the responsibility of  setting up a Slack app on your user, and setting up a Slack app is an intricate UI-based process that is hard to automate. If you want to build a CLI tool on top of an API, you're better off using  OAuth.

If you use  OAuth, you can create an app once and then, when a user logs in, you get a token for that one Slack OAuth app.

node.js node

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Top Node JS Developers | Best Node.js Development Company India

Hire dedicated Node JS developers & programmers in India for custom full-stack NodeJS web development projects on hourly/full-time basis. Strict NDA, 16+ years exp & 2500+ clients|450+ Experts

Hire Dedicated Node.js Developers - Hire Node.js Developers

Get business-centric Node.Js development services from expert Node.JS developers. We have expertise in developing & maintaining Node JS apps as per the business requirements.

Node JS Development Company| Node JS Web Developers-SISGAIN

SISGAIN is the top rated node js development company providing professional services on node js web and mobile development.

Node JS Development Company | Hire Node.js Developers

Looking to hire Node js developers? One of the top Node js development companies in India & USA offers cost-effective Node js web development services.

Top Node js Development Company India | Node js Web Development Services

Looking to outsource a Node js Development Company? ValueCoders has been adding value to businesses with a diverse range of Nodejs development solutions for a decade. 4200+ projects | 450+ Experts | 16+ Yrs Exp