Skip to content

Commit

Permalink
fix login method (#38)
Browse files Browse the repository at this point in the history
* fix login method

* undo constructor changes

---------

Co-authored-by: Elijah Wright <elijah@elijahs.space>
  • Loading branch information
sooluh and elijah-wright authored Jul 10, 2023
1 parent 9f9190f commit 041f9bd
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { parseBloksResponse } = require("./util/Bloks.js");
const androidId = (Math.random() * 1e24).toString(36);

class Client extends EventEmitter {
constructor({ token, userAgent, appId }) {
constructor({ token, userAgent, appId ) {
super({ captureRejections: true });

this.token = token || null;
Expand All @@ -30,7 +30,7 @@ class Client extends EventEmitter {
async login(username, password) {
const base = "https://i.instagram.com";
const loginUrl = "/api/v1/bloks/apps/com.bloks.www.bloks.caa.login.async.send_login_request/";

const params = {
client_input_params: {
password: password,
Expand All @@ -42,20 +42,20 @@ class Client extends EventEmitter {
device_id: `android-${androidId}`,
},
};

const bkClientContext = {
bloks_version:
"5f56efad68e1edec7801f630b5c122704ec5378adbee6609a448f105f34a9c73",
styles_id: "instagram",
};

const requestBody = {
params: JSON.stringify(params),
bk_client_context: JSON.stringify(bkClientContext),
bloks_versioning_id:
"5f56efad68e1edec7801f630b5c122704ec5378adbee6609a448f105f34a9c73",
};

const requestOptions = {
method: "POST",
headers: {
Expand All @@ -69,23 +69,24 @@ class Client extends EventEmitter {
requestBody.bk_client_context,
)}&bloks_versioning_id=${requestBody.bloks_versioning_id}`,
};

const response = await fetch(base + loginUrl, requestOptions);
const text = await response.text();
const bloks = parseBloksResponse(text);

if (bloks.two_factor_required) {
const {
two_factor_identifier,
trusted_notification_polling_nonce
} = bloks.two_factor_info

console.log('Please approve the login request on your Instagram')


const self = this;
const token = await new Promise((resolve) => {
const statusUrl = "/api/v1/two_factor/check_trusted_notification_status/";
const verifyUrl = "/api/v1/accounts/two_factor_login/";

const interval = setInterval(async function () {
requestOptions.body = new URLSearchParams({
two_factor_identifier: two_factor_identifier,
Expand All @@ -94,10 +95,10 @@ class Client extends EventEmitter {
trusted_notification_polling_nonces:
JSON.stringify([trusted_notification_polling_nonce]),
}).toString();

const response = await fetch(base + statusUrl, requestOptions);
const json = await response.json();

if (json.review_status === 1) {
requestOptions.body = new URLSearchParams({
signed_body: 'SIGNATURE.' + JSON.stringify({
Expand All @@ -110,25 +111,31 @@ class Client extends EventEmitter {
verification_method: '4'
})
}).toString()

const response = await fetch(base + verifyUrl, requestOptions);
const json = await response.json();
const header = response.headers.get('Ig-Set-Authorization');


if (json.logged_in_user.pk) {
self.userId = json.logged_in_user.pk;
}

clearInterval(interval);
const token = header.replace("Bearer IGT:2:", "") || null

resolve(token);
}
}, 2_500);
});

this.token = token;
return;
}

if (bloks.login_response && bloks.login_response.logged_in_user.pk) {
if (bloks.login_response.logged_in_user.pk) {
this.userId = bloks.login_response.logged_in_user.pk_id;
}

this.token = bloks.headers?.["IG-Set-Authorization"].replace("Bearer IGT:2:", "");
}
}
Expand Down

0 comments on commit 041f9bd

Please sign in to comment.