Add navigation service for centralized login redirect routing
- Simplify token validation logic in API - Update axios interceptor to use navigation service
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import axios, { AxiosError } from 'axios'
|
||||
import { backendBaseUrl, webuiPrefix } from '@/lib/constants'
|
||||
import { backendBaseUrl } from '@/lib/constants'
|
||||
import { errorMessage } from '@/lib/utils'
|
||||
import { useSettingsStore } from '@/stores/settings'
|
||||
import { useAuthStore } from '@/stores/state'
|
||||
import { navigationService } from '@/services/navigation'
|
||||
|
||||
// Types
|
||||
export type LightragNodeType = {
|
||||
@@ -157,21 +158,13 @@ axiosInstance.interceptors.request.use((config) => {
|
||||
const apiKey = useSettingsStore.getState().apiKey
|
||||
const token = localStorage.getItem('LIGHTRAG-API-TOKEN');
|
||||
|
||||
// Check authentication status for paths that require authentication
|
||||
const authRequiredPaths = ['/documents', '/graphs', '/query', '/health']; // Add all paths that require authentication
|
||||
const isAuthRequired = authRequiredPaths.some(path => config.url?.includes(path));
|
||||
|
||||
if (isAuthRequired && !token && config.url !== '/login') {
|
||||
// Cancel the request and return a rejected Promise
|
||||
return Promise.reject(new Error('Authentication required'));
|
||||
}
|
||||
|
||||
if (apiKey) {
|
||||
config.headers['X-API-Key'] = apiKey
|
||||
}
|
||||
// Always include token if it exists, regardless of path
|
||||
if (token) {
|
||||
config.headers['Authorization'] = `Bearer ${token}`
|
||||
}
|
||||
if (apiKey) {
|
||||
config.headers['X-API-Key'] = apiKey
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
@@ -185,11 +178,11 @@ axiosInstance.interceptors.response.use(
|
||||
sessionStorage.clear();
|
||||
useAuthStore.getState().logout();
|
||||
|
||||
if (window.location.pathname !== `${webuiPrefix}/#/login`) {
|
||||
window.location.href = `${webuiPrefix}/#/login`;
|
||||
}
|
||||
// Use navigation service to handle redirection
|
||||
navigationService.navigateToLogin();
|
||||
|
||||
return Promise.reject(error);
|
||||
// Return a never-resolving promise to prevent further execution
|
||||
return new Promise(() => {});
|
||||
}
|
||||
throw new Error(
|
||||
`${error.response.status} ${error.response.statusText}\n${JSON.stringify(
|
||||
|
Reference in New Issue
Block a user