-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
auth.ts
89 lines (80 loc) · 2.74 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* This file is part of WPPConnect.
*
* WPPConnect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WPPConnect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/
import * as puppeteer from 'puppeteer';
import * as qrcode from 'qrcode-terminal';
export const getInterfaceStatus = async (
waPage: puppeteer.Page
): Promise<puppeteer.HandleFor<Awaited<ReturnType<any>>>> => {
return await waPage
.waitForFunction(
() => {
const elLoginWrapper = document.querySelector(
'body > div > div > .landing-wrapper'
);
const elQRCodeCanvas = document.querySelector('canvas');
if (elLoginWrapper && elQRCodeCanvas) {
return 'UNPAIRED';
}
const streamStatus = WPP?.whatsapp?.Stream?.displayInfo;
if (['PAIRING', 'RESUMING', 'SYNCING'].includes(streamStatus)) {
return 'PAIRING';
}
const elChat = document.querySelector('.app,.two') as HTMLDivElement;
if (elChat && elChat.attributes && elChat.tabIndex) {
return 'CONNECTED';
}
return false;
},
{
timeout: 0,
polling: 100,
}
)
.then(async (element: puppeteer.HandleFor<Awaited<ReturnType<any>>>) => {
return (await element.evaluate((a: any) => a)) as puppeteer.HandleFor<
ReturnType<any>
>;
})
.catch(() => null);
};
/**
* Validates if client is authenticated
* @returns true if is authenticated, false otherwise
* @param waPage
*/
export const isAuthenticated = (waPage: puppeteer.Page) => {
return waPage.evaluate(() => WPP.conn.isRegistered());
};
export const needsToScan = async (waPage: puppeteer.Page) => {
const connected = await isAuthenticated(waPage);
return !connected;
};
export const isInsideChat = async (waPage: puppeteer.Page) => {
return await waPage.evaluate(() => WPP.conn.isMainReady());
};
export const isConnectingToPhone = async (waPage: puppeteer.Page) => {
return await waPage.evaluate(
() => WPP.conn.isMainLoaded() && !WPP.conn.isMainReady()
);
};
export async function asciiQr(code: string): Promise<string> {
return new Promise((resolve) => {
qrcode.generate(code, { small: true }, (qrcode) => {
resolve(qrcode);
});
});
}