Verbessere Login-Handling: Füge Unterstützung für das automatische Hinzufügen des Intent-Feldes im Login-Formular hinzu und verbessere die Cookie-Verwaltung bei der Anmeldung.
All checks were successful
Code Analysis and Production Deploy / analyze (push) Successful in 4m34s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Successful in 2m40s

This commit is contained in:
Torsten Schulz (local)
2026-09-04 16:25:08 +02:00
parent 9a4eb77c56
commit 3fb60227df

View File

@@ -108,16 +108,42 @@ async function loginWithBrowser(email, password) {
await page.waitForTimeout(2_500)
}
const submit = page.locator('button[type="submit"][name="intent"][value="login"], button[type="submit"], input[type="submit"]').first()
await page.evaluate(() => {
const form = document.querySelector('form[action*="/login"]')
if (!form) return
let intent = form.querySelector('input[name="intent"]')
if (!intent) {
intent = document.createElement('input')
intent.setAttribute('type', 'hidden')
intent.setAttribute('name', 'intent')
form.appendChild(intent)
}
intent.setAttribute('value', 'login')
})
const submit = page.locator('button[type="submit"][name="intent"][value="login"]').first()
const genericSubmit = page.locator('button[type="submit"], input[type="submit"]').first()
if (await submit.count()) await submit.click({ noWaitAfter: true })
else if (await genericSubmit.count()) await genericSubmit.click({ noWaitAfter: true })
else await page.locator('form').evaluate(form => form.requestSubmit())
let cookie = null
let cookieNames = []
let loginPageText = ''
for (let attempt = 0; attempt < 40; attempt += 1) {
cookie = (await page.context().cookies(BASE_URL)).find(item => /^sb-\d+-auth-token$/.test(item.name))
const cookies = await page.context().cookies()
cookieNames = cookies.map(item => item.name)
cookie = cookies.find(item => item.name === 'sb-10-auth-token' || /^sb-\d+-auth-token$/.test(item.name) || item.name.includes('auth-token'))
if (cookie) break
if (attempt % 4 === 0) {
loginPageText = await page.locator('body').innerText().catch(() => '')
}
await page.waitForTimeout(500)
}
if (!cookie) throw new Error('CAPTCHA konnte nicht automatisch gelöst werden.')
if (!cookie) {
console.warn('[mytischtennis] Login after solved CAPTCHA did not create a session', {
url: page.url(), cookieNames, pageText: loginPageText.slice(0, 1_000)
})
throw new Error('myTischtennis-Login nach gelöstem CAPTCHA fehlgeschlagen.')
}
return `${cookie.name}=${cookie.value}`
} finally { await browser.close() }
}