mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-14 00:07:01 +08:00
Prevent my-traders API calls when user is not logged in
- Add authentication checks to SWR calls in App.tsx and AITradersPage.tsx - Only call api.getTraders when user and token are available - Modify loadConfigs to skip authenticated API calls when not logged in - Load only public supported models/exchanges for unauthenticated users - Update useEffect dependencies to include user and token 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -88,10 +88,14 @@ function App() {
|
|||||||
// window.location.hash = page === 'competition' ? '' : 'trader';
|
// window.location.hash = page === 'competition' ? '' : 'trader';
|
||||||
// };
|
// };
|
||||||
|
|
||||||
// 获取trader列表
|
// 获取trader列表(仅在用户登录时)
|
||||||
const { data: traders } = useSWR<TraderInfo[]>('traders', api.getTraders, {
|
const { data: traders } = useSWR<TraderInfo[]>(
|
||||||
refreshInterval: 10000,
|
user && token ? 'traders' : null,
|
||||||
});
|
api.getTraders,
|
||||||
|
{
|
||||||
|
refreshInterval: 10000,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// 当获取到traders后,设置默认选中第一个
|
// 当获取到traders后,设置默认选中第一个
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { api } from '../lib/api';
|
|||||||
import type { TraderInfo, CreateTraderRequest, AIModel, Exchange } from '../types';
|
import type { TraderInfo, CreateTraderRequest, AIModel, Exchange } from '../types';
|
||||||
import { useLanguage } from '../contexts/LanguageContext';
|
import { useLanguage } from '../contexts/LanguageContext';
|
||||||
import { t, type Language } from '../i18n/translations';
|
import { t, type Language } from '../i18n/translations';
|
||||||
|
import { useAuth } from '../contexts/AuthContext';
|
||||||
import { getExchangeIcon } from './ExchangeIcons';
|
import { getExchangeIcon } from './ExchangeIcons';
|
||||||
import { getModelIcon } from './ModelIcons';
|
import { getModelIcon } from './ModelIcons';
|
||||||
import { TraderConfigModal } from './TraderConfigModal';
|
import { TraderConfigModal } from './TraderConfigModal';
|
||||||
@@ -35,6 +36,7 @@ interface AITradersPageProps {
|
|||||||
|
|
||||||
export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
|
export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
|
||||||
const { language } = useLanguage();
|
const { language } = useLanguage();
|
||||||
|
const { user, token } = useAuth();
|
||||||
const [showCreateModal, setShowCreateModal] = useState(false);
|
const [showCreateModal, setShowCreateModal] = useState(false);
|
||||||
const [showEditModal, setShowEditModal] = useState(false);
|
const [showEditModal, setShowEditModal] = useState(false);
|
||||||
const [showModelModal, setShowModelModal] = useState(false);
|
const [showModelModal, setShowModelModal] = useState(false);
|
||||||
@@ -53,7 +55,7 @@ export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const { data: traders, mutate: mutateTraders } = useSWR<TraderInfo[]>(
|
const { data: traders, mutate: mutateTraders } = useSWR<TraderInfo[]>(
|
||||||
'traders',
|
user && token ? 'traders' : null,
|
||||||
api.getTraders,
|
api.getTraders,
|
||||||
{ refreshInterval: 5000 }
|
{ refreshInterval: 5000 }
|
||||||
);
|
);
|
||||||
@@ -61,6 +63,21 @@ export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
|
|||||||
// 加载AI模型和交易所配置
|
// 加载AI模型和交易所配置
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadConfigs = async () => {
|
const loadConfigs = async () => {
|
||||||
|
if (!user || !token) {
|
||||||
|
// 未登录时只加载公开的支持模型和交易所
|
||||||
|
try {
|
||||||
|
const [supportedModels, supportedExchanges] = await Promise.all([
|
||||||
|
api.getSupportedModels(),
|
||||||
|
api.getSupportedExchanges()
|
||||||
|
]);
|
||||||
|
setSupportedModels(supportedModels);
|
||||||
|
setSupportedExchanges(supportedExchanges);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to load supported configs:', err);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [modelConfigs, exchangeConfigs, supportedModels, supportedExchanges] = await Promise.all([
|
const [modelConfigs, exchangeConfigs, supportedModels, supportedExchanges] = await Promise.all([
|
||||||
api.getModelConfigs(),
|
api.getModelConfigs(),
|
||||||
@@ -88,7 +105,7 @@ export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
loadConfigs();
|
loadConfigs();
|
||||||
}, []);
|
}, [user, token]);
|
||||||
|
|
||||||
// 显示所有用户的模型和交易所配置(用于调试)
|
// 显示所有用户的模型和交易所配置(用于调试)
|
||||||
const configuredModels = allModels || [];
|
const configuredModels = allModels || [];
|
||||||
|
|||||||
Reference in New Issue
Block a user