* added esLint to frontend project * Remove formModalPropsType from index.ts * Remove chat input component * Delete codeBlock and fileComponent components * Remove unused code for BuildTrigger component * Refactor Chat component and remove unused code * Delete chatTrigger component * Delete unused SVG and PNG files * Remove LightTooltipComponent * Remove LoadingSpinner component * Delete RadialProgressComponent * Delete ReactTooltipComponent * Remove TooltipComponent * Delete ElementStack component * Remove ToggleComponent * Remove unused dependencies from package.json * Update package json * add accordion * change accordion sidebar to shadcn * added million lint * Remove MillionCompiler plugin from Vite config * Refactor authContext autoLogin dependency * Add console.log statements for debugging * Refactored position of elements in IO branch * fix assets imports * fix re-render on pageComponent * Add StrictMode to root render and update key in ExtraSidebarComponent * Add showCanvas state and useEffect to update it to improve performance * Refactor FlowPage component and remove ExtraSidebar from main area * Refactor FlowToolbar to prevent Unnecessary re-render * get node position with zustand in NodeToolbarComponent * Fix ShareModal rendering issue * Remove ExtraSidebar component and fix CodeAreaComponent bug * Refactor: Use useMemo to avoid unnecessary render * merge zustandIo * Remove console.log statements * Update package-lock.json and refactor extraSidebarComponent * update package json * Remove unused msgpack wheel file and add new nvidia_nvjitlink_cu12 wheel file * Fix import formatting in loading.py * Imported missing module and removed unused import --------- Co-authored-by: cristhianzl <cristhian.lousa@gmail.com> Co-authored-by: Lucas Oliveira <lucas.edu.oli@hotmail.com> Co-authored-by: igorrCarvalho <igorsilvabhz6@gmail.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai>
153 lines
3.8 KiB
TypeScript
153 lines
3.8 KiB
TypeScript
import { createContext, useEffect, useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import Cookies from "universal-cookie";
|
|
import {
|
|
autoLogin as autoLoginApi,
|
|
getLoggedUser,
|
|
requestLogout,
|
|
} from "../controllers/API";
|
|
import useAlertStore from "../stores/alertStore";
|
|
import useFlowsManagerStore from "../stores/flowsManagerStore";
|
|
import { Users } from "../types/api";
|
|
import { AuthContextType } from "../types/contexts/auth";
|
|
|
|
const initialValue: AuthContextType = {
|
|
isAdmin: false,
|
|
setIsAdmin: () => false,
|
|
isAuthenticated: false,
|
|
accessToken: null,
|
|
login: () => {},
|
|
logout: () => new Promise(() => {}),
|
|
userData: null,
|
|
setUserData: () => {},
|
|
authenticationErrorCount: 0,
|
|
autoLogin: false,
|
|
setAutoLogin: () => {},
|
|
setApiKey: () => {},
|
|
apiKey: null,
|
|
storeApiKey: () => {},
|
|
};
|
|
|
|
export const AuthContext = createContext<AuthContextType>(initialValue);
|
|
|
|
export function AuthProvider({ children }): React.ReactElement {
|
|
const navigate = useNavigate();
|
|
const cookies = new Cookies();
|
|
const [accessToken, setAccessToken] = useState<string | null>(
|
|
cookies.get("access_token_lf") ?? null
|
|
);
|
|
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(
|
|
!!cookies.get("access_token_lf")
|
|
);
|
|
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
|
const [userData, setUserData] = useState<Users | null>(null);
|
|
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
|
const setLoading = useAlertStore((state) => state.setLoading);
|
|
const [apiKey, setApiKey] = useState<string | null>(
|
|
cookies.get("apikey_tkn_lflw")
|
|
);
|
|
|
|
useEffect(() => {
|
|
const storedAccessToken = cookies.get("access_token_lf");
|
|
if (storedAccessToken) {
|
|
setAccessToken(storedAccessToken);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const apiKey = cookies.get("apikey_tkn_lflw");
|
|
if (apiKey) {
|
|
setApiKey(apiKey);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const isLoginPage = location.pathname.includes("login");
|
|
|
|
autoLoginApi()
|
|
.then((user) => {
|
|
if (user && user["access_token"]) {
|
|
user["refresh_token"] = "auto";
|
|
login(user["access_token"]);
|
|
setUserData(user);
|
|
setAutoLogin(true);
|
|
setLoading(false);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
setAutoLogin(false);
|
|
if (isAuthenticated && !isLoginPage) {
|
|
getUser();
|
|
} else {
|
|
setLoading(false);
|
|
useFlowsManagerStore.setState({ isLoading: false });
|
|
}
|
|
});
|
|
}, [autoLogin]);
|
|
|
|
function getUser() {
|
|
getLoggedUser()
|
|
.then((user) => {
|
|
setUserData(user);
|
|
setLoading(false);
|
|
const isSuperUser = user!.is_superuser;
|
|
setIsAdmin(isSuperUser);
|
|
})
|
|
.catch((error) => {
|
|
setLoading(false);
|
|
});
|
|
}
|
|
|
|
function login(newAccessToken: string) {
|
|
setAccessToken(newAccessToken);
|
|
setIsAuthenticated(true);
|
|
getUser();
|
|
}
|
|
|
|
async function logout() {
|
|
if (autoLogin) {
|
|
return;
|
|
}
|
|
try {
|
|
await requestLogout();
|
|
cookies.remove("apikey_tkn_lflw", { path: "/" });
|
|
setIsAdmin(false);
|
|
setUserData(null);
|
|
setAccessToken(null);
|
|
setIsAuthenticated(false);
|
|
navigate("/login");
|
|
} catch (error) {
|
|
console.error(error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
function storeApiKey(apikey: string) {
|
|
cookies.set("apikey_tkn_lflw", apikey, { path: "/" });
|
|
setApiKey(apikey);
|
|
}
|
|
|
|
return (
|
|
// !! to convert string to boolean
|
|
<AuthContext.Provider
|
|
value={{
|
|
isAdmin,
|
|
setIsAdmin,
|
|
isAuthenticated,
|
|
accessToken,
|
|
login,
|
|
logout,
|
|
setUserData,
|
|
userData,
|
|
authenticationErrorCount: 0,
|
|
setAutoLogin,
|
|
autoLogin,
|
|
setApiKey,
|
|
apiKey,
|
|
storeApiKey,
|
|
}}
|
|
>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|