fix: support phone pairing on QR login page
Some checks failed
CI/CD Pipeline for Pull Requests to Main / test (22.x) (pull_request) Has been cancelled

This commit is contained in:
Eddy de Vink 2026-08-24 15:52:32 +02:00
commit dae347d4b7

View file

@ -363,10 +363,9 @@ const requestPairingCode = async (req, res) => {
},
}
*/
const sessionId = req.params.sessionId
try {
const { phoneNumber, showNotification = true } = req.body
const client = sessions.get(sessionId)
const client = sessions.get(req.params.sessionId)
if (!client) {
return res.json({ success: false, message: 'session_not_found' })
}
@ -375,11 +374,37 @@ const requestPairingCode = async (req, res) => {
client.emit('code', 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 })
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to request pairing code')
res.status(500).json({ success: false, error: error.message })
logger.error({ sessionId: req.params.sessionId, err: error }, 'Failed to request pairing code')
sendErrorResponse(res, 500, error.message)
}
}