Skip to content

Commit

Permalink
se agregan funcionalidades para la autenticacion
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidKingV committed Jun 17, 2024
1 parent 8487d9d commit c2cb166
Show file tree
Hide file tree
Showing 4 changed files with 452 additions and 19 deletions.
26 changes: 26 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Code</title>
</head>
<body>
<h1>QR Code for WhatsApp Web</h1>
<div id="status-message"></div>
<img id="qr-code" src="" alt="QR Code will be displayed here" style="display:none;">
<script>
fetch('/qr-status')
.then(response => response.json())
.then(data => {
if (data.authenticated) {
document.getElementById('status-message').textContent = 'Sesión ya iniciada';
} else {
const qrCodeImg = document.getElementById('qr-code');
qrCodeImg.src = `data:image/png;base64,${data.qrCode}`;
qrCodeImg.style.display = 'block';
}
});
</script>
</body>
</html>
43 changes: 35 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ app.use(bodyParser.urlencoded({ extended: true }));

const SECRET_KEY = process.env.SECRET_KEY;

let isAuthenticated = false; // Variable para verificar si está autenticado
let qrCodeData = '';

const client = new Client({
authStrategy: new LocalAuth()
});
Expand All @@ -23,15 +26,20 @@ client.on('ready', () => {
console.log('Client is ready!');
});

client.on('authenticated', () => {
isAuthenticated = true;
console.log('Client is authenticated');
});

client.on('auth_failure', () => {
isAuthenticated = false;
console.log('Authentication failed');
});

client.on('qr', qr => {
// Genera el QR y guárdalo como una imagen
qrcode.toFile(path.join(__dirname, 'qr.png'), qr, (err) => {
if (err) {
console.error('Error generating QR code', err);
} else {
console.log('QR code saved as qr.png');
}
});
qrCodeData = qr;
isAuthenticated = false; // Asegurarse de que no esté autenticado
console.log('QR code received');
});

client.initialize();
Expand Down Expand Up @@ -93,6 +101,25 @@ app.post('/send-media', authenticateToken, async (req, res) => {
}
});

app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});

// Ruta para servir el código QR
app.get('/qr-status', (req, res) => {
if (isAuthenticated) {
res.json({ authenticated: true });
} else {
qrcode.toBuffer(qrCodeData, (err, buffer) => {
if (err) {
res.status(500).send('Error generating QR code');
} else {
res.json({ authenticated: false, qrCode: buffer.toString('base64') });
}
});
}
});

app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Loading

0 comments on commit c2cb166

Please sign in to comment.