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
This commit is contained in:
parent
5811857ca2
commit
374d3af9c6
6 changed files with 63 additions and 9 deletions
|
|
@ -5,9 +5,10 @@ import {
|
||||||
LANGFLOW_REFRESH_TOKEN,
|
LANGFLOW_REFRESH_TOKEN,
|
||||||
} from "@/constants/constants";
|
} from "@/constants/constants";
|
||||||
import { useGetUserData } from "@/controllers/API/queries/auth";
|
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 useAuthStore from "@/stores/authStore";
|
||||||
import { createContext, useEffect, useState } from "react";
|
import { createContext, useEffect, useState } from "react";
|
||||||
import Cookies from "universal-cookie";
|
import { Cookies } from "react-cookie";
|
||||||
import { useStoreStore } from "../stores/storeStore";
|
import { useStoreStore } from "../stores/storeStore";
|
||||||
import { Users } from "../types/api";
|
import { Users } from "../types/api";
|
||||||
import { AuthContextType } from "../types/contexts/auth";
|
import { AuthContextType } from "../types/contexts/auth";
|
||||||
|
|
@ -41,6 +42,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const setIsAuthenticated = useAuthStore((state) => state.setIsAuthenticated);
|
const setIsAuthenticated = useAuthStore((state) => state.setIsAuthenticated);
|
||||||
|
|
||||||
const { mutate: mutateLoggedUser } = useGetUserData();
|
const { mutate: mutateLoggedUser } = useGetUserData();
|
||||||
|
const { mutate: mutateGetGlobalVariables } = useGetGlobalVariablesMutation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const storedAccessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
const storedAccessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
||||||
|
|
@ -86,12 +88,17 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
setAccessToken(newAccessToken);
|
setAccessToken(newAccessToken);
|
||||||
setIsAuthenticated(true);
|
setIsAuthenticated(true);
|
||||||
getUser();
|
getUser();
|
||||||
|
getGlobalVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeApiKey(apikey: string) {
|
function storeApiKey(apikey: string) {
|
||||||
setApiKey(apikey);
|
setApiKey(apikey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getGlobalVariables() {
|
||||||
|
mutateGetGlobalVariables({});
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
// !! to convert string to boolean
|
// !! to convert string to boolean
|
||||||
<AuthContext.Provider
|
<AuthContext.Provider
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,9 @@ import { useCustomApiHeaders } from "@/customization/hooks/use-custom-api-header
|
||||||
import useAuthStore from "@/stores/authStore";
|
import useAuthStore from "@/stores/authStore";
|
||||||
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
|
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
|
||||||
import * as fetchIntercept from "fetch-intercept";
|
import * as fetchIntercept from "fetch-intercept";
|
||||||
import { useContext, useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { Cookies } from "react-cookie";
|
import { Cookies } from "react-cookie";
|
||||||
import { BuildStatus } from "../../constants/enums";
|
import { BuildStatus } from "../../constants/enums";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
|
||||||
import useAlertStore from "../../stores/alertStore";
|
import useAlertStore from "../../stores/alertStore";
|
||||||
import useFlowStore from "../../stores/flowStore";
|
import useFlowStore from "../../stores/flowStore";
|
||||||
import { checkDuplicateRequestAndStoreRequest } from "./helpers/check-duplicate-requests";
|
import { checkDuplicateRequestAndStoreRequest } from "./helpers/check-duplicate-requests";
|
||||||
|
|
@ -21,7 +20,14 @@ const cookies = new Cookies();
|
||||||
function ApiInterceptor() {
|
function ApiInterceptor() {
|
||||||
const autoLogin = useAuthStore((state) => state.autoLogin);
|
const autoLogin = useAuthStore((state) => state.autoLogin);
|
||||||
const setErrorData = useAlertStore((state) => state.setErrorData);
|
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: mutationLogout } = useLogout();
|
||||||
const { mutate: mutationRenewAccessToken } = useRefreshAccessToken();
|
const { mutate: mutationRenewAccessToken } = useRefreshAccessToken();
|
||||||
const isLoginPage = location.pathname.includes("login");
|
const isLoginPage = location.pathname.includes("login");
|
||||||
|
|
@ -149,10 +155,10 @@ function ApiInterceptor() {
|
||||||
function checkErrorCount() {
|
function checkErrorCount() {
|
||||||
if (isLoginPage) return;
|
if (isLoginPage) return;
|
||||||
|
|
||||||
authenticationErrorCount = authenticationErrorCount + 1;
|
setAuthenticationErrorCount(authenticationErrorCount + 1);
|
||||||
|
|
||||||
if (authenticationErrorCount > 3) {
|
if (authenticationErrorCount > 3) {
|
||||||
authenticationErrorCount = 0;
|
setAuthenticationErrorCount(0);
|
||||||
mutationLogout();
|
mutationLogout();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -169,9 +175,9 @@ function ApiInterceptor() {
|
||||||
}
|
}
|
||||||
mutationRenewAccessToken(undefined, {
|
mutationRenewAccessToken(undefined, {
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
authenticationErrorCount = 0;
|
setAuthenticationErrorCount(0);
|
||||||
await remakeRequest(error);
|
await remakeRequest(error);
|
||||||
authenticationErrorCount = 0;
|
setAuthenticationErrorCount(0);
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { DEFAULT_FOLDER, STARTER_FOLDER_NAME } from "@/constants/constants";
|
import { DEFAULT_FOLDER, STARTER_FOLDER_NAME } from "@/constants/constants";
|
||||||
import { FolderType } from "@/pages/MainPage/entities";
|
import { FolderType } from "@/pages/MainPage/entities";
|
||||||
|
import useAuthStore from "@/stores/authStore";
|
||||||
import { useFolderStore } from "@/stores/foldersStore";
|
import { useFolderStore } from "@/stores/foldersStore";
|
||||||
import { useTypesStore } from "@/stores/typesStore";
|
import { useTypesStore } from "@/stores/typesStore";
|
||||||
import { useQueryFunctionType } from "@/types/api";
|
import { useQueryFunctionType } from "@/types/api";
|
||||||
|
|
@ -18,7 +19,10 @@ export const useGetFoldersQuery: useQueryFunctionType<
|
||||||
const setMyCollectionId = useFolderStore((state) => state.setMyCollectionId);
|
const setMyCollectionId = useFolderStore((state) => state.setMyCollectionId);
|
||||||
const setFolders = useFolderStore((state) => state.setFolders);
|
const setFolders = useFolderStore((state) => state.setFolders);
|
||||||
|
|
||||||
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||||
|
|
||||||
const getFoldersFn = async (): Promise<FolderType[]> => {
|
const getFoldersFn = async (): Promise<FolderType[]> => {
|
||||||
|
if (!isAuthenticated) return [];
|
||||||
const res = await api.get(`${getURL("FOLDERS")}/`);
|
const res = await api.get(`${getURL("FOLDERS")}/`);
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import useAuthStore from "@/stores/authStore";
|
||||||
import { useGlobalVariablesStore } from "@/stores/globalVariablesStore/globalVariables";
|
import { useGlobalVariablesStore } from "@/stores/globalVariablesStore/globalVariables";
|
||||||
import getUnavailableFields from "@/stores/globalVariablesStore/utils/get-unavailable-fields";
|
import getUnavailableFields from "@/stores/globalVariablesStore/utils/get-unavailable-fields";
|
||||||
import { useQueryFunctionType } from "@/types/api";
|
import { useQueryFunctionType } from "@/types/api";
|
||||||
|
|
@ -20,7 +21,10 @@ export const useGetGlobalVariables: useQueryFunctionType<
|
||||||
(state) => state.setUnavailableFields,
|
(state) => state.setUnavailableFields,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||||
|
|
||||||
const getGlobalVariablesFn = async (): Promise<GlobalVariable[]> => {
|
const getGlobalVariablesFn = async (): Promise<GlobalVariable[]> => {
|
||||||
|
if (!isAuthenticated) return [];
|
||||||
const res = await api.get(`${getURL("VARIABLES")}/`);
|
const res = await api.get(`${getURL("VARIABLES")}/`);
|
||||||
setGlobalVariablesEntries(res.data.map((entry) => entry.name));
|
setGlobalVariablesEntries(res.data.map((entry) => entry.name));
|
||||||
setUnavailableFields(getUnavailableFields(res.data));
|
setUnavailableFields(getUnavailableFields(res.data));
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
};
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
// authStore.js
|
// authStore.js
|
||||||
import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
|
import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
|
||||||
import { AuthStoreType } from "@/types/zustand/auth";
|
import { AuthStoreType } from "@/types/zustand/auth";
|
||||||
import Cookies from "universal-cookie";
|
import { Cookies } from "react-cookie";
|
||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
|
|
||||||
const cookies = new Cookies();
|
const cookies = new Cookies();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue