fleet/frontend/utilities/local.ts
Jacob Shandling 36be3d14a1
UI – Refrain from clearing the auth_token and redirecting to login for errors specific to this scenario (#14911)
## Addresses #12968


https://www.loom.com/share/37aaaa36936b47079ff3088c3430e36b?sid=c249306b-a32e-4a33-be83-aae2d13c98aa

- Improve the implementation of error reporting by `sendRequest` to
handle when AxiosError information is being provided in different fields
(`response`, `message`, `code`, or nowhere), as opposed to relying on
only the `response` field, which is empty in some (including this)
situations
- Using the more fine-grained reporting above, exempt `Request aborted`
errors when fetching a user's data, which is what occurs here, from
triggering a token clear and login page redirect.
- Use dedicated token handling utilities everywhere

## Checklist for submitter
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-11-03 08:32:41 -07:00

29 lines
653 B
TypeScript

const {
window: { localStorage },
} = global;
const local = {
clear: (): void => {
localStorage.clear();
},
getItem: (itemName: string): string | null => {
return localStorage.getItem(`FLEET::${itemName}`);
},
setItem: (itemName: string, value: string): void => {
return localStorage.setItem(`FLEET::${itemName}`, value);
},
removeItem: (itemName: string): void => {
localStorage.removeItem(`FLEET::${itemName}`);
},
};
export const authToken = (): string | null => {
return local.getItem("auth_token");
};
export const clearToken = (): void => {
return local.removeItem("auth_token");
};
export default local;