mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 12:39:20 +00:00
feat(example): auto restore session
This is as suggested in #29. Mainly implemeted to ease work while developing, but also serves as an example.
This commit is contained in:
17
example.js
17
example.js
@@ -1,6 +1,13 @@
|
|||||||
|
const fs = require('fs');
|
||||||
const { Client } = require('./index');
|
const { Client } = require('./index');
|
||||||
|
|
||||||
const client = new Client({puppeteer: {headless: false}});
|
const SESSION_FILE_PATH = './session.json';
|
||||||
|
let sessionCfg;
|
||||||
|
if (fs.existsSync(SESSION_FILE_PATH)) {
|
||||||
|
sessionCfg = require(SESSION_FILE_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = new Client({puppeteer: {headless: false}, session: sessionCfg});
|
||||||
// You can use an existing session and avoid scanning a QR code by adding a "session" object to the client options.
|
// You can use an existing session and avoid scanning a QR code by adding a "session" object to the client options.
|
||||||
// This object must include WABrowserId, WASecretBundle, WAToken1 and WAToken2.
|
// This object must include WABrowserId, WASecretBundle, WAToken1 and WAToken2.
|
||||||
|
|
||||||
@@ -13,6 +20,14 @@ client.on('qr', (qr) => {
|
|||||||
|
|
||||||
client.on('authenticated', (session) => {
|
client.on('authenticated', (session) => {
|
||||||
console.log('AUTHENTICATED', session);
|
console.log('AUTHENTICATED', session);
|
||||||
|
|
||||||
|
if (!fs.existsSync(SESSION_FILE_PATH)) {
|
||||||
|
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('auth_failure', msg => {
|
client.on('auth_failure', msg => {
|
||||||
|
|||||||
Reference in New Issue
Block a user