whatsapp-web.js icon indicating copy to clipboard operation
whatsapp-web.js copied to clipboard

Unable to reconnect Client after QR Code scanned and client destroyed

Open Minenat69 opened this issue 2 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

I am doing an application than can send PDF on WhatsApp using this library. The sending is done in two steps :

  1. The client logs in preference panel. A new Client using LocalAuth is created, a QR code is displayed, the user scans the QR code and once the client is ready, he is destroyed.
  2. When the user wants to send PDF (click on 'Send' in another panel than the preference one), on backend a new Client using LocalAuth is instantiated but QR code should not appear since id for client is saved and session directory is detected. Once the client is ready, a MessageMedia is created and sent to the desired number. After 5sec (client.sendMessage promise is not resolved once the message is recieved), the client is destroyed.

These steps worked perfectly before the week-end. Today, the step 2 does not work anymore, and client.initialize loops indefenitely, not events is emitted. I looked on the code and it seems to await indefenitely on the needAuthentification Promise race (Client.js line 185). Since INTRO_QR_SELECTOR is not resolved because the client already scanned it in step 1, the promise seems to wait on INTRO_IMG_SELECTOR.

WhatsApp mobile application had an update 3 days ago, maybe something changed on their website too and the selectors you are waiting for does not exists anymore.

Expected behavior

As a user of the application, the expected behavior is on step 2 the new Client initialises well since the QR code for corresponding id is scanned on step 1. Once the client is initialized and ready, he should send a message with a PDF attachment, but no events is emitted and client runs indefenitely on initialize

Steps to Reproduce the Bug or Issue

  1. Create a new Client, scan QR code, save id somewhere and destroy client (after few seconds just in case)
  2. Recreate new Client with same id and session path

Relevant Code

I am putting here your code since the bug seems to be located here (Client.js line 185)

const INTRO_IMG_SELECTOR = '[data-testid="intro-md-beta-logo-dark"], [data-testid="intro-md-beta-logo-light"], [data-asset-intro-image-light="true"], [data-asset-intro-image-dark="true"]';
const INTRO_QRCODE_SELECTOR = 'div[data-ref] canvas';        

const needAuthentication = await Promise.race([
/* This promise is never resolved */
          new Promise(resolve => {
              page.waitForSelector(INTRO_IMG_SELECTOR, { timeout: this.options.authTimeoutMs })
                  .then(() => resolve(false))
                  .catch((err) => resolve(err));
          }),

/* This promise is not resolved since client already scanned QR code */
          new Promise(resolve => {
              page.waitForSelector(INTRO_QRCODE_SELECTOR, { timeout: this.options.authTimeoutMs })
                  .then(() => resolve(true))
                  .catch((err) => resolve(err));
          })
      ]);

Browser Type

Bundled Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS : The app runs on Electron, so it can run Windows, Mac and such. I'm developping the app on Windows Phone : I am testing the client QR scan and message sending on my Iphone 13 (IOS 16.1) whatsapp-web.js version : Last one since i have "^1.18.4" on my package.json ('^' gets the last version and i have yarn install again this morning) Node.js version : 16.13.2

Additional context

After some inspections (running on non headless with my chrome executable and devtools), there seem to be an issue when loading whatsapp web the first time on pupeteer. When I scan the QR code on step 1 and whatsapp web opens in pupeteer, i get this error in dev tools : image Something seems to be up with Chrome (and Chromiun) cache

Minenat69 avatar Jan 23 '23 14:01 Minenat69

@themazim I don't have this issue anymore, pretty much corrected by itself and tbh I don't really know how but here is what i did : 1 - Delete all your active sessions 2 - Go on Client.js and comment all lines in initialize function from await page.goto(WhatsWebURL (line 112) included to the end of the function 3 - You need to have pupeteer on head mode (i personnaly linked my personnal chrome) and enable devtools :

 const client = new Client({
          authStrategy: new LocalAuth({clientId: wa_id, dataPath: wdxWADir}),
          puppeteer: {
              headless: false,
              devtools: true,
              dumpio: true,
              executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
          }
      });

4 - Because of the comments, pupeteer will only open a blank page on your separate browser, and manually go on web.whatsapp.com and scan QR code 5 - Once logged, check if you have the Cache.put errors on your devtools. They were gone for me 6 - After that, uncomment lines and check if you still have the error with all initialize function

My theory is that somehow Cache needed to be "cleared" and something made him bugged, but I don't like when I fix an issue without really knowing what caused it, and hope that this solution is not temporary. Devs still need to lookup on that, cause this behaviour is pretty weird.

Minenat69 avatar Jan 25 '23 15:01 Minenat69

I restarted and it also solved by itself somehow. We use the mongodb connector to handle sessions.

themazim avatar Jan 25 '23 16:01 themazim

In my case nothing is being stored to dataPath

Agusteando avatar Mar 04 '23 14:03 Agusteando

My code is breaking at the same exact step of needAuthentication. I have made a macBuild . Whenever I close the build and try to restart it it is not able to login using the LocalAuth . When tried in non-headless mode I can see the chromium browser trying to load but neither the QrCode is coming, nor the whatsapp is getting logged in , it just keeps loading the scanner.

I have kept my library and auth files outside the build and am trying to access it from there.

AdityaHarivansh avatar Aug 30 '23 07:08 AdityaHarivansh

My code is breaking at the same exact step of needAuthentication. I have made a macBuild . Whenever I close the build and try to restart it it is not able to login using the LocalAuth . When tried in non-headless mode I can see the chromium browser trying to load but neither the QrCode is coming, nor the whatsapp is getting logged in , it just keeps loading the scanner.

I have kept my library and auth files outside the build and am trying to access it from there.

Have you tried updating to the latest main version? Especially this fix: https://github.com/pedroslopez/whatsapp-web.js/commit/abac063b779570729476cf42e29dc694e5345ca6

themazim avatar Aug 30 '23 07:08 themazim

Yes I am on the latest version I am not using the library I am using there code repo I have tried everything but once the build is closed and then opened again . It can't connect with whatsapp using the session previously stored. I have to manually delete the wwebjs_Auth folder and rescan.

AdityaHarivansh avatar Aug 30 '23 08:08 AdityaHarivansh