mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
fix: streamline WhatsApp login flow
This commit is contained in:
@@ -84,12 +84,6 @@ struct ConnectionsSettings: View {
|
||||
.buttonStyle(.bordered)
|
||||
.disabled(self.store.whatsappBusy)
|
||||
|
||||
Button("Wait for scan") {
|
||||
Task { await self.store.waitWhatsAppLogin() }
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.disabled(self.store.whatsappBusy)
|
||||
|
||||
Spacer()
|
||||
|
||||
Button("Logout") {
|
||||
|
||||
@@ -158,10 +158,11 @@ final class ConnectionsStore {
|
||||
}
|
||||
}
|
||||
|
||||
func startWhatsAppLogin(force: Bool) async {
|
||||
func startWhatsAppLogin(force: Bool, autoWait: Bool = true) async {
|
||||
guard !self.whatsappBusy else { return }
|
||||
self.whatsappBusy = true
|
||||
defer { self.whatsappBusy = false }
|
||||
var shouldAutoWait = false
|
||||
do {
|
||||
let params: [String: AnyCodable] = [
|
||||
"force": AnyCodable(force),
|
||||
@@ -174,12 +175,16 @@ final class ConnectionsStore {
|
||||
self.whatsappLoginMessage = result.message
|
||||
self.whatsappLoginQrDataUrl = result.qrDataUrl
|
||||
self.whatsappLoginConnected = nil
|
||||
shouldAutoWait = autoWait && result.qrDataUrl != nil
|
||||
} catch {
|
||||
self.whatsappLoginMessage = error.localizedDescription
|
||||
self.whatsappLoginQrDataUrl = nil
|
||||
self.whatsappLoginConnected = nil
|
||||
}
|
||||
await self.refresh(probe: true)
|
||||
if shouldAutoWait {
|
||||
Task { await self.waitWhatsAppLogin() }
|
||||
}
|
||||
}
|
||||
|
||||
func waitWhatsAppLogin(timeoutMs: Int = 120_000) async {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import { DisconnectReason } from "@whiskeysockets/baileys";
|
||||
|
||||
import { danger, info, success } from "../globals.js";
|
||||
import { logInfo } from "../logger.js";
|
||||
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
||||
@@ -7,6 +9,8 @@ import { renderQrPngBase64 } from "./qr-image.js";
|
||||
import {
|
||||
createWaSocket,
|
||||
formatError,
|
||||
getStatusCode,
|
||||
logoutWeb,
|
||||
readWebSelfId,
|
||||
waitForWaConnection,
|
||||
webAuthExists,
|
||||
@@ -22,6 +26,7 @@ type ActiveLogin = {
|
||||
qrDataUrl?: string;
|
||||
connected: boolean;
|
||||
error?: string;
|
||||
errorStatus?: number;
|
||||
waitPromise: Promise<void>;
|
||||
};
|
||||
|
||||
@@ -127,6 +132,7 @@ export async function startWebLoginWithQr(
|
||||
.catch((err) => {
|
||||
if (activeLogin?.id === login.id) {
|
||||
activeLogin.error = formatError(err);
|
||||
activeLogin.errorStatus = getStatusCode(err);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -186,6 +192,14 @@ export async function waitForWebLogin(
|
||||
}
|
||||
|
||||
if (login.error) {
|
||||
if (login.errorStatus === DisconnectReason.loggedOut) {
|
||||
await logoutWeb(runtime);
|
||||
const message =
|
||||
"WhatsApp reported the session is logged out. Cleared cached web session; please scan a new QR.";
|
||||
await resetActiveLogin(message);
|
||||
runtime.log(danger(message));
|
||||
return { connected: false, message };
|
||||
}
|
||||
const message = `WhatsApp login failed: ${login.error}`;
|
||||
await resetActiveLogin(message);
|
||||
runtime.log(danger(message));
|
||||
|
||||
Reference in New Issue
Block a user