Skip to content

Commit 470dcad

Browse files
committed
fix: uid and session id mismatch
1 parent 270fb3b commit 470dcad

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

src/routes/(app)/compete/[roomId]/+page.svelte

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
userId: string;
2121
name: string;
2222
image?: string;
23+
isHost?: boolean; // Add flag to track host status
24+
email?: string; // Add email for stable identification
2325
}
2426
2527
let roomId = page.params.roomId;
@@ -28,6 +30,7 @@
2830
let error = $state<string | null>(null);
2931
let loading = $state(true);
3032
let creatorId = $state<string | null>(null);
33+
let roomCreationTime = $state<number>(Date.now()); // Track room creation time
3134
3235
// Agora related variables - only need client and tracks
3336
let agoraClient = $state<IAgoraRTCClient | null>(null);
@@ -226,28 +229,37 @@
226229
}
227230
});
228231
229-
// Determine host (first user or lowest UID)
232+
// Determine host - first joiner or based on email if authentication is used
233+
let isCurrentUserHost = false;
234+
const userEmail = $session.data?.user?.email;
235+
230236
if (remoteUsersList.length === 0) {
231-
creatorId = uid.toString();
232-
console.log(`[AGORA] No other users found, setting self as host with UID ${uid}`);
237+
// First user to join becomes host
238+
isCurrentUserHost = true;
239+
console.log(`[AGORA] No other users found, setting self as host`);
233240
} else {
234-
// If there are remote users, use the lowest UID as creator
235-
const allUids = [uid.toString(), ...remoteUsersList.map((u) => u.uid.toString())];
236-
const lowestUid = allUids.sort()[0];
237-
creatorId = lowestUid;
238-
console.log(`[AGORA] Setting ${lowestUid} as host`);
241+
// Check participants to see if there's already a host
242+
const existingHost = participants.find(p => p.isHost);
243+
244+
// If no host is set yet, the first authenticated user becomes host
245+
if (!existingHost && userEmail) {
246+
isCurrentUserHost = true;
247+
console.log(`[AGORA] No host exists yet, setting authenticated user as host`);
248+
}
239249
}
240250
241251
// Add current user to participants
242252
const userName = $session.data?.user?.name || `You (${uid})`;
243253
participants = [
244254
...participants
245255
.filter((p) => p.userId !== uid.toString())
246-
.map((p) => ({ ...p, image: p.image ?? undefined })),
256+
.map((p) => ({ ...p })),
247257
{
248258
userId: uid.toString(),
249259
name: userName,
250-
image: $session.data?.user?.image ?? undefined
260+
image: $session.data?.user?.image ?? undefined,
261+
email: userEmail,
262+
isHost: isCurrentUserHost
251263
}
252264
];
253265
@@ -441,7 +453,12 @@
441453
}
442454
});
443455
444-
let isHost = $derived($session.data?.user?.id === creatorId);
456+
// Determine if current user is host
457+
let isHost = $derived(() => {
458+
const currentUserId = agoraClient?.uid?.toString();
459+
const currentUserParticipant = participants.find(p => p.userId === currentUserId);
460+
return !!currentUserParticipant?.isHost;
461+
});
445462
</script>
446463

447464
<!-- The UI remains similar but updates for our simplified approach -->
@@ -559,7 +576,7 @@
559576
</div>
560577
{/if}
561578
<span>{participant.name}</span>
562-
{#if participant.userId === creatorId}
579+
{#if participant.isHost}
563580
<Badge variant="secondary" class="ml-2">Host</Badge>
564581
{/if}
565582
</div>

0 commit comments

Comments
 (0)