diff --git a/packages/cli/src/ui/components/AuthDialog.tsx b/packages/cli/src/ui/components/AuthDialog.tsx index d0601dc1446..6e6296c1074 100644 --- a/packages/cli/src/ui/components/AuthDialog.tsx +++ b/packages/cli/src/ui/components/AuthDialog.tsx @@ -52,8 +52,8 @@ export function AuthDialog({ useInput((_input, key) => { if (key.escape) { - if (settings.merged.selectedAuthType === undefined) { - // Prevent exiting if no auth method is set + if (settings.merged.selectedAuthType === undefined || errorMessage) { + // Prevent exiting if no auth method is set or there's an error message setErrorMessage( 'You must select an auth method to proceed. Press Ctrl+C twice to exit.', ); diff --git a/packages/cli/src/ui/hooks/useAuthCommand.ts b/packages/cli/src/ui/hooks/useAuthCommand.ts index c19a91b6a2e..176dd106bdb 100644 --- a/packages/cli/src/ui/hooks/useAuthCommand.ts +++ b/packages/cli/src/ui/hooks/useAuthCommand.ts @@ -27,43 +27,68 @@ export const useAuthCommand = ( }, []); const [isAuthenticating, setIsAuthenticating] = useState(false); + const [pendingAuthSelection, setPendingAuthSelection] = useState< + | { + authType: AuthType; + scope: SettingScope; + } + | undefined + >( + // If user already has a saved auth method, set it as pending for auto-authentication + settings.merged.selectedAuthType + ? { authType: settings.merged.selectedAuthType, scope: SettingScope.User } + : undefined, + ); useEffect(() => { const authFlow = async () => { - const authType = settings.merged.selectedAuthType; - if (isAuthDialogOpen || !authType) { + if (isAuthDialogOpen || !pendingAuthSelection) { return; } try { setIsAuthenticating(true); - await config.refreshAuth(authType); - console.log(`Authenticated via "${authType}".`); + await config.refreshAuth(pendingAuthSelection.authType); + settings.setValue( + pendingAuthSelection.scope, + 'selectedAuthType', + pendingAuthSelection.authType, + ); + console.log(`Authenticated via "${pendingAuthSelection.authType}".`); } catch (e) { setAuthError(`Failed to login. Message: ${getErrorMessage(e)}`); openAuthDialog(); } finally { setIsAuthenticating(false); + setPendingAuthSelection(undefined); } }; void authFlow(); - }, [isAuthDialogOpen, settings, config, setAuthError, openAuthDialog]); + }, [ + isAuthDialogOpen, + pendingAuthSelection, + settings, + config, + setAuthError, + openAuthDialog, + ]); const handleAuthSelect = useCallback( async (authType: AuthType | undefined, scope: SettingScope) => { if (authType) { await clearCachedCredentialFile(); - settings.setValue(scope, 'selectedAuthType', authType); + setPendingAuthSelection({ authType, scope }); } setIsAuthDialogOpen(false); setAuthError(null); }, - [settings, setAuthError], + [setAuthError], ); const cancelAuthentication = useCallback(() => { setIsAuthenticating(false); + setPendingAuthSelection(undefined); }, []); return {