pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.48k stars 3.69k forks source link

Failed QR authentication: Scanning a QR generates another QR with an undefined part #3083

Closed SerpentSociety closed 5 months ago

SerpentSociety commented 5 months ago

Is there an existing issue for this?

Describe the bug

When a QR code is generated, the console logs a normal valid QR code string (example: QR RECEIVED 2@YZ3JcY2m7irzr9qy5bnOmAbFPRCTY1um6KnoLkZeCKCPSUC1XciOdE8aBL7+76BDip2SpwvPWvrjvg==,cbuKc8eWUeBL1FFEsEx1nN0txR/HayeOOotSE10dMEA=,Rm5+jRVvLnapiWQ4euReLHnI/eBU+T+jek5vKbwuFW0=,FPtkOitGptEPPnKUHF2XRw3K3GSXFKDfHFrajlQJ13I=,1) But, after scanning the QR code corresponding to this string, a new QR is generated, in between scanning/ authentication process by whats app, and the QR string has an undefined part (example QR RECEIVED undefined,cbuKc8eWUeBL1FFEsEx1nN0txR/HayeOOotSE10dMEA=,Rm5+jRVvLnapiWQ4euReLHnI/eBU+T+jek5vKbwuFW0=,FPtkOitGptEPPnKUHF2XRw3K3GSXFKDfHFrajlQJ13I=,1), thus the authentication fails/ timeouts Any further attempt to scan using WhatsApp returns a "couldn't link device" error by WhatsApp

Note: The same piece of code used to work for several months, any possibly similar issues was solved by removing the wwebjs_auth directory and reinstalling the node_modules

Expected behavior

After the QR code is generated and scanned, the device must be connected and the authentication flow shouldn't be interrupted by the generation of an undefined QR string

Steps to Reproduce the Bug or Issue

  1. Create a multiple-client sessions supported script that maps the data of a user, with the client ID corresponding to a unique user ID from a different system
  2. run the script and scan the generated QR

Relevant Code


const https = require('https');
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const qrcode = require('qrcode');
const socketIO = require('socket.io');
const mysql = require('mysql2/promise');
const fs = require('fs');
const express = require('express');
const { body, validationResult } = require('express-validator');
const router = express.Router();
const axios = require('axios');

// MySQL database connection pool
const pool = mysql.createPool({
  host: 'localhost',
  user: 'xxx',
  password: 'yyy',
  database: 'zzz',
  connectionLimit: 10,
});

const server = https.createServer({
  cert: fs.readFileSync('xxx'),
  key: fs.readFileSync('yyy')
});

const io = socketIO(server, { secure: true });

// client and connection status stored in this map (required by my web application)
const clients = new Map();

//deviceId is a unique identifier in my web application
function createClient(deviceId) {
  const clientId = `client-${deviceId}`;
  const p = 'https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html';

  const client = new Client({
    authStrategy: new LocalAuth({ clientId }),
    puppeteer: {
      headless: true,
      args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--single-process',
        '--no-zygote',
        '--no-first-run',
        '--no-default-browser-check',
        '--disable-extensions',
        '--disable-default-apps',
        '--disable-sync',
        '--disable-translate',
        '--disable-web-security',
        '--disable-features=site-per-process',
        '--disable-infobars',
        '--window-position=0,0',
        '--ignore-certificate-errors',
        '--ignore-certificate-errors-spki-list',
        '--disable-gpu',
        '--disable-webgl',
        '--disable-threaded-animation',
        '--disable-threaded-scrolling',
        '--disable-in-process-stack-traces',
        '--disable-histogram-customizer',
        '--disable-gl-extensions',
        '--disable-composited-antialiasing',
        '--disable-canvas-aa',
        '--disable-3d-apis',
        '--disable-accelerated-2d-canvas',
        '--disable-accelerated-jpeg-decoding',
        '--disable-accelerated-mjpeg-decode',
        '--disable-app-list-dismiss-on-blur',
        '--disable-accelerated-video-decode'
      ]
    },
    type: 'remote',
    remotePath: p,
    webVersionCache: {
      type: 'remote',
      remotePath: p
    }
  });

  let currentQR = null;
  let isConnected = false;
  let connectedNumber = null;

  client.on('qr', (qr) => {
    console.log('QR RECEIVED', qr);
    qrcode.toDataURL(qr, (err, url) => {
      if (err) {
        console.error('Failed to generate QR code', err);
        return;
      }
      currentQR = url;
      io.emit('qrgenerated', { url: currentQR, deviceId });
    });
  });

  client.on('authenticated', () => {
    console.log('Client is authenticated!');
    isConnected = true;

    if (client.info) {
      const info = client.info;
      connectedNumber = info.wid.user;
      io.emit('authenticated', {
        nomor: connectedNumber,
        nama: info.pushname,
        img: info.imgUrl,
        deviceId
      });
    } else {
      console.log('Client info not available yet');
    }

    // Update the client data in the clients map
    clients.set(deviceId, {
      client,
      currentQR,
      isConnected,
      connectedNumber
    });
  });

  client.on('ready', () => {
    console.log('Client is ready!');

    if (client.info) {
      const info = client.info;
      connectedNumber = info.wid.user;
      io.emit('connected', {
        nomor: connectedNumber,
        nama: info.pushname,
        img: info.imgUrl,
        deviceId
      });
    } else {
      console.log('Client info not available yet');
    }

    // Update the client data in the clients map
    clients.set(deviceId, {
      client,
      currentQR,
      isConnected,
      connectedNumber
    });
  });

  client.on('disconnected', (reason) => {
    console.log('Client was logged out', reason);
    isConnected = false;
    connectedNumber = null;
    io.emit('disconnected', deviceId);
    currentQR = null;

    // Remove the client from the clients map
    clients.delete(deviceId);
  });

  client.on('message', (message) => {
    handleMessage(message, deviceId, connectedNumber);
  });

  client.initialize();

  return {
    client,
    currentQR,
    isConnected,
    connectedNumber
  };
}

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Additional context

No response

prashant1879 commented 5 months ago

Hello @SerpentSociety

Please checkout my comment on #3021

gunika1321 commented 5 months ago

Hi, I am using an AWS server located in Oregon. Can this be a reason for the whatsapp qr code issue?

prashant1879 commented 5 months ago

Hello @gunika1321 I choose that VPS which is near my location (probably same region) so Its do not send an error I have another vps which location Germany, I'm from india same server configuration and same code but does not works their.

theshubhampanchal commented 5 months ago

I have also tried in Indian server and I'm also in India, but not working. I am using Debian 12 OS

gunika1321 commented 5 months ago

hello @prashant1879, I tried shifting my server to India but it is still not working