fix(auth): prevent unauthorized requests and improve logout handling
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user