Skip to content

Commit

Permalink
fix: correctly call the /variable endpoint after the user is authenti…
Browse files Browse the repository at this point in the history
…cated (#4276)

fix: correctly call the /variable endpoint after the user is authenticated (#4267)

* ✨ (authContext.tsx): Add functionality to fetch global variables on authentication
🔧 (api.tsx): Replace universal-cookie import with react-cookie for consistency
🔧 (authStore.ts): Replace universal-cookie import with react-cookie for consistency
🔧 (use-get-global-variables.ts): Add check to only fetch global variables if user is authenticated
✨ (use-get-mutation-global-variables.ts): Add mutation function to fetch and update global variables
🔧 (authStore.ts): Replace universal-cookie import with react-cookie for consistency

* ✨ (use-get-folders.ts): add authentication check before fetching folders data to ensure only authenticated users can access the data
  • Loading branch information
Cristhianzl authored and jordanrfrazier committed Oct 25, 2024
1 parent 6d7cce6 commit a4af411
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/frontend/src/contexts/authContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
LANGFLOW_REFRESH_TOKEN,
} from "@/constants/constants";
import { useGetUserData } from "@/controllers/API/queries/auth";
import { useGetGlobalVariablesMutation } from "@/controllers/API/queries/variables/use-get-mutation-global-variables";
import useAuthStore from "@/stores/authStore";
import { createContext, useEffect, useState } from "react";
import Cookies from "universal-cookie";
import { Cookies } from "react-cookie";
import { useStoreStore } from "../stores/storeStore";
import { Users } from "../types/api";
import { AuthContextType } from "../types/contexts/auth";
Expand Down Expand Up @@ -41,6 +42,7 @@ export function AuthProvider({ children }): React.ReactElement {
const setIsAuthenticated = useAuthStore((state) => state.setIsAuthenticated);

const { mutate: mutateLoggedUser } = useGetUserData();
const { mutate: mutateGetGlobalVariables } = useGetGlobalVariablesMutation();

useEffect(() => {
const storedAccessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
Expand Down Expand Up @@ -86,12 +88,17 @@ export function AuthProvider({ children }): React.ReactElement {
setAccessToken(newAccessToken);
setIsAuthenticated(true);
getUser();
getGlobalVariables();
}

function storeApiKey(apikey: string) {
setApiKey(apikey);
}

function getGlobalVariables() {
mutateGetGlobalVariables({});
}

return (
// !! to convert string to boolean
<AuthContext.Provider
Expand Down
20 changes: 13 additions & 7 deletions src/frontend/src/controllers/API/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { useCustomApiHeaders } from "@/customization/hooks/use-custom-api-header
import useAuthStore from "@/stores/authStore";
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
import * as fetchIntercept from "fetch-intercept";
import { useContext, useEffect } from "react";
import { useEffect } from "react";
import { Cookies } from "react-cookie";
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";
Expand All @@ -21,7 +20,14 @@ const cookies = new Cookies();
function ApiInterceptor() {
const autoLogin = useAuthStore((state) => state.autoLogin);
const setErrorData = useAlertStore((state) => state.setErrorData);
let { accessToken, authenticationErrorCount } = useContext(AuthContext);
const accessToken = useAuthStore((state) => state.accessToken);
const authenticationErrorCount = useAuthStore(
(state) => state.authenticationErrorCount,
);
const setAuthenticationErrorCount = useAuthStore(
(state) => state.setAuthenticationErrorCount,
);

const { mutate: mutationLogout } = useLogout();
const { mutate: mutationRenewAccessToken } = useRefreshAccessToken();
const isLoginPage = location.pathname.includes("login");
Expand Down Expand Up @@ -149,10 +155,10 @@ function ApiInterceptor() {
function checkErrorCount() {
if (isLoginPage) return;

authenticationErrorCount = authenticationErrorCount + 1;
setAuthenticationErrorCount(authenticationErrorCount + 1);

if (authenticationErrorCount > 3) {
authenticationErrorCount = 0;
setAuthenticationErrorCount(0);
mutationLogout();
return false;
}
Expand All @@ -169,9 +175,9 @@ function ApiInterceptor() {
}
mutationRenewAccessToken(undefined, {
onSuccess: async () => {
authenticationErrorCount = 0;
setAuthenticationErrorCount(0);
await remakeRequest(error);
authenticationErrorCount = 0;
setAuthenticationErrorCount(0);
},
onError: (error) => {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEFAULT_FOLDER, STARTER_FOLDER_NAME } from "@/constants/constants";
import { FolderType } from "@/pages/MainPage/entities";
import useAuthStore from "@/stores/authStore";
import { useFolderStore } from "@/stores/foldersStore";
import { useTypesStore } from "@/stores/typesStore";
import { useQueryFunctionType } from "@/types/api";
Expand All @@ -20,7 +21,10 @@ export const useGetFoldersQuery: useQueryFunctionType<
);
const setMyCollectionId = useFolderStore((state) => state.setMyCollectionId);

const isAuthenticated = useAuthStore((state) => state.isAuthenticated);

const getFoldersFn = async (): Promise<FolderType[]> => {
if (!isAuthenticated) return [];
const res = await api.get(`${getURL("FOLDERS")}/`);
const data = res.data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import useAuthStore from "@/stores/authStore";
import { useGlobalVariablesStore } from "@/stores/globalVariablesStore/globalVariables";
import getUnavailableFields from "@/stores/globalVariablesStore/utils/get-unavailable-fields";
import { useQueryFunctionType } from "@/types/api";
Expand All @@ -20,7 +21,10 @@ export const useGetGlobalVariables: useQueryFunctionType<
(state) => state.setUnavailableFields,
);

const isAuthenticated = useAuthStore((state) => state.isAuthenticated);

const getGlobalVariablesFn = async (): Promise<GlobalVariable[]> => {
if (!isAuthenticated) return [];
const res = await api.get(`${getURL("VARIABLES")}/`);
setGlobalVariablesEntries(res.data.map((entry) => entry.name));
setUnavailableFields(getUnavailableFields(res.data));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useGlobalVariablesStore } from "@/stores/globalVariablesStore/globalVariables";
import getUnavailableFields from "@/stores/globalVariablesStore/utils/get-unavailable-fields";
import { useMutationFunctionType } from "@/types/api";
import { GlobalVariable } from "@/types/global_variables";
import { UseMutationOptions, UseMutationResult } from "@tanstack/react-query";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

export const useGetGlobalVariablesMutation: useMutationFunctionType<
undefined
> = (options?) => {
const { mutate } = UseRequestProcessor();

const setGlobalVariablesEntries = useGlobalVariablesStore(
(state) => state.setGlobalVariablesEntries,
);
const setUnavailableFields = useGlobalVariablesStore(
(state) => state.setUnavailableFields,
);

const getGlobalVariablesFn = async (): Promise<GlobalVariable[]> => {
const res = await api.get(`${getURL("VARIABLES")}/`);
setGlobalVariablesEntries(res.data.map((entry) => entry.name));
setUnavailableFields(getUnavailableFields(res.data));
return res.data;
};

const mutation: UseMutationResult<undefined, Error, GlobalVariable[]> =
mutate(["useGetGlobalVariables"], getGlobalVariablesFn, options);

return mutation;
};
2 changes: 1 addition & 1 deletion src/frontend/src/stores/authStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// authStore.js
import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
import { AuthStoreType } from "@/types/zustand/auth";
import Cookies from "universal-cookie";
import { Cookies } from "react-cookie";
import { create } from "zustand";

const cookies = new Cookies();
Expand Down

0 comments on commit a4af411

Please sign in to comment.