Skip to content

Commit

Permalink
feat(example): auto restore session
Browse files Browse the repository at this point in the history
This is as suggested in #29. Mainly implemeted to ease work while developing, but also serves as an example.
  • Loading branch information
pedroslopez committed Feb 9, 2020
1 parent 6f44fc5 commit 5fbd8dd
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const fs = require('fs');
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.
// This object must include WABrowserId, WASecretBundle, WAToken1 and WAToken2.

Expand All @@ -13,6 +20,14 @@ client.on('qr', (qr) => {

client.on('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 => {
Expand Down

0 comments on commit 5fbd8dd

Please sign in to comment.