fix: support phone pairing on QR login page #1
1 changed files with 30 additions and 5 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue