fix: clean up WhatsApp credentials on QR cancel to prevent ghost channel (#637)
This commit is contained in:
committed by
GitHub
Unverified
parent
4d75dc1e5f
commit
7643cb8a75
@@ -2,7 +2,7 @@ import { dirname, join } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { createRequire } from 'module';
|
||||
import { EventEmitter } from 'events';
|
||||
import { existsSync, mkdirSync, rmSync } from 'fs';
|
||||
import { existsSync, mkdirSync, rmSync, readdirSync } from 'fs';
|
||||
import { deflateSync } from 'zlib';
|
||||
import { getOpenClawDir, getOpenClawResolvedDir } from './paths';
|
||||
|
||||
@@ -182,6 +182,7 @@ export class WhatsAppLoginManager extends EventEmitter {
|
||||
private qr: string | null = null;
|
||||
private accountId: string | null = null;
|
||||
private active: boolean = false;
|
||||
private loginSucceeded: boolean = false;
|
||||
private retryCount: number = 0;
|
||||
private maxRetries: number = 5;
|
||||
|
||||
@@ -195,6 +196,7 @@ export class WhatsAppLoginManager extends EventEmitter {
|
||||
private async finishLogin(accountId: string): Promise<void> {
|
||||
if (!this.active) return;
|
||||
console.log('[WhatsAppLogin] Finishing login, closing socket to hand over to Gateway...');
|
||||
this.loginSucceeded = true;
|
||||
await this.stop();
|
||||
// Allow enough time for WhatsApp server to fully release the session
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
@@ -221,6 +223,7 @@ export class WhatsAppLoginManager extends EventEmitter {
|
||||
|
||||
this.accountId = accountId;
|
||||
this.active = true;
|
||||
this.loginSucceeded = false;
|
||||
this.qr = null;
|
||||
this.retryCount = 0;
|
||||
|
||||
@@ -385,6 +388,8 @@ export class WhatsAppLoginManager extends EventEmitter {
|
||||
* Stop current login process
|
||||
*/
|
||||
async stop(): Promise<void> {
|
||||
const shouldCleanup = !this.loginSucceeded && this.accountId;
|
||||
const cleanupAccountId = this.accountId;
|
||||
this.active = false;
|
||||
this.qr = null;
|
||||
if (this.socket) {
|
||||
@@ -405,6 +410,31 @@ export class WhatsAppLoginManager extends EventEmitter {
|
||||
}
|
||||
this.socket = null;
|
||||
}
|
||||
|
||||
// Clean up the credentials directory that was created during start()
|
||||
// when the login was cancelled (not successfully authenticated).
|
||||
// This prevents listConfiguredChannels() from reporting WhatsApp
|
||||
// as configured based solely on the existence of this directory.
|
||||
if (shouldCleanup && cleanupAccountId) {
|
||||
try {
|
||||
const authDir = join(homedir(), '.openclaw', 'credentials', 'whatsapp', cleanupAccountId);
|
||||
if (existsSync(authDir)) {
|
||||
rmSync(authDir, { recursive: true, force: true });
|
||||
console.log(`[WhatsAppLogin] Cleaned up auth dir for cancelled login: ${authDir}`);
|
||||
// Also remove the parent whatsapp dir if it's now empty
|
||||
const parentDir = join(homedir(), '.openclaw', 'credentials', 'whatsapp');
|
||||
if (existsSync(parentDir)) {
|
||||
const remaining = readdirSync(parentDir);
|
||||
if (remaining.length === 0) {
|
||||
rmSync(parentDir, { recursive: true, force: true });
|
||||
console.log('[WhatsAppLogin] Removed empty whatsapp credentials directory');
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[WhatsAppLogin] Failed to clean up auth dir after cancel:', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user