Merge pull request 'fix: support phone pairing on QR login page' (#1) from fix/pairing-code-phone-login into main

This commit is contained in:
eddy 2026-08-24 15:54:55 +02:00
commit e8b981d2d5

View file

@ -363,10 +363,9 @@ const requestPairingCode = async (req, res) => {
}, },
} }
*/ */
const sessionId = req.params.sessionId
try { try {
const { phoneNumber, showNotification = true } = req.body const { phoneNumber, showNotification = true } = req.body
const client = sessions.get(sessionId) const client = sessions.get(req.params.sessionId)
if (!client) { if (!client) {
return res.json({ success: false, message: 'session_not_found' }) return res.json({ success: false, message: 'session_not_found' })
} }
@ -375,11 +374,37 @@ const requestPairingCode = async (req, res) => {
client.emit('code', code) client.emit('code', code)
return code return code
}) })
const result = await client.requestPairingCode(phoneNumber, showNotification)
// WhatsApp Web may initially expose only the QR flow. Switch the page to
// phone-number login before invoking the internal pairing API.
await client.pupPage.evaluate(() => {
const labels = [
'Log in with phone number',
'Inloggen met telefoonnummer',
]
const element = [...document.querySelectorAll('div,button,a,span')].find((node) =>
labels.includes(node.textContent.trim()),
)
if (element) element.click()
})
await new Promise((resolve) => setTimeout(resolve, 500))
// Drive the WhatsApp Web module directly in this Puppeteer page. This is
// intentionally kept here until the public wrapper handles this flow reliably.
const result = await client.pupPage.evaluate(
async (phoneNumber, showNotification) => {
const api = window.require('WAWebAltDeviceLinkingApi')
api.setPairingType('ALT_DEVICE_LINKING')
await api.initializeAltDeviceLinking()
return api.startAltLinkingFlow(phoneNumber, showNotification)
},
phoneNumber,
showNotification,
)
res.json({ success: true, result }) res.json({ success: true, result })
} catch (error) { } catch (error) {
logger.error({ sessionId, err: error }, 'Failed to request pairing code') logger.error({ sessionId: req.params.sessionId, err: error }, 'Failed to request pairing code')
res.status(500).json({ success: false, error: error.message }) sendErrorResponse(res, 500, error.message)
} }
} }