From 64b4ea20bdadbdc2bbc7c08a88d1fac0dbb94f83 Mon Sep 17 00:00:00 2001 From: Nur Muhammad Date: Mon, 21 Mar 2022 17:24:57 +0800 Subject: [PATCH] Fix for new auth strategy & multi-device beta support --- .gitignore | 3 +- ...tiple-device.js => app-multiple-account.js | 28 +++++-------------- app.js | 24 +++------------- ...device.html => index-multiple-account.html | 0 4 files changed, 13 insertions(+), 42 deletions(-) rename app-multiple-device.js => app-multiple-account.js (89%) rename index-multiple-device.html => index-multiple-account.html (100%) diff --git a/.gitignore b/.gitignore index d8155c4..152f866 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules whatsapp-session*.json package-lock.json -downloaded-media/ \ No newline at end of file +downloaded-media/ +.wwebjs_auth \ No newline at end of file diff --git a/app-multiple-device.js b/app-multiple-account.js similarity index 89% rename from app-multiple-device.js rename to app-multiple-account.js index 71141ba..147193e 100644 --- a/app-multiple-device.js +++ b/app-multiple-account.js @@ -1,4 +1,4 @@ -const { Client, MessageMedia } = require('whatsapp-web.js'); +const { Client, MessageMedia, LocalAuth } = require('whatsapp-web.js'); const express = require('express'); const socketIO = require('socket.io'); const qrcode = require('qrcode'); @@ -18,7 +18,7 @@ app.use(express.urlencoded({ })); app.get('/', (req, res) => { - res.sendFile('index-multiple-device.html', { + res.sendFile('index-multiple-account.html', { root: __dirname }); }); @@ -53,12 +53,6 @@ const getSessionsFile = function() { const createSession = function(id, description) { console.log('Creating session: ' + id); - const SESSION_FILE_PATH = `./whatsapp-session-${id}.json`; - let sessionCfg; - if (fs.existsSync(SESSION_FILE_PATH)) { - sessionCfg = require(SESSION_FILE_PATH); - } - const client = new Client({ restartOnAuthFail: true, puppeteer: { @@ -74,7 +68,9 @@ const createSession = function(id, description) { '--disable-gpu' ], }, - session: sessionCfg + authStrategy: new LocalAuth({ + clientId: id + }) }); client.initialize(); @@ -97,27 +93,17 @@ const createSession = function(id, description) { setSessionsFile(savedSessions); }); - client.on('authenticated', (session) => { + client.on('authenticated', () => { io.emit('authenticated', { id: id }); io.emit('message', { id: id, text: 'Whatsapp is authenticated!' }); - sessionCfg = session; - fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) { - if (err) { - console.error(err); - } - }); }); - client.on('auth_failure', function(session) { + client.on('auth_failure', function() { io.emit('message', { id: id, text: 'Auth failure, restarting...' }); }); client.on('disconnected', (reason) => { io.emit('message', { id: id, text: 'Whatsapp is disconnected!' }); - fs.unlinkSync(SESSION_FILE_PATH, function(err) { - if(err) return console.log(err); - console.log('Session file deleted!'); - }); client.destroy(); client.initialize(); diff --git a/app.js b/app.js index 181452a..c2d291b 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,4 @@ -const { Client, MessageMedia } = require('whatsapp-web.js'); +const { Client, MessageMedia, LocalAuth } = require('whatsapp-web.js'); const express = require('express'); const { body, validationResult } = require('express-validator'); const socketIO = require('socket.io'); @@ -24,12 +24,6 @@ app.use(fileUpload({ debug: true })); -const SESSION_FILE_PATH = './whatsapp-session.json'; -let sessionCfg; -if (fs.existsSync(SESSION_FILE_PATH)) { - sessionCfg = require(SESSION_FILE_PATH); -} - app.get('/', (req, res) => { res.sendFile('index.html', { root: __dirname @@ -51,7 +45,7 @@ const client = new Client({ '--disable-gpu' ], }, - session: sessionCfg + authStrategy: new LocalAuth() }); client.on('message', msg => { @@ -133,16 +127,10 @@ io.on('connection', function(socket) { socket.emit('message', 'Whatsapp is ready!'); }); - client.on('authenticated', (session) => { + client.on('authenticated', () => { socket.emit('authenticated', 'Whatsapp is authenticated!'); socket.emit('message', 'Whatsapp is authenticated!'); - console.log('AUTHENTICATED', session); - sessionCfg = session; - fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) { - if (err) { - console.error(err); - } - }); + console.log('AUTHENTICATED'); }); client.on('auth_failure', function(session) { @@ -151,10 +139,6 @@ io.on('connection', function(socket) { client.on('disconnected', (reason) => { socket.emit('message', 'Whatsapp is disconnected!'); - fs.unlinkSync(SESSION_FILE_PATH, function(err) { - if(err) return console.log(err); - console.log('Session file deleted!'); - }); client.destroy(); client.initialize(); }); diff --git a/index-multiple-device.html b/index-multiple-account.html similarity index 100% rename from index-multiple-device.html rename to index-multiple-account.html