Skip to content
Closed
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
4 changes: 2 additions & 2 deletions packages/cli/src/ui/components/AuthDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
Expand Down
39 changes: 32 additions & 7 deletions packages/cli/src/ui/hooks/useAuthCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down