Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start new auth flow on 401 responses #339

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

kmerenkov
Copy link

No description provided.

@kmerenkov kmerenkov requested a review from adi-griever March 21, 2025 23:33
@kmerenkov kmerenkov changed the title feat: delete local credentials on 401 responses feat: start new auth flow on 401 responses Mar 22, 2025
@kmerenkov kmerenkov marked this pull request as ready for review March 22, 2025 00:41
Copy link
Contributor

@PlayLikeNeverB4 PlayLikeNeverB4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok overall, but please retest it carefully because it changed some core code.

const credentialsPath = join(configDir, CREDENTIALS_FILE);
try {
if (existsSync(credentialsPath)) {
unlinkSync(credentialsPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to use rmSync just like everywhere else.

// noop
process.exit(0);
} catch (err) {
attempts++;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should show number of attempts to the user somewhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an implementation detail and not for retrying networking requests, that's why I think having a counter is unnecessary at this point. We may handle it differently in the future 🤷

Let me explain why it's there. Maybe it's not very clear from the PR.

If a user had invalid credentials:

  • ensureAuth would not error out because credentials are present on the file system
  • the execution would go to the command itself, for example, to project list
  • it'd raise an error due to 401 and yargs would call exit 1 under the hood because it has .fail(async (msg, err) => { and the rest of the error handling

This design makes it impossible to re-enter the command execution if it fails.
So, I had to change the design.

First, I removed the error handler from yargs and extracted it into a separate function called handleError.
Second, I removed the error handler from yargs with this line: .fail(false);

From the doc:

Providing false as a value for fn can be used to prevent yargs from exiting and printing a failure message. This is useful if you wish to handle failures yourself using try/catch and .getHelp().

Finally, third, I had to introduce a loop to call const args = await builder.argv;, call the handleError that handles authentication on auth error, does many other things, and indicates that the command can be retried. In our case, this is because handleError will delete invalid credentials, and on the next retry ensureAuth from the yargs pipeline will call const apiKey = await authFlow(props);.

Phew, I think I didn't forget anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants