Skip to content
Merged
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
5 changes: 3 additions & 2 deletions src/utils/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/ServerUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const normalizeServerUrl = url =>
String(url ?? '')
.trim()
.replace(/\/+$/, '')
23 changes: 15 additions & 8 deletions src/views/Authorization/LoginSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -149,15 +153,15 @@ const LoginSettings = () => {
setStatus('testing')
setErrorMessage('')

const result = await testConnection(trimmedURL)
const result = await testConnection(normalizedURL)

if (!result.ok) {
setStatus('error')
setErrorMessage(result.message)
return
}

await Preferences.set({ key: 'customServerUrl', value: trimmedURL })
await Preferences.set({ key: 'customServerUrl', value: normalizedURL })
try {
await offlineDB.clearAll()
} catch (e) {
Expand Down Expand Up @@ -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')
Expand Down
Loading