fix(auth): prevent unauthorized requests and improve logout handling

This commit is contained in:
2025-11-14 20:51:24 +08:00
parent 034c190373
commit 5bacd45419
3 changed files with 34 additions and 9 deletions

View File

@@ -18,7 +18,11 @@ export function useProfileSetting() {
const { fetchUserInfo, userInfo } = useUserData();
useEffect(() => {
fetchUserInfo();
// 仅在已登录情况下才获取用户信息避免登录页触发401导致重复跳转
const authorization = localStorage.getItem('Authorization');
if (authorization && authorization !== '') {
fetchUserInfo();
}
}, [fetchUserInfo]);
const updateUserInfo = async (newUserInfo: Partial<IUserInfo>) => {
@@ -58,6 +62,12 @@ export function useLanguageSetting() {
try {
// LanguageAbbreviation to language
const translatedLanguage = LanguageAbbreviationKeysMap[language];
const authorization = localStorage.getItem('Authorization');
// 未登录时仅本地切换语言不请求后端避免401导致跳转
if (!authorization || authorization === '') {
setLanguage(translatedLanguage);
return;
}
await updateUserInfo({ language: translatedLanguage });
setLanguage(translatedLanguage);
} catch (error) {