Fix case where user can get stuck in a deleted organization if they were prev logged into it

This commit is contained in:
Tuan Dang
2025-02-18 18:35:22 -08:00
parent 8518fed726
commit 823bf134dd
8 changed files with 114 additions and 81 deletions
+27 -24
View File
@@ -24,10 +24,10 @@ import {
User,
UserEnc
} from "./types";
import { queryClient } from "@app/hooks/api/reactQuery";
export const fetchUserDetails = async () => {
const { data } = await apiRequest.get<{ user: User & UserEnc }>("/api/v1/user");
return data.user;
};
@@ -278,30 +278,33 @@ export const useRegisterUserAction = () => {
});
};
export const useLogoutUser = (keepQueryClient?: boolean) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async () => {
await apiRequest.post("/api/v1/auth/logout");
},
onSuccess: () => {
setAuthToken("");
// Delete the cookie by not setting a value; Alternatively clear the local storage
localStorage.removeItem("protectedKey");
localStorage.removeItem("protectedKeyIV");
localStorage.removeItem("protectedKeyTag");
localStorage.removeItem("publicKey");
localStorage.removeItem("encryptedPrivateKey");
localStorage.removeItem("iv");
localStorage.removeItem("tag");
localStorage.removeItem("PRIVATE_KEY");
localStorage.removeItem("orgData.id");
sessionStorage.removeItem(SessionStorageKeys.CLI_TERMINAL_TOKEN);
export const logoutUser = async () => {
await apiRequest.post("/api/v1/auth/logout");
};
if (!keepQueryClient) {
queryClient.clear();
}
}
// Utility function to clear session storage and query cache
export const clearSession = (keepQueryClient?: boolean) => {
setAuthToken(""); // Clear authentication token
localStorage.removeItem("protectedKey");
localStorage.removeItem("protectedKeyIV");
localStorage.removeItem("protectedKeyTag");
localStorage.removeItem("publicKey");
localStorage.removeItem("encryptedPrivateKey");
localStorage.removeItem("iv");
localStorage.removeItem("tag");
localStorage.removeItem("PRIVATE_KEY");
localStorage.removeItem("orgData.id");
sessionStorage.removeItem(SessionStorageKeys.CLI_TERMINAL_TOKEN);
if (!keepQueryClient) {
queryClient.clear(); // Clear React Query cache
}
};
export const useLogoutUser = (keepQueryClient?: boolean) => {
return useMutation({
mutationFn: logoutUser,
onSuccess: () => clearSession(keepQueryClient)
});
};