From 18cdaf8b295cb6ab1f197ed070c0e1204382b4e8 Mon Sep 17 00:00:00 2001 From: Adi Aharoni Date: Mon, 2 Mar 2026 22:03:31 +0200 Subject: [PATCH] fix: prevent stale change:ref from false QR disconnect the change:ref listener on AuthStore.Conn was never removed after a successful QR scan. When WhatsApp Web clears the ref after authentication begins, the listener would fire onQRChangedEvent with an invalid ref, causing a spurious 'Max qrcode retries reached' disconnect. three fixes: 1. Reset qrRetries when loading_screen fires 2. Guard against null/undefined ref in the change:ref callback 3. Remove the change:ref listener once change:hasSynced fires --- src/Client.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Client.js b/src/Client.js index c4df1bfaaa..47c945f5ee 100644 --- a/src/Client.js +++ b/src/Client.js @@ -220,6 +220,11 @@ class Client extends EventEmitter { ); } else { let qrRetries = 0; + + this.on(Events.LOADING_SCREEN, () => { + qrRetries = 0; + }); + await exposeFunctionIfAbsent( this.pupPage, 'onQRChangedEvent', @@ -270,10 +275,18 @@ class Client extends EventEmitter { ',' + platform; - window.onQRChangedEvent(getQR(window.AuthStore.Conn.ref)); // initial qr - window.AuthStore.Conn.on('change:ref', (_, ref) => { + const onRefChange = (_, ref) => { + if (ref == null) return; window.onQRChangedEvent(getQR(ref)); - }); // future QR changes + }; + + window.onQRChangedEvent(getQR(window.AuthStore.Conn.ref)); // initial qr + window.AuthStore.Conn.on('change:ref', onRefChange); // future QR changes + + // Remove QR listener once authentication succeeds + window.AuthStore.AppState.on('change:hasSynced', () => { + window.AuthStore.Conn.off('change:ref', onRefChange); + }); }); } }