Skip to content

Commit

Permalink
fix: Fixed session re-initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Oct 20, 2022
1 parent 62c9bf1 commit b523416
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
31 changes: 17 additions & 14 deletions src/api/whatsapp.ts
Expand Up @@ -26,10 +26,11 @@ import treekill = require('tree-kill');
import { evaluateAndReturn } from './helpers';

export class Whatsapp extends BusinessLayer {
private connected: boolean | null = null;

constructor(public page: Page, session?: string, options?: CreateConfig) {
super(page, session, options);

let connected = false;
let interval: any = null;

const removeToken = async () => {
Expand All @@ -45,31 +46,24 @@ export class Whatsapp extends BusinessLayer {

if (this.page) {
this.page.on('close', async () => {
if (!connected) {
if (this.connected === false) {
await removeToken();
}
clearInterval(interval);
});

page
.evaluate(() => WPP.conn.isAuthenticated())
.then((isAuthenticated) => {
connected = isAuthenticated;
})
.catch(() => null);
}

interval = setInterval(async (state) => {
const newConnected = await page
.evaluate(() => WPP.conn.isAuthenticated())
.evaluate(() => WAPI.isRegistered())
.catch(() => null);

if (newConnected === null || newConnected === connected) {
if (newConnected === null || newConnected === this.connected) {
return;
}

if (newConnected) {
connected = newConnected;
this.connected = newConnected;
setTimeout(async () => {
this.log('verbose', 'Updating session token', { type: 'token' });
const tokenData = await this.getSessionTokenBrowser();
Expand All @@ -88,13 +82,13 @@ export class Whatsapp extends BusinessLayer {
}
}, 1000);
} else {
if (!newConnected && connected) {
if (!newConnected && this.connected) {
setTimeout(async () => {
await page.evaluate(() => localStorage.clear());
await page.reload();
}, 1000);
}
connected = newConnected;
this.connected = newConnected;

setTimeout(async () => {
await removeToken();
Expand All @@ -110,6 +104,15 @@ export class Whatsapp extends BusinessLayer {
}, 1000);
}

protected async afterPageScriptInjected() {
await super.afterPageScriptInjected();
this.page
.evaluate(() => WAPI.isRegistered())
.then((isAuthenticated) => {
this.connected = isAuthenticated;
})
.catch(() => null);
}
/**
* Decrypts message file
* @param data Message object
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/auth.ts
Expand Up @@ -67,7 +67,7 @@ export const getInterfaceStatus = async (
* @param waPage
*/
export const isAuthenticated = async (waPage: puppeteer.Page) => {
return await waPage.evaluate(() => WPP.conn.isAuthenticated());
return await waPage.evaluate(() => WAPI.isRegistered());
};

export const needsToScan = async (waPage: puppeteer.Page) => {
Expand Down
22 changes: 12 additions & 10 deletions src/controllers/browser.ts
Expand Up @@ -102,17 +102,19 @@ export async function initWhatsapp(
await setWhatsappVersion(page, version);
}

log?.('verbose', `Loading WhatsApp WEB`);

const timeout = 10 * 1000;
page
.goto(puppeteerConfig.whatsappUrl, {
timeout,
waitUntil: 'domcontentloaded',
})
.catch(() => {});
setTimeout(() => {
log?.('verbose', `Loading WhatsApp WEB`);

const timeout = 10 * 1000;
page
.goto(puppeteerConfig.whatsappUrl, {
timeout,
waitUntil: 'domcontentloaded',
})
.catch(() => {});

log?.('verbose', `WhatsApp WEB loaded`);
log?.('verbose', `WhatsApp WEB loaded`);
}, 1000);

return page;
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/initializer.ts
Expand Up @@ -242,7 +242,7 @@ export async function create(

let waitLoginPromise = null;
client.onStateChange(async (state) => {
const connected = await page.evaluate(() => WPP.conn.isAuthenticated());
const connected = await page.evaluate(() => WAPI.isRegistered());
if (!connected) {
await sleep(2000);

Expand Down
8 changes: 8 additions & 0 deletions src/lib/wapi/wapi.js
Expand Up @@ -456,6 +456,14 @@ if (typeof window.WAPI === 'undefined') {
return await WPP.conn.logout();
};

/**
* @todo Temporary fix while is not implemented on WA-JS
*/
window.WAPI.isRegistered = function () {
const m = WPP.webpack.search((m) => m.isRegistered);
return m.isRegistered();
};

readyPromise.then(addOnStreamChange);
readyPromise.then(addOnStateChange);
readyPromise.then(initNewMessagesListener);
Expand Down
1 change: 1 addition & 0 deletions src/types/WAPI.d.ts
Expand Up @@ -82,6 +82,7 @@ interface WAPI {
getWAVersion: () => string;
isConnected: () => boolean;
isLoggedIn: () => boolean;
isRegistered: () => boolean;
joinGroup: (groupId: string) => Promise<string | boolean>;
killServiceWorker: () => boolean;
leaveGroup: (groupId: string) => any;
Expand Down

0 comments on commit b523416

Please sign in to comment.