fix: track all auth events, avoid race condition

This commit is contained in:
Alejandro Gómez
2026-03-20 10:44:14 +01:00
parent ff4f5f322b
commit 40c02cdac7

View File

@@ -20,6 +20,7 @@ import publishService, {
type RelayStatusUpdate,
} from "./publish-service";
import pool from "./relay-pool";
import relayAuthManager from "./relay-auth";
import type { IRelay } from "applesauce-relay";
// ============================================================================
@@ -209,6 +210,38 @@ class EventLogService {
}
});
}, RELAY_POLL_INTERVAL);
// Monitor auth state transitions via centralized manager
this.subscriptions.push(
relayAuthManager.states$
.pipe(pairwise())
.subscribe(([prevMap, currMap]) => {
for (const [url, currState] of currMap) {
const prevState = prevMap.get(url);
const prevStatus = prevState?.status ?? "none";
if (currState.status === prevStatus) continue;
if (currState.status === "authenticated") {
this.addEntry({ type: "AUTH", relay: url, status: "success" });
} else if (currState.status === "failed") {
this.addEntry({ type: "AUTH", relay: url, status: "failed" });
} else if (currState.status === "rejected") {
this.addEntry({ type: "AUTH", relay: url, status: "rejected" });
} else if (
currState.status === "challenge_received" &&
currState.challenge
) {
this.addEntry({
type: "AUTH",
relay: url,
status: "challenge",
challenge: currState.challenge,
});
}
}
}),
);
}
/**
@@ -270,37 +303,6 @@ class EventLogService {
}),
);
// Track authentication events
subscription.add(
relay.authenticated$
.pipe(
startWith(relay.authenticated),
pairwise(),
filter(([prev, curr]) => prev !== curr && curr === true),
)
.subscribe(() => {
this.addEntry({
type: "AUTH",
relay: url,
status: "success",
});
}),
);
// Track challenges
subscription.add(
relay.challenge$
.pipe(filter((challenge): challenge is string => !!challenge))
.subscribe((challenge) => {
this.addEntry({
type: "AUTH",
relay: url,
status: "challenge",
challenge,
});
}),
);
// Track notices — deduplicate per relay
subscription.add(
relay.notice$.subscribe((notice) => {