From feecae18f5736b6559cfb8dcdb2b357f2a138d74 Mon Sep 17 00:00:00 2001 From: Mohamad Tarbin Date: Sun, 29 Nov 2026 23:38:03 -0500 Subject: [PATCH] fix : https://github.com/donetick/frontend/issues/394 make sure the URL have no / in the end --- src/utils/ApiClient.js | 5 +++-- src/utils/ServerUrl.js | 4 ++++ src/views/Authorization/LoginSettings.jsx | 23 +++++++++++++++-------- 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 src/utils/ServerUrl.js diff --git a/src/utils/ApiClient.js b/src/utils/ApiClient.js index b520791ad..5ca2f2f23 100644 --- a/src/utils/ApiClient.js +++ b/src/utils/ApiClient.js @@ -10,13 +10,14 @@ import { clearSession } from './AuthSession' import { logout, RefreshToken } from './Fetcher' import { isOAuthExchangeInProgress } from './OAuthExchangeState' import { recordServerResponse, recordServerUnreachable } from './ServerHealth' +import { normalizeServerUrl } from './ServerUrl' import { isRefreshTokenExpired, saveTokens } from './TokenStorage' const OAUTH_EXCHANGE_IN_PROGRESS = 'OAuth exchange in progress' class ApiClient { constructor() { - this.customServerURL = `${API_URL}/api/v1` + this.customServerURL = `${normalizeServerUrl(API_URL)}/api/v1` this.isRefreshing = false this.failedQueue = [] this.lastRefreshTime = 0 @@ -44,7 +45,7 @@ class ApiClient { key: 'customServerUrl', }) - this.customServerURL = `${serverURL || API_URL}/api/v1` + this.customServerURL = `${normalizeServerUrl(serverURL || API_URL)}/api/v1` this.initialized = true } getApiURL() { diff --git a/src/utils/ServerUrl.js b/src/utils/ServerUrl.js new file mode 100644 index 000000000..bf4927148 --- /dev/null +++ b/src/utils/ServerUrl.js @@ -0,0 +1,4 @@ +export const normalizeServerUrl = url => + String(url ?? '') + .trim() + .replace(/\/+$/, '') diff --git a/src/views/Authorization/LoginSettings.jsx b/src/views/Authorization/LoginSettings.jsx index 836867b2d..67b3e5648 100644 --- a/src/views/Authorization/LoginSettings.jsx +++ b/src/views/Authorization/LoginSettings.jsx @@ -11,6 +11,7 @@ import { API_URL } from '../../Config' import { useResource } from '../../queries/ResourceQueries' import { apiClient } from '../../utils/ApiClient' import { offlineDB } from '../../utils/OfflineDB' +import { normalizeServerUrl } from '../../utils/ServerUrl' import { AuthSubmitButton, AuthTextField } from './AuthFields' import AuthShell from './AuthShell' import { authButtonSx } from './authStyles' @@ -36,7 +37,7 @@ const LoginSettings = () => { } const testConnection = async url => { - const testURL = url.replace(/\/+$/, '') + '/api/v1/resource' + const testURL = `${normalizeServerUrl(url)}/api/v1/resource` const controller = new AbortController() const timeoutId = setTimeout( () => controller.abort(), @@ -132,15 +133,18 @@ const LoginSettings = () => { const handleSave = async e => { e.preventDefault() - const trimmedURL = serverURL.trim() + const normalizedURL = normalizeServerUrl(serverURL) - if (trimmedURL === '') { - await Preferences.set({ key: 'customServerUrl', value: API_URL }) + if (normalizedURL === '') { + await Preferences.set({ + key: 'customServerUrl', + value: normalizeServerUrl(API_URL), + }) Navigate('/login') return } - if (!isValidURL(trimmedURL)) { + if (!isValidURL(normalizedURL)) { setStatus('error') setErrorMessage(t('server.invalidUrl')) return @@ -149,7 +153,7 @@ const LoginSettings = () => { setStatus('testing') setErrorMessage('') - const result = await testConnection(trimmedURL) + const result = await testConnection(normalizedURL) if (!result.ok) { setStatus('error') @@ -157,7 +161,7 @@ const LoginSettings = () => { return } - await Preferences.set({ key: 'customServerUrl', value: trimmedURL }) + await Preferences.set({ key: 'customServerUrl', value: normalizedURL }) try { await offlineDB.clearAll() } catch (e) { @@ -275,7 +279,10 @@ const LoginSettings = () => { disabled={isTesting} sx={{ ...authButtonSx, mt: 1 }} onClick={async () => { - await Preferences.set({ key: 'customServerUrl', value: API_URL }) + await Preferences.set({ + key: 'customServerUrl', + value: normalizeServerUrl(API_URL), + }) await apiClient.init(true) refetchResource() Navigate('/login')