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

redirect after client.on('authenticated') #3041

Closed
1 task done
ademir10 opened this issue May 17, 2024 · 2 comments
Closed
1 task done

redirect after client.on('authenticated') #3041

ademir10 opened this issue May 17, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ademir10
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

After to generate the QRcode or even already authenticated i can't make a redirect after client.initialize(), because i receive this message error:
node:_http_outgoing:696
throw new ERR_HTTP_HEADERS_SENT('set');
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Expected behavior

after to authenticated, be redirected to other page

Steps to Reproduce the Bug or Issue

//Async basic function requested by front-end:
gerar_qrcode: async function (req, res) {
const QRcode = require('qrcode');
client.on('qr', (qr) => {
const url = qr;
QRcode.toDataURL(url, (err, qrCodeUrl) => {
if (err) {
return console.log('erro ' + err);
}
return res.view({ qr_gerado: qrCodeUrl });
})
//if Authenticated redirect my front-end to the page called home:
}).on('authenticated', () => {
console.log('User Authenticated!');
return res.redirect('/pages/home');
});
client.initialize();
},

Relevant Code

//Full error message:
User Authenticated!
node:_http_outgoing:696
throw new ERR_HTTP_HEADERS_SENT('set');
^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (node:_http_outgoing:696:11)
at ServerResponse.header (/Users/ademir/apps/logchat/node_modules/express/lib/response.js:776:10)
at ServerResponse.location (/Users/ademir/apps/logchat/node_modules/express/lib/response.js:893:15)
at ServerResponse.redirect (/Users/ademir/apps/logchat/node_modules/express/lib/response.js:931:18)
at Client. (/Users/ademir/apps/logchat/api/controllers/PagesController.js:294:18)
at Client.emit (node:events:515:28)
at Client.initialize (/Users/ademir/apps/logchat/node_modules/whatsapp-web.js/src/Client.js:707:14)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}

Node.js v21.0.0
[nodemon] app crashed - waiting for file changes before starting...

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

OS: Mac
"node": "^21.0.0",
"whatsapp-web.js": "^1.23.0",

Additional context

there is some other way to check if client is authenticated?
thank you guys! i just posted it here because the Official WWebJS Discord Server is not working.

@ademir10 ademir10 added the bug Something isn't working label May 17, 2024
@alechkos
Copy link
Collaborator

Check your code

@ademir10
Copy link
Author

ademir10 commented May 17, 2024

Check your code

thank you for your help my dear friend! could you help me with this code? i'm trying to do it but i still not did it work as expected..

any suggestion? please..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants