import { Result } from "../../gwarm/core/result"; import { AuthHandler } from "../../gwarm/handlers/auth.handler"; import { BrowserContext, Page } from "playwright"; import { AuthPayload } from "../../gwarm/payloads/auth.payload" import { AuthResult } from "../../gwarm/results/auth.result"; export class GmailAuthHandler extends AuthHandler { async handle(context: BrowserContext, payload: AuthPayload): Promise { const page = context.pages()[0]; await this.prepareLoginPage(page); await this.fillEmail(page, payload.email); await this.fillPassword(page, payload.password); await this.checkAndPassConfirmation(page, payload.confirmationEmail); await this.checkIfCanNotSignIn(page); await this.checkIfTheAccountIsDisabled(page); await this.checkIfLogedIn(page); return await this.getResult(context); } async prepareLoginPage(page: Page) { await page.goto('https://accounts.google.com/?hl=en', { waitUntil: "load" }); await page.waitForSelector("text=Sign in") } async fillEmail(page: Page, email: string) { await page.click("input[type=\"email\"]"); await page.fill("input[type=\"email\"]", email); await page.click("text=Next") await page.waitForResponse(async (res) => { return res.url().includes("https://accounts.google.com/_/lookup/accountlookup") }); await page.waitForTimeout(1000); const NotFound = await page.isVisible("text=Couldn’t find your Google Account"); if (NotFound) throw new Error("Couldn’t find your Google Account"); const HasCaptcha = await page.isVisible("#captchaimg"); if (HasCaptcha) throw new Error("You Have To Resolve Captcha"); } async fillPassword(page: Page, password: string) { await page.click("input[type=\"password\"]"); await page.fill("input[type=\"password\"]", password) await page.click("text=Next"); let m: any; await page.waitForResponse(async (res) => { m = await res.text() return res.url().includes( "https://accounts.google.com/_/signin/challenge" ) }) if (m.includes("INCORRECT")) { throw new Error("The Password is Incorrect"); } await page.waitForEvent("framenavigated", () => { return true; }) await page.waitForTimeout(1000); } async checkAndPassConfirmation(page: Page, confirmationEmail: string) { const hasConfirmation = await page.isVisible("text=Verify"); console.log("has confirmation", hasConfirmation); if (hasConfirmation) { await page.click("text=Confirm your recovery email") await page.click("input[type=\"email\"]") await page.fill("input[type=\"email\"]", confirmationEmail); await page.click("text=Next") } console.log("has confirmation finished") } async checkIfTheAccountIsDisabled(page: Page) { console.log("checkIfTheAccountIsDisabled started") const visible = await page.isVisible("text=Your account has been disabled"); if (visible) throw new Error(`Email is Disabled`) console.log("checkIfTheAccountIsDisabled finished") } async waitForPageLoading(page: Page) { await page.waitForNavigation(); } async checkIfLogedIn(page: Page) { let url = page.url(); if(url.includes("https://myaccount.google.com/")) return; await page.waitForNavigation({ url:(url)=>{ console.log(url); return url.toString().includes("https://myaccount.google.com/") }, }); url=page.url() if (!url.includes("https://myaccount.google.com/")) { throw new Error("Unknow Error this is page is not the logged page") } } async checkIfCanNotSignIn(page: Page) { console.log("checkIfCanNotSignIn start") const visible = await page.isVisible("text=Couldn’t sign you in"); if (visible) throw new Error("Couldn’t sign you in"); console.log("checkIfCanNotSignIn finished") } async getResult(context: BrowserContext): Promise { const result = new AuthResult(); result.token = await context.storageState(); return result; } }