Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore session #2

Closed
jovazxc-zz opened this issue Mar 16, 2019 · 9 comments
Closed

Restore session #2

jovazxc-zz opened this issue Mar 16, 2019 · 9 comments

Comments

@jovazxc-zz
Copy link

I'm trying to restore the WhatsApp Session, I found that most of data is saved in the localStorage and a little part is on cookies

I obtained the cookies using the dev tools functions because there are HttpOnly, and it can't be obtained through normal methods

async cookies() {
    const client = await this.pupPage.target().createCDPSession();
    let r = await client.send('Network.getAllCookies');
    return r.cookies;
}

But I'm unable to apply them, I'd tried with

page.setCookie()

But isn't working

@jovazxc-zz
Copy link
Author

jovazxc-zz commented Mar 16, 2019

I've done, but restore isn't working headless, waitforselector never get resolved

Clone my repo https://github.com/jovazxc/whatsapp-web.js/ and try the following

First npm install

  • Modify example.js and set headless to true
  • node example.js
  • Scan QR code on terminal (You have to set terminal to full size first)
  • Close (Ctrl c)
  • node example.js
  • Waiting for auth never is solved
  • Close
  • Modify headless to false
  • node example.js

@pedroslopez
Copy link
Owner

pedroslopez commented Mar 17, 2019

Thanks for the input!

The main issue here with using localStorage is that there's something in WhatsApp Web's code that prevents us from going to the application more than once while in headless mode. Just doing a simple refresh on the page won't let you get in, showing you the following error:

screen

This means that we would need to apply the tokens to localStorage using some other method. I was able to do this using puppeteer's evaluateOnNewDocument function that runs before any other scripts do. Restoring sessions should now work. I've added this in a7a7034.

To restore the session just pass a "session" parameter to the options object like so:

const client = new Client({session: {
    WABrowserId: "",
    WASecretBundle: "",
    WAToken1: "",
    WAToken2: ""
}});

The authenticated event now also returns the session object via its single param:

client.on('authenticated', (session) => {
    // session param contains object structured as above
});

@JRakhimov
Copy link

I have a question here, how long can we stay in one session? My client have responded during the 2 days and then there was 'Client was logged out' @jovazxc @pedroslopez

@pedroslopez
Copy link
Owner

pedroslopez commented Mar 19, 2019

I'm not entirely sure, to be honest... However, make sure you don't log in to WhatsApp Web for that number on another app or browser as that will log it out. If connection to the phone is lost, this will also trigger a disconnect event. Ideally this case should be a bit more resilient so if it is indeed because connection was temporarily lost, then it should recognize when it's back online instead of just emitting the disconnect event and closing the client.

@gunomnifluencer
Copy link

gunomnifluencer commented Jul 8, 2020

Hello,

How to store session and use it? I had put on session.json like this :
{"WABrowserId" : 1 ,"WASecretBundle" : "test" ,"WAToken1" : "a1","WAToken1" : "a2"}

and this my scan code :

const qrcode = require('qrcode-terminal');

const { Client } = require('whatsapp-web.js');
const fs = require('fs');

const SESSION_FILE_PATH = './session.json';
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) 
{
    sessionCfg = require(SESSION_FILE_PATH);
}

const client = new Client({ session: sessionCfg });

client.on('qr', qr => {
    qrcode.generate(qr, {small: true});
});

client.on('ready', () => {
    console.log('Client is ready!');
});
client.initialize();`=

It doesn't work, is there something wrong on my code?

please help me, thanks.

@pedroslopez
Copy link
Owner

Hi @gunomnifluencer, you're not saving the session info on successful login. Specifically, you're missing the authenticated event.

For more info, please read https://waguide.pedroslopez.me/features/resuming-sessions

mpirescarvalho referenced this issue in mpirescarvalho/whatsapp-web.js Feb 2, 2021
mpirescarvalho referenced this issue in mpirescarvalho/whatsapp-web.js Feb 11, 2021
@TLGits
Copy link

TLGits commented Feb 8, 2022

how to check if the client is logged in successfully?

I follow the https://waguide.pedroslopez.me/features/resuming-sessions, but it does not trigger the 'authenticated'.

@pedroslopez

koodinikula pushed a commit to koodinikula/whatsapp-web.js that referenced this issue Oct 29, 2022
…/patch

Update WhatsApp Web Version (2.2236.10)
@hridayjit
Copy link

hridayjit commented Jun 1, 2023

how to delete the whatsapp-web.js localAuth session if the user unlinks the device from phone and start a new session? @pedroslopez

@Ashish-bmn
Copy link

Hello,

How to store session and use it? I had put on session.json like this : {"WABrowserId" : 1 ,"WASecretBundle" : "test" ,"WAToken1" : "a1","WAToken1" : "a2"}

and this my scan code :

const qrcode = require('qrcode-terminal');

const { Client } = require('whatsapp-web.js');
const fs = require('fs');

const SESSION_FILE_PATH = './session.json';
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) 
{
    sessionCfg = require(SESSION_FILE_PATH);
}

const client = new Client({ session: sessionCfg });

client.on('qr', qr => {
    qrcode.generate(qr, {small: true});
});

client.on('ready', () => {
    console.log('Client is ready!');
});
client.initialize();`=

It doesn't work, is there something wrong on my code?

please help me, thanks.

(node:1496) DeprecationWarning: options.session is deprecated and will be removed in a future release due to incompatibility with multi-device. Use the LocalAuth authStrategy, don't pass in a session as an option, or suppress this warning by using the LegacySessionAuth strategy explicitly (see https://wwebjs.dev/guide/authentication.html#legacysessionauth-strategy).
(Use node --trace-deprecation ... to show where the warning was created)

diegofemello referenced this issue in diegofemello/whatsapp-web.js Aug 6, 2023
Revert version info of auto update: "Update WhatsApp Web Version (2.2321.6)"
@devsakae devsakae mentioned this issue Nov 9, 2023
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants