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
14.24k stars 3.42k forks source link

client.logout() error #2110

Open ibrahim0132 opened 1 year ago

ibrahim0132 commented 1 year ago

Is there an existing issue for this?

Describe the bug

When i am using the client.logout() then showing the below error

node:fs:1780 handleErrorFromBinding(ctx); ^

Error: EPERM: operation not permitted, unlink '\?\C:\Users\ibrah\OneDrive\Desktop\Whatsapp_bot_example\server.wwebjs_auth\session\Default\Cache\Cache_Data\data_0' at unlinkSync (node:fs:1780:3) at _unlinkSync (node:internal/fs/rimraf:214:14) at fixWinEPERMSync (node:internal/fs/rimraf:306:5) at rimrafSync (node:internal/fs/rimraf:200:14) at node:internal/fs/rimraf:253:9 at Array.forEach () at _rmdirSync (node:internal/fs/rimraf:250:7) at fixWinEPERMSync (node:internal/fs/rimraf:304:5) at rimrafSync (node:internal/fs/rimraf:200:14) at node:internal/fs/rimraf:253:9 at Array.forEach () at _rmdirSync (node:internal/fs/rimraf:250:7) at fixWinEPERMSync (node:internal/fs/rimraf:304:5) at rimrafSync (node:internal/fs/rimraf:200:14) at node:internal/fs/rimraf:253:9 at Array.forEach () { errno: -4048, syscall: 'unlink', code: 'EPERM', path: '\\?\C:\Users\ibrah\OneDrive\Desktop\Whatsapp_bot_example\server\.wwebjs_auth\session\Default\Cache\Cache_Data\data_0' }

Expected behavior

na

Steps to Reproduce the Bug or Issue

na

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

Node.js v18.12.0

Additional context

na

LTY526 commented 1 year ago

Try have your project & session folder on another folder? Not sure if Onedrive folder has some sort of special permission restrictions

developer-choice124 commented 1 year ago

image I am also having the same issue,

location to session: D:\Ajay\Work\Electron JS\sessions location to project: D:\Ajay\Work\Electron JS\evr\whatsapp_automation_frontend

I am making compressed build of whatsapp-web.js using @vercel/ncc. I am using whatsapp-web.js as child process using nodeJs fork in the main process of my electron-react project. Everything works fine except when I try to logout the user which was logged in by using the QR(LocalAuth strategy). logout ends with the error attached in the image.

OS & Environment OS: Windows 10 Node: v18.12.1 Yarn: 1.22.19

ibrahim0132 commented 1 year ago

I have found the solution

1) Go to the file: "node_modules/whatsapp-web.js/src/authStrategies/LocalAuth.js" 2) Find the function end of the file: async logout() { if (this.userDataDir) { return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } }

3) Now Edit the function like this:

async logout() { if (this.userDataDir) { try{ return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } catch{} } }

4) Now the problem solved! 5) Done!

developer-choice124 commented 1 year ago

I have found the solution

  1. Go to the file: "node_modules/whatsapp-web.js/src/authStrategies/LocalAuth.js"
  2. Find the function end of the file: async logout() { if (this.userDataDir) { return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } }
  3. Now Edit the function like this:

async logout() { if (this.userDataDir) { try{ return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } catch{} } }

  1. Now the problem solved!
  2. Done!

This solution just avoid throwing error but issue still persists. Are we cleaning the session data good enough so that next try can generate QR properly? I am curious if it can do even this much

qazgal1 commented 11 months ago

i am having the same problem please fix the logout function

shirser121 commented 11 months ago

Please provide your code and WhatsApp Web version

juntusbr commented 2 weeks ago

Recently I had the same problem. My fix was:

1 go to: node_modules/whatsapp-web.js/src/authStrategies/LocalAuth.js 2 change the logout async :D

async logout() {
    try {
        if (!this.userDataDir) {
            throw new Error('No user data directory specified.');
        }

        const dirExists = await fs.promises.access(this.userDataDir)
            .then(() => true)
            .catch(() => false);

        if (dirExists) {
            const dirContents = await fs.promises.readdir(this.userDataDir);

            if (dirContents.length > 0) {
                console.log('Directory ${this.userDataDir} is not empty. Deleting contents...');

                // Delete all files and subdirectories inside userDataDir
                await Promise.all(dirContents.map(file => fs.promises.rm(path.join(this.userDataDir, file), { recursive: true, force: true })));
            }

            // Now remove the userDataDir itself
            await fs.promises.rm(this.userDataDir, { recursive: true, force: true });
            console.log('Directory ${this.userDataDir} has been removed');
        } else {
            console.warn('Directory ${this.userDataDir} does not exist.');
        }
    } catch (e) {
        console.error('Failed to remove directory ${this.userDataDir}:', e);
        throw new Error(e);
    }
}