diff --git a/prime-gaming.js b/prime-gaming.js index a6e502a..0ee1fce 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -34,6 +34,26 @@ await page.setViewportSize({ width: cfg.width, height: cfg.height }); // workaro const notify_games = []; let user; +const waitForSignedInOrMFA = async p => { + const otpLocator = p.locator('#auth-mfa-otpcode, input[name=otpCode]'); + const waitSignedIn = p.waitForURL('**/claims/**signedIn=true', { timeout: cfg.login_timeout }).then(() => true).catch(() => false); + const waitMFA = (async () => { + try { + await otpLocator.waitFor({ timeout: cfg.login_timeout }); + } catch { + return false; + } + await handleMFA(p); + try { + await p.waitForURL('**/claims/**signedIn=true', { timeout: cfg.login_timeout }); + } catch { + // if it still fails, caller will handle via timeout + } + return true; + })(); + await Promise.race([waitSignedIn, waitMFA]); +}; + const handleMFA = async p => { const otpField = p.locator('#auth-mfa-otpcode, input[name=otpCode]'); if (!await otpField.count()) return false; @@ -58,7 +78,7 @@ try { await page.click('input[type="submit"]'); await page.fill('[name=password]', password); await page.click('input[type="submit"]'); - await handleMFA(page).catch(() => {}); + await waitForSignedInOrMFA(page); try { await page.waitForURL('**/ap/signin**'); const error = await page.locator('.a-alert-content').first().innerText(); @@ -111,6 +131,7 @@ try { await page.click('input[type="submit"]'); await page.fill('[name=password]', password); await page.click('input[type="submit"]'); + await waitForSignedInOrMFA(page); try { await page.waitForURL('**/ap/signin**'); const error = await page.locator('.a-alert-content').first().innerText(); @@ -123,11 +144,6 @@ try { } catch { // navigation ok } - try { - await handleMFA(page); - } catch { - // ignore MFA watcher errors - } } else { console.log('Waiting for you to login in the browser.'); await notify('prime-gaming: no longer signed in and not enough options set for automatic login.');