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

@@ -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')
})
}}