( settings: LoadedSettings, setAuthError: (error: string | null) => void, config: Config, )
| 15 | } from '@anus-dev/anus-core'; |
| 16 | |
| 17 | export const useAuthCommand = ( |
| 18 | settings: LoadedSettings, |
| 19 | setAuthError: (error: string | null) => void, |
| 20 | config: Config, |
| 21 | ) => { |
| 22 | const [isAuthDialogOpen, setIsAuthDialogOpen] = useState(() => { |
| 23 | // Show auth dialog when no auth method is configured |
| 24 | if (settings.merged.selectedAuthType === undefined) { |
| 25 | const hasGrokKey = |
| 26 | process.env['GROK_API_KEY'] || settings.merged.grok?.apiKey; |
| 27 | |
| 28 | // Only skip auth dialog if we have a valid API key |
| 29 | if (hasGrokKey) { |
| 30 | return false; |
| 31 | } |
| 32 | // Show auth dialog to prompt for API key setup |
| 33 | return true; |
| 34 | } |
| 35 | return false; |
| 36 | }); |
| 37 | |
| 38 | const openAuthDialog = useCallback(() => { |
| 39 | setIsAuthDialogOpen(true); |
| 40 | }, []); |
| 41 | |
| 42 | const [isAuthenticating, setIsAuthenticating] = useState(false); |
| 43 | |
| 44 | useEffect(() => { |
| 45 | const authFlow = async () => { |
| 46 | const authType = settings.merged.selectedAuthType; |
| 47 | if (isAuthDialogOpen || !authType) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | setIsAuthenticating(true); |
| 53 | await config.refreshAuth(authType); |
| 54 | console.log(`Authenticated via "${authType}".`); |
| 55 | } catch (e) { |
| 56 | setAuthError( |
| 57 | `Grok authentication failed - please check your API key: ${getErrorMessage(e)}`, |
| 58 | ); |
| 59 | openAuthDialog(); |
| 60 | } finally { |
| 61 | setIsAuthenticating(false); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | void authFlow(); |
| 66 | }, [isAuthDialogOpen, settings, config, setAuthError, openAuthDialog]); |
| 67 | |
| 68 | const handleAuthSelect = useCallback( |
| 69 | async (authType: AuthType | undefined, scope: SettingScope) => { |
| 70 | if (authType) { |
| 71 | await clearCachedCredentialFile(); |
| 72 | |
| 73 | settings.setValue(scope, 'selectedAuthType', authType); |
| 74 | } |
no test coverage detected