How to add auth to your Node.js CLI using WorkOS
Authenticate users in your Node.js command-line tool with a secure OAuth 2.0 Device Code flow using WorkOS. This tutorial shows how to implement login via the terminal, step by step.
Modern CLI apps often need to authenticate users — and using a simple web-based OAuth flow is the cleanest way to do it.
In this tutorial, we’ll implement a CLI login flow using WorkOS CLI Auth to authenticate users from the command line by using a web browser. Based on the OAuth 2.0 Device Authorization Grant, this method is perfect for CLIs where embedding a browser isn't feasible.
In this guide, we’ll walk through how to add auth to your Node.js CLI using WorkOS, step by step.
What we will build
You’ll create a Node.js CLI that:
- Requests a device code from WorkOS: The CLI starts the login flow by contacting WorkOS to get a device_code (for itself) and a user_code (for the user), along with a URL the user should visit.
- Displays a user-friendly prompt: The CLI prints the user_code and verification URL in the terminal, and optionally opens the browser automatically for the user to log in and approve.
- Polls WorkOS while the user logs in: As the user completes authentication in the browser, the CLI quietly checks in with WorkOS every few seconds to see if the user has completed the login.
- Exchanges the device code for tokens: Once authorized, the CLI receives an access token ready to make authenticated API calls.
This flow is ideal for tools running in environments that can’t easily embed a browser window (like the terminal), and it avoids the need to paste auth tokens manually.
Let’s dive in.
Prerequisites
Before starting, make sure you have:
- A WorkOS account.
- A CLI app registered as a public app. You can configure one in the WorkOS dashboard > Applications. Note down the Client ID.
- NodeJS 18+ installed.
- You must use AuthKit for your authentication UI (for info on how to customize the look-and-feel see our docs).
Step 1: Start the auth flow from the CLI
Start by initiating the device authorization flow from your CLI tool. This will generate a user-facing code and a verification URL.
This function hits the WorkOS Device Authorization endpoint and retrieves:
- device_code: For your CLI only.
- user_code: To show the user.
- verification_uri_complete: A URL users can click to authorize.
- interval: How often to poll for the token.
Step 2: Prompt the user to authenticate
Display the user code and link in the terminal. Optionally, open the browser automatically.
Note the following:
- Always display the user_code and verification_uri to the user.
- Never show the device_code, it’s only used internally for polling.
Step 3: Poll for access tokens
While the user authenticates in their browser, your app should poll the token endpoint.
Note the following:
- Poll at the interval specified in the authorization response (every 5 seconds).
- Respect slow_down errors by increasing your polling interval.
- Stop polling when you receive access_denied or expired_token errors.
- Implement a reasonable timeout to avoid infinite polling.
The response looks like this:
Putting it all together
That’s it! You’ve just added OAuth-powered login to your CLI. Your users can now authenticate securely through the browser, without ever pasting tokens by hand.