Last active
February 25, 2019 13:04
-
-
Save k0d3d/d82bc7f11ce8c2b87c1a7b1109309af9 to your computer and use it in GitHub Desktop.
deny if parameters and body is not set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require("puppeteer"); | |
module.exports = async function (context, req) { | |
context.log("Started working function"); | |
if (!req.body || | |
!req.body.username || | |
!req.body.password) { | |
context.res = { | |
status: 400, | |
body: "Request Parameters not set // expecting {username: 'xxx', password: 'xxx'} in req.body" | |
} | |
return | |
} | |
const browser = await puppeteer.connect({ | |
browserWSEndpoint: "ws://d.ixit.com.ng" | |
}); | |
const page = await browser.newPage(); | |
await page.goto("https://www.instagram.com/accounts/login"); | |
await page.setViewport({ width: 1237, height: 670 }); | |
await page.waitForSelector("[name=username]"); | |
await page.type("[name=username]", req.body.username); | |
await page.type("[name=password]", req.body.password); | |
await page.click("[type=submit]"); | |
try { | |
await page.waitForNavigation({ timeout: 2000 }); | |
await page.goto("https://www.instagram.com/accounts/activity/"); | |
const isLoggedIn = await page.$("h1"); | |
if (isLoggedIn) { | |
context.res = { | |
body: 'Session Saved', | |
headers: { | |
"Content-Type": "text/html" | |
} | |
}; | |
} else { | |
// for some reasons, like 2FA or IGs, | |
// or any other thing wrong, | |
// return the current page html. So I can | |
// troubleshoot | |
context.res = { | |
body: await page.content(), | |
headers: { | |
"Content-Type": "text/html" | |
} | |
}; | |
} | |
} catch (e) { | |
await page.waitForSelector("[role=alert]"); | |
const alertElement = await page.$eval( | |
"[role=alert]", | |
node => node.innerHTML | |
); | |
context.res = { | |
status: 401, | |
body: alertElement | |
}; | |
} | |
await browser.close(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment