Skip to content

Conversation

@mateis-rgb
Copy link

No description provided.

});

return ig.account.login(answers.username, answers.password);
return Bluebird.try(() => ig.account.login(answers.username, answers.password)).catch(
Copy link
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
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
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

Copy link
Owner

@noook noook Feb 21, 2024

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 block
  return await ig.account.login(answers.username, answers.password)
} catch (err) {
  // Only handle IgLoginTwoFactorRequiredError
  if (!(err instanceof IgLoginTwoFactorRequiredError)) {
    throw e; // Unhandled error
  }
  // err should now be inferred as IgLoginTwoFactorRequiredError
  const { username, totp_two_factor_on, two_factor_identifier } = err.response.body.two_factor_info
  const verificationMethod = totp_two_factor_on ? '0' : '1';
  const { code } = await inquirer.prompt([
    {
      type: 'input',
      name: 'code',
      message: `Enter code received via ${verificationMethod === '1' ? 'SMS' : 'TOTP'}`,
    },
  ]);
  try {
    // Same thing as above, try to resolve the promise.
    return await ig.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 error
    console.error(e)
  }
}

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

Copy link
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?

Copy link
Owner

@noook noook left a comment

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

@noook noook changed the title A2f is added and fully functional feat: allow 2-factor authentication Feb 20, 2024
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.

2 participants