2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00
Oliver 289af4e924
Refactor login state management (#7158)
* Refactor login state management

- Previously relied only on presence of cookie
- Cookie may not actually be *valid*
- Inspect actual login state by looking at userState values
- Ensures better sequencing of global state API requests
- Login state is now correctly preseed across browsers

* Ignore errors for user/me/ API endpoint in playwright test

* Do not request notifications unless logged in

* Prevent duplicate licenses

* Update src/frontend/src/views/DesktopAppView.tsx

Co-authored-by: Matthias Mair <code@mjmair.com>

* Simplify checkLoginState

* Fix bug in return types

* Update playwright tests

* linting

* Remove error msg

* Use token auth for API calls

- Will (hopefully) allow us to bypass csrfmiddle request handling?

* Refetch token if not available

* Use cache for DISPLAY_FULL_NAMES setting

* Update src/frontend/tests/baseFixtures.ts

Co-authored-by: Matthias Mair <code@mjmair.com>

* PUI test updates

* Tweak doLogout function

* Revert change to baseFixtures.ts

* Cleanup

* Fix highlighted property

* Test cleanup

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
2024-05-07 23:11:38 +10:00

142 lines
3.0 KiB
TypeScript

import { setApiDefaults } from '../App';
import { useServerApiState } from './ApiState';
import { useGlobalSettingsState, useUserSettingsState } from './SettingsState';
import { useGlobalStatusState } from './StatusState';
import { useUserState } from './UserState';
export interface Host {
host: string;
name: string;
}
export interface HostList {
[key: string]: Host;
}
// Type interface fully defining the current user
export interface UserProps {
pk: number;
username: string;
first_name: string;
last_name: string;
email: string;
is_staff?: boolean;
is_superuser?: boolean;
roles?: Record<string, string[]>;
}
// Type interface fully defining the current server
export interface ServerAPIProps {
server: null | string;
version: null | string;
instance: null | string;
apiVersion: null | number;
worker_running: null | boolean;
worker_pending_tasks: null | number;
plugins_enabled: null | boolean;
plugins_install_disabled: null | boolean;
active_plugins: PluginProps[];
email_configured: null | boolean;
debug_mode: null | boolean;
docker_mode: null | boolean;
database: null | string;
system_health: null | boolean;
platform: null | string;
installer: null | string;
target: null | string;
default_locale: null | string;
}
export interface AuthProps {
sso_enabled: boolean;
sso_registration: boolean;
mfa_required: boolean;
providers: Provider[];
registration_enabled: boolean;
password_forgotten_enabled: boolean;
}
export interface Provider {
id: string;
name: string;
configured: boolean;
login: string;
connect: string;
display_name: string;
}
// Type interface defining a single 'setting' object
export interface Setting {
pk: number;
key: string;
value: string;
name: string;
description: string;
type: SettingType;
units: string;
choices: SettingChoice[];
model_name: string | null;
api_url: string | null;
typ: SettingTyp;
plugin?: string;
method?: string;
required?: boolean;
}
export interface SettingChoice {
value: string;
display_name: string;
}
export enum SettingTyp {
Inventree = 'inventree',
Plugin = 'plugin',
User = 'user',
Notification = 'notification'
}
export enum SettingType {
Boolean = 'boolean',
Integer = 'integer',
String = 'string',
Choice = 'choice',
Model = 'related field'
}
export interface PluginProps {
name: string;
slug: string;
version: null | string;
}
// Errors
export type ErrorResponse = {
data: any;
status: number;
statusText: string;
message?: string;
};
export type SettingsLookup = {
[key: string]: string;
};
/*
* Refetch all global state information.
* Necessary on login, or if locale is changed.
*/
export function fetchGlobalStates() {
const { isLoggedIn } = useUserState.getState();
if (!isLoggedIn()) {
return;
}
setApiDefaults();
useServerApiState.getState().fetchServerApiState();
useUserState.getState().fetchUserState();
useUserSettingsState.getState().fetchSettings();
useGlobalSettingsState.getState().fetchSettings();
useGlobalStatusState.getState().fetchStatus();
}