Created
March 2, 2019 16:53
-
-
Save k0d3d/493246efc12725b4c7819d935269a78a to your computer and use it in GitHub Desktop.
using utils and strategy
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"); | |
const {returnResponse} = require('../utils') | |
module.exports = async function(context, req, pageCookiesIn) { | |
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.setViewport({ width: 1237, height: 670 }); | |
await page.goto("https://www.instagram.com/accounts/login"); | |
let userHasExisitingSession = false | |
if (pageCookiesIn) { | |
userHasExisitingSession = Object.values(context.bindings.pageCookiesIn[0]) | |
userHasExisitingSession = userHasExisitingSession.filter(o => o instanceof Object) | |
await page.setCookie(...userHasExisitingSession) | |
} | |
// attempt login | |
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 }); | |
let ob = { ...await page.cookies(), id: req.body.username } | |
// Cosmos DB output binding | |
// create a new document | |
context.bindings.pageCookiesOut = JSON.stringify(ob); | |
// start testing strategies | |
await returnResponse(context, page) | |
await browser.close(); | |
} catch (e) { | |
// the page does not refresh because, of .... | |
// many possible errors. | |
// Instagram displays an error alert message | |
// if authentication fails. | |
try { | |
await page.waitForSelector("[role=alert]"); | |
const alertElement = await page.$eval( | |
"[role=alert]", | |
node => node.innerHTML | |
); | |
// check for that then respond | |
returnResponse(context, page, 401) | |
} catch (e) { | |
returnResponse(context, page, 500) | |
} | |
await browser.close(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment