-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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); |
There was a problem hiding this comment.
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++; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
andyargs
would callexit 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.
No description provided.