Skip to content

startWebLoginWithCode calls requestPairingCode before socket reaches connecting state #56797

Description

@stangebala

Bug

startWebLoginWithCode() in extensions/whatsapp/src/login-qr.ts calls sock.requestPairingCode(digits) immediately after createWaSocket() returns, without waiting for the socket to reach the "connecting" state. This causes requestPairingCode to fail with "Connection Closed" every time.

Expected behavior

Per the Baileys docs, requestPairingCode must only be called after the socket emits a connection.update with connection === "connecting" or a QR event:

sock.ev.on('connection.update', async (update) => {
  const {connection, lastDisconnect, qr} = update
  if (connection == "connecting" || !!qr) {
    const code = await sock.requestPairingCode(phoneNumber)
  }
})

Current code (broken)

// login-qr.ts — startWebLoginWithCode
sock = await createWaSocket(false, Boolean(opts.verbose), { authDir: account.authDir });
// ... immediately:
const code = await sock.requestPairingCode(digits); // fails — socket not ready

Suggested fix

Wait for the QR/connecting event before calling requestPairingCode:

sock = await createWaSocket(false, Boolean(opts.verbose), {
  authDir: account.authDir,
  onQr: () => { /* socket is now in connecting state */ },
});
// Wait for onQr to fire
const code = await sock.requestPairingCode(digits); // now works

Compare with startWebLoginWithQr which correctly waits for the QR event before proceeding.

Environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions