Welcome to the final chapter of our telemetry module!
In the previous chapter, Secure Subprocess Execution, we learned how to run the GitHub command line tool securely without leaking user passwords.
Now, we have one final decision to make: Which command should we run?
We have two options to check if a user is logged in. One is slow and depends on the internet. The other is instant and works offline. In this chapter, we will learn why choosing the "Local-Only" option is critical for a fast, responsive application.
Imagine you are standing in front of your house. You want to know: "Can I get inside?"
You have two ways to find out:
You call a locksmith service. You wait on hold. They look up your address in a database. They verify you are the owner. Then they tell you via phone, "Yes, you have access."
You reach into your pocket. Do you feel a key?
The Use Case: When our application starts, we want to know if the user is logged into GitHub so we can enable certain features.
We want the Pocket Check. We want to verify the user has a "Key" (Token) on their computer without calling the "Locksmith" (GitHub Servers).
To make our software feel professional, we need to minimize two things:
The GitHub CLI (gh) gives us two commands that look similar but act very differently.
gh auth status
This command is the "Locksmith." It connects to api.github.com to verify your session.
gh auth tokenThis command is the "Pocket Check." It only looks at the configuration files stored on your hard drive to see if a token exists.
In our code, we simply choose the right arguments.
// Ideally, we prefer the command that stays local
const command = 'gh'
const args = ['auth', 'token'] // <--- This is the local check
// AVOID this for simple checks:
// const args = ['auth', 'status']
Let's visualize the journey our data takes with both approaches.
This involves a round-trip across the world.
This never leaves the user's computer.
Let's look at our final ghAuthStatus.ts file one last time. We are combining everything we learned in this tutorial.
which check.stdout: ignore.['auth', 'token'] arguments.// ghAuthStatus.ts
import { execa } from 'execa'
import { which } from '../which.js' // From Chapter 3
export async function getGhAuthStatus() {
// 1. Check if tool exists (Chapter 3)
const ghPath = await which('gh')
if (!ghPath) return 'not_installed'
// 2. Local-Only Verification (Chapter 5)
// We use 'token' instead of 'status' for speed
const { exitCode } = await execa('gh', ['auth', 'token'], {
stdout: 'ignore', // Security (Chapter 4)
stderr: 'ignore',
reject: false,
})
// 3. Return State (Chapter 2)
return exitCode === 0 ? 'authenticated' : 'not_authenticated'
}
Explanation:
By specifically choosing ['auth', 'token'], we ensure that execa finishes almost instantly. The gh tool simply opens a text file in the user's home folder, sees the text string, and exits with code 0. We don't care what the token is, only that the computer has one.
In this final chapter, we optimized our telemetry system.
gh auth token to ensure our app works offline and starts up instantly.Congratulations! You have completed the module.
You have built a sophisticated system that:
You are now ready to start building features that use these checks to interact with GitHub safely!
Generated by Code IQ