Handle error on logout (#6482)

* Handle error on logout

- Logout failure redirects to login page

* Handle case if user is undefined when fetching role data

* Cleanup error messages

* More error message cleanup
This commit is contained in:
Oliver 2024-02-15 01:17:01 +11:00 committed by GitHub
parent aa7eaaab3a
commit 0e1923a90c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 18 deletions

View File

@ -53,7 +53,11 @@ export const doBasicLogin = async (username: string, password: string) => {
*/
export const doLogout = async (navigate: any) => {
// Logout from the server session
await api.post(apiUrl(ApiEndpoints.user_logout));
await api.post(apiUrl(ApiEndpoints.user_logout)).catch(() => {
// If an error occurs here, we are likely already logged out
navigate('/login');
return;
});
// Logout from this session
// Note that clearToken() then calls setApiDefaults()

View File

@ -41,8 +41,8 @@ export const useGlobalSettingsState = create<SettingsStateProps>(
lookup: generate_lookup(response.data)
});
})
.catch((error) => {
console.error('Error fetching global settings:', error);
.catch((_error) => {
console.error('Error fetching global settings');
});
},
getSetting: (key: string, default_value?: string) => {
@ -75,8 +75,8 @@ export const useUserSettingsState = create<SettingsStateProps>((set, get) => ({
lookup: generate_lookup(response.data)
});
})
.catch((error) => {
console.error('Error fetching user settings:', error);
.catch((_error) => {
console.error('Error fetching user settings');
});
},
getSetting: (key: string, default_value?: string) => {
@ -115,11 +115,8 @@ export const createPluginSettingsState = ({
lookup: generate_lookup(settings)
});
})
.catch((error) => {
console.error(
`Error fetching plugin settings for plugin ${plugin}:`,
error
);
.catch((_error) => {
console.error(`Error fetching plugin settings for plugin ${plugin}`);
});
},
getSetting: (key: string, default_value?: string) => {

View File

@ -54,8 +54,8 @@ export const useUserState = create<UserStateProps>((set, get) => ({
};
set({ user: user });
})
.catch((error) => {
console.error('Error fetching user data:', error);
.catch((_error) => {
console.error('Error fetching user data');
});
// Fetch role data
@ -65,13 +65,15 @@ export const useUserState = create<UserStateProps>((set, get) => ({
const user: UserProps = get().user as UserProps;
// Update user with role data
user.roles = response.data?.roles ?? {};
user.is_staff = response.data?.is_staff ?? false;
user.is_superuser = response.data?.is_superuser ?? false;
set({ user: user });
if (user) {
user.roles = response.data?.roles ?? {};
user.is_staff = response.data?.is_staff ?? false;
user.is_superuser = response.data?.is_superuser ?? false;
set({ user: user });
}
})
.catch((error) => {
console.error('Error fetching user roles:', error);
.catch((_error) => {
console.error('Error fetching user roles');
});
},
checkUserRole: (role: UserRoles, permission: UserPermissions) => {