Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Migrate autoLogin control variable to Zustand store #2843

Merged
merged 7 commits into from
Jul 22, 2024
4 changes: 3 additions & 1 deletion src/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ import useTrackLastVisitedPath from "./hooks/use-track-last-visited-path";
import Router from "./routes";
import { Case } from "./shared/components/caseComponent";
import useAlertStore from "./stores/alertStore";
import useAuthStore from "./stores/authStore";
import { useDarkStore } from "./stores/darkStore";
import useFlowsManagerStore from "./stores/flowsManagerStore";
import { useFolderStore } from "./stores/foldersStore";

export default function App() {
useTrackLastVisitedPath();
const isLoading = useFlowsManagerStore((state) => state.isLoading);
const { isAuthenticated, login, setUserData, setAutoLogin, getUser, logout } =
const { isAuthenticated, login, setUserData, getUser, logout } =
useContext(AuthContext);
const setAutoLogin = useAuthStore((state) => state.setAutoLogin);
const setLoading = useAlertStore((state) => state.setLoading);
const refreshStars = useDarkStore((state) => state.refreshStars);
const dark = useDarkStore((state) => state.dark);
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/src/components/authAdminGuard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import useAuthStore from "@/stores/authStore";
import { useContext } from "react";
import { Navigate } from "react-router-dom";
import { AuthContext } from "../../contexts/authContext";

export const ProtectedAdminRoute = ({ children }) => {
const { isAdmin, isAuthenticated, logout, userData, autoLogin } =
const { isAdmin, isAuthenticated, logout, userData } =
useContext(AuthContext);

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

if (!isAuthenticated) {
logout();
} else if ((userData && !isAdmin) || autoLogin) {
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/components/authLoginGuard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import useAuthStore from "@/stores/authStore";
import { useContext } from "react";
import { Navigate } from "react-router-dom";
import { AuthContext } from "../../contexts/authContext";

export const ProtectedLoginRoute = ({ children }) => {
const { isAuthenticated, autoLogin } = useContext(AuthContext);
const { isAuthenticated } = useContext(AuthContext);
const autoLogin = useAuthStore((state) => state.autoLogin);

if (autoLogin === true) {
window.location.replace("/");
Expand Down
4 changes: 3 additions & 1 deletion 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 useAuthStore from "@/stores/authStore";
import useAlertStore from "../../stores/alertStore";
import { useDarkStore } from "../../stores/darkStore";
import useFlowStore from "../../stores/flowStore";
Expand All @@ -34,7 +35,8 @@ export default function Header(): JSX.Element {
const notificationCenter = useAlertStore((state) => state.notificationCenter);
const location = useLocation();

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

const navigate = useNavigate();
const removeFlow = useFlowsManagerStore((store) => store.removeFlow);
Expand Down
8 changes: 2 additions & 6 deletions src/frontend/src/contexts/authContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
LANGFLOW_API_TOKEN,
LANGFLOW_AUTO_LOGIN_OPTION,
} from "@/constants/constants";
import useAuthStore from "@/stores/authStore";
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import { createContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
Expand All @@ -29,8 +30,6 @@ const initialValue: AuthContextType = {
userData: null,
setUserData: () => {},
authenticationErrorCount: 0,
autoLogin: false,
setAutoLogin: () => {},
setApiKey: () => {},
apiKey: null,
storeApiKey: () => {},
Expand All @@ -50,7 +49,6 @@ export function AuthProvider({ children }): React.ReactElement {
);
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(LANGFLOW_API_TOKEN),
Expand Down Expand Up @@ -104,7 +102,7 @@ export function AuthProvider({ children }): React.ReactElement {
}

async function logout() {
if (autoLogin) {
if (useAuthStore.getState().autoLogin) {
return;
}
try {
Expand Down Expand Up @@ -143,8 +141,6 @@ export function AuthProvider({ children }): React.ReactElement {
setUserData,
userData,
authenticationErrorCount: 0,
setAutoLogin,
autoLogin,
setApiKey,
apiKey,
storeApiKey,
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/controllers/API/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
LANGFLOW_ACCESS_TOKEN,
LANGFLOW_AUTO_LOGIN_OPTION,
} 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";
Expand All @@ -19,8 +20,9 @@ const api: AxiosInstance = axios.create({
});

function ApiInterceptor() {
const autoLogin = useAuthStore((state) => state.autoLogin);
const setErrorData = useAlertStore((state) => state.setErrorData);
let { accessToken, logout, authenticationErrorCount, autoLogin } =
let { accessToken, logout, authenticationErrorCount } =
useContext(AuthContext);
const cookies = new Cookies();
const setSaveLoading = useFlowsManagerStore((state) => state.setSaveLoading);
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/modals/apiModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "ace-builds/src-noconflict/theme-github";
import "ace-builds/src-noconflict/theme-twilight";
import { ReactNode, forwardRef, useContext, useEffect, useState } from "react";
// import "ace-builds/webpack-resolver";
import useAuthStore from "@/stores/authStore";
import { cloneDeep } from "lodash";
import CodeTabsComponent from "../../components/codeTabsComponent";
import IconComponent from "../../components/genericIconComponent";
Expand Down Expand Up @@ -51,7 +52,8 @@ const ApiModal = forwardRef(
const tweaksList = useTweaksStore((state) => state.tweaksList);
const isThereTweaks = Object.keys(tweaksCode).length > 0;
const [activeTweaks, setActiveTweaks] = useState(false);
const { autoLogin } = useContext(AuthContext);
const autoLogin = useAuthStore((state) => state.autoLogin);

const [open, setOpen] =
mySetOpen !== undefined && myOpen !== undefined
? [myOpen, mySetOpen]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { usePostAddApiKey } from "@/controllers/API/queries/api-keys";
import { useGetProfilePicturesQuery } from "@/controllers/API/queries/files";
import useAuthStore from "@/stores/authStore";
import { useContext, useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { CONTROL_PATCH_USER_STATE } from "../../../../constants/constants";
Expand Down Expand Up @@ -31,8 +32,6 @@ export const GeneralPage = () => {
CONTROL_PATCH_USER_STATE,
);

const { autoLogin } = useContext(AuthContext);

const setSuccessData = useAlertStore((state) => state.setSuccessData);
const setErrorData = useAlertStore((state) => state.setErrorData);
const { userData, setUserData } = useContext(AuthContext);
Expand All @@ -41,6 +40,7 @@ export const GeneralPage = () => {
const hasApiKey = useStoreStore((state) => state.hasApiKey);
const loadingApiKey = useStoreStore((state) => state.loadingApiKey);
const { password, cnfPassword, profilePicture, apikey } = inputState;
const autoLogin = useAuthStore((state) => state.autoLogin);

const { storeApiKey } = useContext(AuthContext);
const setHasApiKey = useStoreStore((state) => state.updateHasApiKey);
Expand Down
2 changes: 0 additions & 2 deletions src/frontend/src/types/contexts/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export type AuthContextType = {
userData: Users | null;
setUserData: (userData: Users | null) => void;
authenticationErrorCount: number;
autoLogin: boolean;
setAutoLogin: (autoLogin: boolean) => void;
apiKey: string | null;
setApiKey: (apiKey: string | null) => void;
storeApiKey: (apiKey: string) => void;
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/types/zustand/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface AuthStoreType {
setAuthenticationErrorCount: (authenticationErrorCount: number) => void;
logout: () => Promise<void>;
// setUserData: (userData: Users | null) => void;
// setAutoLogin: (autoLogin: boolean) => void;
// setIsAdmin: (isAdmin: boolean) => void;
// setApiKey: (apiKey: string | null) => void;

Expand Down