whatsapp-web.js
whatsapp-web.js copied to clipboard
session object always undefined in client.on('authenticated')
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
hi. my session
object returned from client.on('authenticated', (session) => { ..... });
is always undefined
no matter what, my nodejs
project is currently running from localhost
here is my index.js
:
// Import essential libraries
const express = require('express');
const app = express();
const path = require('path');
const router = express.Router();
app.get('/test', (req, res) => {
const { Client, LocalAuth } = require('whatsapp-web.js');
const client = new Client({
authStrategy: new LocalAuth({ clientId: "1234" })
});
client.on('authenticated', (session) => {
console.log('Client > On > Authenticated');
console.log('Client > On > Authenticated > Session Object: ' + JSON.stringify(session, null, 2));
// Save the session object however you prefer.
// Convert it to json, save it to a file, store it in a database...
});
client.on('qr', (qr) => {
console.log('Client > On > QR Code');
var qr_img = require('qr-image');
// Generate and scan this code with your phone
try {
var img = qr_img.image(qr);
res.writeHead(200, {'Content-Type': 'image/png'});
img.pipe(res);
} catch (e) {
res.writeHead(414, {'Content-Type': 'text/html'});
res.end('<h1>414 Request-URI Too Large</h1>');
}
console.log('Client > On > QR Code > Received');
});
client.on('ready', () => {
console.log('Client > On > Ready');
});
client.on('message', msg => {
console.log('Client > On > Message');
});
client.initialize();
})
//add the router
app.use('/', router);
app.listen(process.env.port || 3001);
console.log('Running at Port 3001');
Expected behavior
session
object not undefined
Steps to Reproduce the Bug or Issue
- copy my code
- paste into a new
node.js
projectindex.js
file - run
node index.js
Relevant Code
No response
Browser Type
Google Chrome
WhatsApp Account Type
Standard
Does your WhatsApp account have multidevice enabled?
Yes, I am using Multi Device
Environment
Windows 11
Additional context
No response
Hi there!
It's not a bug, it's because you're using LocalAuth
strategy and saving your session files inside your selected path.
According to wwebjs.dev - LocalAuth documentation, you need a persistent file system to save your session and restore it, the authenticated
event won't return your session.
Also, almost all (maybe all) whatsapp numbers at today are using multidevice. So, if you were looking at LegacySessionAuth
strategy to save your session as JSON object, it won't work at all.
I would recommend you to try RemoteAuth
strategy, using a free MongoDB cluster.
import { MongoStore } from 'wwebjs-mongo';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
await mongoose.connect(process.env.MONGODB_URI!);
const store = new MongoStore({ mongoose });
const client = new Client({
authStrategy: new RemoteAuth({
store,
backupSyncIntervalMs: 300000, // in ms, minimum interval starts at 60000
clientId: `your-client-id`, // I would say it's required
dataPath: './your_sessions_path/', // optional
}),
restartOnAuthFail: true, // optional
puppeteer: {
headless: true,
args: [
'--no-sandbox',
// other node args
],
},
});
client.initialize();
Getting session in event client.on('authenticated')
is only available if you using LegacySessionAuth()
Hi, I am trying to send a message after the client is ready. The client is not getting ready for 2 minutes. But it is active on my mobile, not able to send messages. Kindly help me. Thanks in advance.
Hi, I am trying to send a message after the client is ready. The client is not getting ready for 2 minutes. But it is active on my mobile, not able to send messages. Kindly help me. Thanks in advance.
Try to update the lib version, you are probably running on an old version