fix: ensure apiClient initialization and update resource queries in authentication flow

This commit is contained in:
Mo Tarbin
2026-02-10 00:42:25 -05:00
parent 1ce7749826
commit 9c115efecb
6 changed files with 33 additions and 15 deletions

View File

@@ -36,6 +36,10 @@ export const AuthProvider = ({ children }) => {
const login = async credentials => {
setIsLoading(true)
try {
// Ensure apiClient is initialized with the correct URL
await apiClient.init()
const currentBaseURL = apiClient.getApiURL()
const isNative =
typeof window !== 'undefined' && window.Capacitor?.isNativePlatform?.()
@@ -50,7 +54,7 @@ export const AuthProvider = ({ children }) => {
config.credentials = 'include'
}
const response = await fetch(`${baseURL}/auth/login`, config)
const response = await fetch(`${currentBaseURL}/auth/login`, config)
if (!response.ok) {
const error = await response.json()

View File

@@ -13,7 +13,7 @@ const isTokenValid = () => {
}
export const useResource = () => {
const { data, isLoading, error } = useQuery({
const { data, isLoading, error, refetch } = useQuery({
queryKey: ['resource'],
queryFn: async () => {
const response = await GetResource()
@@ -22,7 +22,6 @@ export const useResource = () => {
staleTime: 6 * 60 * 60 * 1000, // 6 hours in milliseconds
refetchOnWindowFocus: false,
refetchOnReconnect: false,
enabled: isTokenValid(), // Only run query when we have a valid token
})
return { data, isLoading, error }
return { data, isLoading, error, refetch }
}

View File

@@ -16,12 +16,12 @@ class ApiClient {
this.refreshCooldown = 3 * 1000 // 3 seconds in milliseconds
}
async init() {
async init(force = false) {
if (this.initPromise) {
return this.initPromise
}
if (this.initialized) {
if (this.initialized && !force) {
return Promise.resolve()
}

View File

@@ -35,7 +35,8 @@ const createChore = userID => {
}).then(response => response.json())
}
const signUp = (username, password, displayName, email) => {
const signUp = async (username, password, displayName, email) => {
await apiClient.init(true)
const baseURL = apiManager.getApiURL()
return fetch(`${baseURL}/auth/`, {
method: 'POST',
@@ -46,7 +47,8 @@ const signUp = (username, password, displayName, email) => {
})
}
const UpdatePassword = newPassword => {
const UpdatePassword = async newPassword => {
await apiClient.init(true)
const baseURL = apiManager.getApiURL()
return fetch(`${baseURL}/users/change_password`, {
method: 'PUT',
@@ -55,7 +57,8 @@ const UpdatePassword = newPassword => {
})
}
const login = (username, password) => {
const login = async (username, password) => {
await apiClient.init(true)
const baseURL = apiManager.getApiURL()
return fetch(`${baseURL}/auth/login`, {
headers: {
@@ -66,7 +69,8 @@ const login = (username, password) => {
})
}
const logout = () => {
const logout = async () => {
await apiClient.init(true)
const baseURL = apiManager.getApiURL()
const isNative =
typeof window !== 'undefined' && window.Capacitor?.isNativePlatform?.()
@@ -476,6 +480,7 @@ const GetLabels = async () => {
}
const GetResource = async () => {
await apiClient.init()
const basedURL = apiManager.getApiURL()
const resp = await fetch(`${basedURL}/resource`, {
method: 'GET',

View File

@@ -2,6 +2,7 @@ import { Box, Button, CircularProgress, Container, Typography } from '@mui/joy'
import { useEffect, useState } from 'react'
import Logo from '../../Logo'
import { Capacitor } from '@capacitor/core'
import Cookies from 'js-cookie'
import { useRef } from 'react'
import { Link, useNavigate, useParams } from 'react-router-dom'
@@ -42,7 +43,7 @@ const AuthenticationLoading = () => {
})
})
}
const handleOAuth2 = () => {
const handleOAuth2 = async () => {
// get provider from params:
const urlParams = new URLSearchParams(window.location.search)
const code = urlParams.get('code')
@@ -58,8 +59,11 @@ const AuthenticationLoading = () => {
}
if (code) {
await apiClient.init()
const baseURL = apiClient.getApiURL()
const redirectURI = Capacitor.isNativePlatform()
? 'donetick://auth/oauth2'
: `${window.location.origin}/auth/oauth2`
fetch(`${baseURL}/auth/oauth2/callback`, {
method: 'POST',
headers: {
@@ -68,7 +72,7 @@ const AuthenticationLoading = () => {
body: JSON.stringify({
code,
state: returnedState,
redirect_uri: `${window.location.origin}/auth/oauth2`,
redirect_uri: redirectURI,
}),
}).then(response => {
if (response.status === 200) {

View File

@@ -4,10 +4,12 @@ import React from 'react'
import { useNavigate } from 'react-router-dom'
import { API_URL } from '../../Config'
import Logo from '../../Logo'
import { useResource } from '../../queries/ResourceQueries'
import { useNotification } from '../../service/NotificationProvider'
import { apiClient } from '../../utils/ApiClient'
const LoginSettings = () => {
const Navigate = useNavigate()
const { refetch: refetchResource } = useResource()
const [serverURL, setServerURL] = React.useState('')
const { showError } = useNotification()
@@ -114,8 +116,12 @@ const LoginSettings = () => {
Preferences.set({
key: 'customServerUrl',
value: serverURL,
}).then(() => {
apiClient.customServerURL = serverURL + '/api/v1'
}).then(async () => {
// apiClient.customServerURL = serverURL + '/api/v1's
// Force re-initialization to reload from Preferences
await apiClient.init(true)
// refetch resource queries to update the API URL
refetchResource()
Navigate('/login')
})
}}