Skip to content

feat: allow 2-factor authentication - #17

Open
mateis-rgb wants to merge 1 commit into
noook:mainfrom
mateis-rgb:a2f
Open

feat: allow 2-factor authentication#17
mateis-rgb wants to merge 1 commit into
noook:mainfrom
mateis-rgb:a2f

Conversation

@mateis-rgb

Copy link
Copy Markdown

No description provided.

Comment threadsrc/domain/auth.ts
});

return ig.account.login(answers.username, answers.password);
return Bluebird.try(() => ig.account.login(answers.username, answers.password)).catch(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Is Bluebird really necessary for this ? A regular try/catch block should do the work

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I used the code which is an example on the Instagram-private-api, but I didn't ask myself the question of whether Bluebird was really useful or not, I'm going to do some testing and see if it changes anything

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

After re-examining the code, Bluebird is used to do try/catch with Promises so a classic try/catch block would not allow you to do that, especially since it catches the error in the catch but also a class "IgLoginTwoFactorRequiredError" which is used to type the error or something like that, from what I understood

@noooknoookFeb 21, 2024

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This should be strictly equivalent.

try{// return await within an async makes the function try to resolve the promise, if not it will fall in the following catch blockreturnawaitig.account.login(answers.username,answers.password)}catch(err){// Only handle IgLoginTwoFactorRequiredErrorif(!(errinstanceofIgLoginTwoFactorRequiredError)){throwe;// Unhandled error}// err should now be inferred as IgLoginTwoFactorRequiredErrorconst{ username, totp_two_factor_on, two_factor_identifier }=err.response.body.two_factor_infoconstverificationMethod=totp_two_factor_on ? '0' : '1';const{ code }=awaitinquirer.prompt([{type: 'input',name: 'code',message: `Enter code received via ${verificationMethod==='1' ? 'SMS' : 'TOTP'}`,},]);try{// Same thing as above, try to resolve the promise.returnawaitig.account.twoFactorLogin({
username,verificationCode: code,twoFactorIdentifier: two_factor_identifier,
verificationMethod,// '1' = SMS (default), '0' = TOTP (google auth for example)trustThisDevice: '1',// Can be omitted as '1' is used by default});}catch(e){// Log the error. Ideally, it should be rethrown because catching without rethrowing makes the process run as if there was no errorconsole.error(e)}}

Here is the complete explanation of return awaithttps://stackoverflow.com/a/62819622/8886385

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

so good, you found what was missing, will you update the github repo?

@noooknoook left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM, except for Bluebird I don't believe this dependency is necessary

@noooknoook changed the title A2f is added and fully functionalfeat: allow 2-factor authenticationFeb 20, 2024
Sign up for freeto 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.

2 participants

@mateis-rgb@noook