From 5fbd8ddf687b10c2c69b1f426c24b98ee5743299 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 9 Feb 2020 01:52:38 -0400 Subject: [PATCH] feat(example): auto restore session This is as suggested in #29. Mainly implemeted to ease work while developing, but also serves as an example. --- example.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/example.js b/example.js index 29e5fcc..19742db 100644 --- a/example.js +++ b/example.js @@ -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. @@ -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 => {