diff --git a/web/src/routes/auth/cli/[code]/+page.svelte b/web/src/routes/auth/cli/[code]/+page.svelte index da00a7f4..12116f4a 100644 --- a/web/src/routes/auth/cli/[code]/+page.svelte +++ b/web/src/routes/auth/cli/[code]/+page.svelte @@ -3,10 +3,13 @@ import { page } from '$app/stores'; import { goto } from '$app/navigation'; import { api } from '$lib/api/client'; + import type { User } from '$lib/types'; let status = $state<'loading' | 'pending' | 'approved' | 'expired' | 'error' | 'success' | 'already_approved'>('loading'); let error = $state(''); let approving = $state(false); + let switchingAccount = $state(false); + let currentUser = $state(null); onMount(async () => { const code = $page.params.code; @@ -36,6 +39,15 @@ return; } + // Best-effort load of the current user so we can show which account + // the CLI session is about to be linked to. If this fails, the chip + // just won't render but the Approve flow still works. + try { + currentUser = await api.auth.me(); + } catch { + currentUser = null; + } + status = 'pending'; } catch { status = 'error'; @@ -65,6 +77,32 @@ approving = false; } } + + async function handleSwitchAccount() { + const code = $page.params.code; + if (!code) { + error = 'Missing CLI session code.'; + return; + } + switchingAccount = true; + error = ''; + try { + await api.auth.logout(); + } catch (err: unknown) { + // Surface the failure rather than swallowing it: if the server + // did not invalidate the session cookie, navigating to /login + // would bounce straight back here authenticated and "switch + // accounts" would appear to be a no-op. Showing the error + // gives the user a clear next step (retry / close the tab). + switchingAccount = false; + error = + err instanceof Error && err.message + ? `Couldn't sign you out: ${err.message}` + : "Couldn't sign you out. Please try again or close this tab."; + return; + } + goto(`/login?redirect=/auth/cli/${code}`, { replaceState: true }); + }
@@ -89,11 +127,41 @@ A CLI session is requesting access to your account. Approve this request to sign in from the terminal.

+ {#if currentUser} + + + {/if} + {#if error}

{error}

{/if} -