Improve SABnzbd API key handling and settings UI

Clear credentials when switching download client types in the admin settings page and update validation logic for enabling the test connection button. In SABnzbd service, trim API key input and add explicit validation and error handling for missing or invalid API keys during connection tests.
This commit is contained in:
kikootwo
2026-01-07 10:31:57 -05:00
parent e008744df1
commit 24ea53bd2f
2 changed files with 47 additions and 3 deletions
+12 -2
View File
@@ -1532,9 +1532,15 @@ export default function AdminSettings() {
<select
value={settings.downloadClient.type}
onChange={(e) => {
// Clear credentials when switching client types
setSettings({
...settings,
downloadClient: { ...settings.downloadClient, type: e.target.value },
downloadClient: {
...settings.downloadClient,
type: e.target.value,
username: '', // Clear username (only used by qBittorrent)
password: '', // Clear password/API key
},
});
setValidated({ ...validated, download: false });
}}
@@ -1775,7 +1781,11 @@ export default function AdminSettings() {
<Button
onClick={testDownloadClientConnection}
loading={testing}
disabled={!settings.downloadClient.url || !settings.downloadClient.username || !settings.downloadClient.password}
disabled={
!settings.downloadClient.url ||
!settings.downloadClient.password ||
(settings.downloadClient.type === 'qbittorrent' && !settings.downloadClient.username)
}
variant="outline"
className="w-full"
>
+35 -1
View File
@@ -91,7 +91,7 @@ export class SABnzbdService {
disableSSLVerify: boolean = false
) {
this.baseUrl = baseUrl.replace(/\/$/, '');
this.apiKey = apiKey;
this.apiKey = apiKey?.trim() || '';
this.defaultCategory = defaultCategory;
this.disableSSLVerify = disableSSLVerify;
@@ -114,6 +114,40 @@ export class SABnzbdService {
*/
async testConnection(): Promise<{ success: boolean; version?: string; error?: string }> {
try {
// Validate API key is not empty
if (!this.apiKey || this.apiKey.trim() === '') {
return {
success: false,
error: 'API key is required for SABnzbd',
};
}
// Use queue endpoint to test authentication (requires valid API key)
const response = await this.client.get('/api', {
params: {
mode: 'queue',
output: 'json',
apikey: this.apiKey,
},
});
// Check if SABnzbd returned an error (invalid API key)
// SABnzbd can return errors in different formats:
// - { status: false, error: "message" }
// - { error: "message" }
// - Plain text error
if (response.data?.status === false || response.data?.error) {
const errorMsg = response.data?.error || 'Authentication failed';
return {
success: false,
error: errorMsg.includes('API Key')
? 'Invalid API key. Check your SABnzbd configuration (Config → General → API Key).'
: errorMsg,
};
}
// Queue endpoint requires auth - if we got here, API key is valid
// Now get the version
const version = await this.getVersion();
return { success: true, version };
} catch (error) {