Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class RelayAuth extends _$RelayAuth {
ref.read(relaysReplicaDelayProvider.notifier).setDelay();
return true;
}
// If relay is already authenticated with a different public key (user switch/logout race condition),
// close the relay connection. RelayClosedMixin will automatically invalidate the relay provider,
// causing the next request to get a fresh connection with correct credentials.
if (RelayAuthService.isAlreadyAuthenticatedWithDifferentKeyError(error)) {
relay.close();
}
return false;
},
);
Expand Down Expand Up @@ -188,4 +194,13 @@ class RelayAuthService {
static bool isRelayAuthoritativeError(Object? error) {
return error is SendEventException && error.code.startsWith('relay-is-authoritative');
}

/// Detects if the error indicates the relay is already authenticated with a different public key.
/// This can happen during user switch/logout when a relay connection is reused before being invalidated.
static bool isAlreadyAuthenticatedWithDifferentKeyError(Object? error) {
if (error is! SendEventException) return false;
final errorMessage = error.code.toLowerCase();
return errorMessage.contains('already authenticated') &&
errorMessage.contains('different public key');
}
}
15 changes: 10 additions & 5 deletions lib/app/features/user/providers/count_provider.r.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,16 @@ class Count extends _$Count {
},
);

await ref.read(ionConnectNotifierProvider.notifier).sendEvent(
requestEvent,
actionSource: ActionSourceRelayUrl(relay.keys.first.url),
cache: false,
);
try {
await ref.read(ionConnectNotifierProvider.notifier).sendEvent(
requestEvent,
actionSource: ActionSourceRelayUrl(relay.keys.first.url),
cache: false,
);
} catch (sendError) {
relay.keys.first.unsubscribe(subscription.id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we unsubscribe in the finally block already, why do we need it here?

rethrow;
}

final responseEntity = await messagesFuture;

Expand Down
24 changes: 14 additions & 10 deletions lib/app/features/user/providers/followers_count_provider.r.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ class FollowersCount extends _$FollowersCount {
),
];

return await ref.watch(
countProvider(
actionSource: ActionSourceUser(pubkey),
requestData: EventCountRequestData(filters: filters),
key: pubkey,
type: EventCountResultType.followers,
cache: cache,
network: network,
).future,
) as FutureOr<int?>;
try {
return await ref.watch(
countProvider(
actionSource: ActionSourceUser(pubkey),
requestData: EventCountRequestData(filters: filters),
key: pubkey,
type: EventCountResultType.followers,
cache: cache,
network: network,
).future,
) as FutureOr<int?>;
} catch (error) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will show incorrect data (0) instead of infinity loading, in case of exception, is it better / do we need to do that?

}
}

void addOne() {
Expand Down