Skip to content

Commit

Permalink
refactor: change logout and refresh token to use useQuery (langflow-a…
Browse files Browse the repository at this point in the history
…i#2999)

✨ (headerComponent/index.tsx): Add functionality to handle user logout in the header component
🔧 (api.tsx): Add useLogout and useRefreshAccessToken functions to handle user logout and access token refresh
🔧 (use-post-logout.ts): Implement useLogout function to handle user logout functionality
🔧 (use-post-refresh-access.ts): Implement useRefreshAccessToken function to handle access token refresh
🔧 (authStore.ts): Remove unnecessary imports and update the authStore functionality for user logout and access token management.

Co-authored-by: anovazzi1 <[email protected]>
  • Loading branch information
Cristhianzl and anovazzi1 authored Jul 29, 2024
1 parent 1710bca commit 9903b44
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
21 changes: 17 additions & 4 deletions src/frontend/src/components/headerComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "../../constants/constants";
import { AuthContext } from "../../contexts/authContext";

import { useLogout } from "@/controllers/API/queries/auth";
import useAuthStore from "@/stores/authStore";
import useAlertStore from "../../stores/alertStore";
import { useDarkStore } from "../../stores/darkStore";
Expand All @@ -35,10 +36,13 @@ export default function Header(): JSX.Element {
const notificationCenter = useAlertStore((state) => state.notificationCenter);
const location = useLocation();

const { logout, userData } = useContext(AuthContext);
const { userData } = useContext(AuthContext);
const isAdmin = useAuthStore((state) => state.isAdmin);
const autoLogin = useAuthStore((state) => state.autoLogin);

const { mutate: mutationLogout } = useLogout();
const logout = useAuthStore((state) => state.logout);

const navigate = useNavigate();
const removeFlow = useFlowsManagerStore((store) => store.removeFlow);
const hasStore = useStoreStore((state) => state.hasStore);
Expand Down Expand Up @@ -83,6 +87,17 @@ export default function Header(): JSX.Element {
LOCATIONS_TO_RETURN.some((path) => location.pathname.includes(path)) &&
visitedFlowPathBefore();

const handleLogout = () => {
mutationLogout(undefined, {
onSuccess: () => {
logout();
},
onError: (error) => {
console.error(error);
},
});
};

return (
<div className="header-arrangement">
<div className="header-start-display lg:w-[407px]">
Expand Down Expand Up @@ -275,9 +290,7 @@ export default function Header(): JSX.Element {
<DropdownMenuSeparator />
<DropdownMenuItem
className="cursor-pointer gap-2"
onClick={() => {
logout();
}}
onClick={handleLogout}
>
<ForwardedIconComponent name="LogOut" className="w-4" />
Log Out
Expand Down
33 changes: 23 additions & 10 deletions src/frontend/src/controllers/API/api.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import {
LANGFLOW_ACCESS_TOKEN,
LANGFLOW_AUTO_LOGIN_OPTION,
} from "@/constants/constants";
import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
import useAuthStore from "@/stores/authStore";
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
import { useContext, useEffect } from "react";
import { Cookies } from "react-cookie";
import { renewAccessToken } from ".";
import { BuildStatus } from "../../constants/enums";
import { AuthContext } from "../../contexts/authContext";
import useAlertStore from "../../stores/alertStore";
import useFlowStore from "../../stores/flowStore";
import { checkDuplicateRequestAndStoreRequest } from "./helpers/check-duplicate-requests";
import { useLogout, useRefreshAccessToken } from "./queries/auth";

// Create a new Axios instance
const api: AxiosInstance = axios.create({
Expand All @@ -22,10 +19,12 @@ const api: AxiosInstance = axios.create({
function ApiInterceptor() {
const autoLogin = useAuthStore((state) => state.autoLogin);
const setErrorData = useAlertStore((state) => state.setErrorData);
let { accessToken, logout, authenticationErrorCount } =
useContext(AuthContext);
let { accessToken, authenticationErrorCount } = useContext(AuthContext);
const cookies = new Cookies();
const setSaveLoading = useFlowsManagerStore((state) => state.setSaveLoading);
const { mutate: mutationLogout } = useLogout();
const { mutate: mutationRenewAccessToken } = useRefreshAccessToken();
const logout = useAuthStore((state) => state.logout);

useEffect(() => {
const interceptor = api.interceptors.response.use(
Expand Down Expand Up @@ -127,7 +126,14 @@ function ApiInterceptor() {

if (authenticationErrorCount > 3) {
authenticationErrorCount = 0;
logout();
mutationLogout(undefined, {
onSuccess: () => {
logout();
},
onError: (error) => {
console.error(error);
},
});
return false;
}

Expand All @@ -137,10 +143,17 @@ function ApiInterceptor() {
async function tryToRenewAccessToken(error: AxiosError) {
try {
if (window.location.pathname.includes("/login")) return;
await renewAccessToken();
mutationRenewAccessToken({});
} catch (error) {
clearBuildVerticesState(error);
logout();
mutationLogout(undefined, {
onSuccess: () => {
logout();
},
onError: (error) => {
console.error(error);
},
});
return Promise.reject("Authentication error");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/controllers/API/queries/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export * from "./use-delete-users";
export * from "./use-get-autologin";
export * from "./use-get-user";
export * from "./use-get-users-page";
export * from "./use-patch-logout";
export * from "./use-patch-reset-password";
export * from "./use-patch-update-user";
export * from "./use-post-add-user";
export * from "./use-post-login-user";
export * from "./use-post-logout";
export * from "./use-post-refresh-access";
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useLogout: useMutationFunctionType<undefined, void> = (
if (autoLogin) {
return {};
}
const res = await api.patch(`${getURL("LOGOUT")}`);
const res = await api.post(`${getURL("LOGOUT")}`);
return res.data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const useRefreshAccessToken: useMutationFunctionType<undefined, any> = (
}

const mutation: UseMutationResult = mutate(
["useRefrshAccessToken"],
["useRefreshAccessToken"],
refreshAccess,
options,
);
Expand Down
10 changes: 0 additions & 10 deletions src/frontend/src/stores/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@
import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import { AuthStoreType } from "@/types/zustand/auth";
import { useNavigate } from "react-router-dom";
import Cookies from "universal-cookie";
import { create } from "zustand";
import {
getGlobalVariables,
getLoggedUser,
requestLogout,
} from "../controllers/API";
import useAlertStore from "../stores/alertStore";
import { useFolderStore } from "../stores/foldersStore";
import { useGlobalVariablesStore } from "../stores/globalVariablesStore/globalVariables";
import { useStoreStore } from "../stores/storeStore";
import { Users } from "../types/api";

const cookies = new Cookies();

Expand Down

0 comments on commit 9903b44

Please sign in to comment.