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.05k stars 3.58k forks source link

RemoteAuth failed to load existing session #2530

Open rithienatan opened 11 months ago

rithienatan commented 11 months ago

Is there an existing issue for this?

Describe the bug

After authenticated and throwing the "remote_session_saved" event. However, when we try to recover the session, it fails and whatsapp immediately redirect to the QR code page.

Expected behavior

RemoteAuth should restore the existing session correctly.

Steps to Reproduce the Bug or Issue

  1. Start wwebjs-mongo or any other custom plugin for manager session.
  2. Starts application normally.
  3. Wait for the "remote_session_saved" event and shout down the app.
  4. Restart application and try to restore session.

Relevant Code

RemoteAuth.js file:

async unCompressSession(compressedSessionPath) {
    var stream = fs.createReadStream(compressedSessionPath);
    await new Promise((resolve, reject) => {
        stream.pipe(unzipper.Extract({
            path: this.userDataDir
        }))
            .on('error', err => reject(err))
            .on('finish', () => resolve());
    });
    await fs.promises.unlink(compressedSessionPath);
}

Browser Type

Google Chrome

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Additional context

I've seen this implementation - https://github.com/nkbhasker/nest-whatsapp citing by @abhikhedekar4241 on #2452 and I try too look what is different to this reffer application continue to work and I discover that fs.createReadStream does not uncompressing zip file properly.

So, what i discover is replacing fs.createReadStream to AdmZip library, RemoteAuth start to working well again. - Probably this could be used to fix this issue.

What I haven't discover is how solve this problem only using fs.createReadStream to uncompress zip file properly.

Heres the code I encountered in https://github.com/nkbhasker/nest-whatsapp to temporarely fix this issue:

async unCompressSession(compressedSessionPath: string) {
    await new Promise<void>((resolve, reject) => {
        const zip = new AdmZip(compressedSessionPath);
        zip.extractAllToAsync(this.userDataDir, true, false, (err) => {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
    await promises.unlink(compressedSessionPath);
}
SherefAbolmagd commented 11 months ago

Facing the same issue when retrieving the session data

wilsinho8 commented 11 months ago

Problem solved putting the code

async unCompressSession(compressedSessionPath) { await new Promise((resolve, reject) => { const zip = new AdmZip(compressedSessionPath); zip.extractAllToAsync(this.userDataDir, true, false, (err) => { if (err) { reject(err); } else { resolve(); } }); }); await promises.unlink(compressedSessionPath); }

SherefAbolmagd commented 11 months ago

I was able to resolved it by 1- installing the npm i adm-zip 2 - adding this in RemoteAuth.js var AdmZip = require('adm-zip'); 3- then replace the unCompressSession function with this

 async unCompressSession(compressedSessionPath) {
        await new Promise((resolve, reject) => {
            const zip = new AdmZip(compressedSessionPath);
            zip.extractAllToAsync(this.userDataDir, true, false, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
        await fs.promises.unlink(compressedSessionPath);
    }
rithienatan commented 11 months ago

Hello guys, here is an update on this bug.

Once I fix this issue, I will close this report.

rithienatan commented 11 months ago

Hello everyone, this is the last update that I bring here:

Yesterday, I made a PR for this issue, but it already has an existing PR (#2412) related to this. So, I will wait for this PR to be approved and then close this issue report.

SergioZhidkov commented 7 months ago

Awesome!

bymarcelo commented 1 month ago

I fixed it in an official/recommended way on this commit: https://github.com/pedroslopez/whatsapp-web.js/pull/3200 Encourage maintainers to approve the PR.

While the PR is not approved, you can use my fork like this:

npm install github:bymarcelo/whatsapp-web.js#fix-remote-auth-unzip

NOTE: Recommended to use node version >= 18, tested v20