refactor: move isAuthenticated from context to zustand store" (#2845)
* remove isAuthenticated from context and move it to zustand store * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * refactor: fix authentication logic in authStore.ts --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
ae096a3167
commit
90508b25e5
8 changed files with 15 additions and 20 deletions
|
|
@ -30,12 +30,12 @@ import { useFolderStore } from "./stores/foldersStore";
|
||||||
export default function App() {
|
export default function App() {
|
||||||
useTrackLastVisitedPath();
|
useTrackLastVisitedPath();
|
||||||
const isLoading = useFlowsManagerStore((state) => state.isLoading);
|
const isLoading = useFlowsManagerStore((state) => state.isLoading);
|
||||||
const { isAuthenticated, login, setUserData, getUser, logout } =
|
const { login, setUserData, getUser, logout } = useContext(AuthContext);
|
||||||
useContext(AuthContext);
|
|
||||||
const setAutoLogin = useAuthStore((state) => state.setAutoLogin);
|
const setAutoLogin = useAuthStore((state) => state.setAutoLogin);
|
||||||
const setLoading = useAlertStore((state) => state.setLoading);
|
const setLoading = useAlertStore((state) => state.setLoading);
|
||||||
const refreshStars = useDarkStore((state) => state.refreshStars);
|
const refreshStars = useDarkStore((state) => state.refreshStars);
|
||||||
const dark = useDarkStore((state) => state.dark);
|
const dark = useDarkStore((state) => state.dark);
|
||||||
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||||
|
|
||||||
useGetVersionQuery();
|
useGetVersionQuery();
|
||||||
const cookies = new Cookies();
|
const cookies = new Cookies();
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,8 @@ import { Navigate } from "react-router-dom";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
|
|
||||||
export const ProtectedAdminRoute = ({ children }) => {
|
export const ProtectedAdminRoute = ({ children }) => {
|
||||||
const { isAdmin, isAuthenticated, logout, userData } =
|
const { isAdmin, logout, userData } = useContext(AuthContext);
|
||||||
useContext(AuthContext);
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||||
|
|
||||||
const autoLogin = useAuthStore((state) => state.autoLogin);
|
const autoLogin = useAuthStore((state) => state.autoLogin);
|
||||||
|
|
||||||
if (!isAuthenticated) {
|
if (!isAuthenticated) {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
|
import useAuthStore from "@/stores/authStore";
|
||||||
import { useContext } from "react";
|
import { useContext } from "react";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
|
|
||||||
export const ProtectedRoute = ({ children }) => {
|
export const ProtectedRoute = ({ children }) => {
|
||||||
const { isAuthenticated, logout } = useContext(AuthContext);
|
const { logout } = useContext(AuthContext);
|
||||||
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||||
|
|
||||||
if (!isAuthenticated) {
|
if (!isAuthenticated) {
|
||||||
logout();
|
logout();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
import useAuthStore from "@/stores/authStore";
|
import useAuthStore from "@/stores/authStore";
|
||||||
import { useContext } from "react";
|
|
||||||
import { Navigate } from "react-router-dom";
|
import { Navigate } from "react-router-dom";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
|
||||||
|
|
||||||
export const ProtectedLoginRoute = ({ children }) => {
|
export const ProtectedLoginRoute = ({ children }) => {
|
||||||
const { isAuthenticated } = useContext(AuthContext);
|
|
||||||
const autoLogin = useAuthStore((state) => state.autoLogin);
|
const autoLogin = useAuthStore((state) => state.autoLogin);
|
||||||
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||||
|
|
||||||
if (autoLogin === true) {
|
if (autoLogin === true) {
|
||||||
window.location.replace("/");
|
window.location.replace("/");
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ import { AuthContextType } from "../types/contexts/auth";
|
||||||
const initialValue: AuthContextType = {
|
const initialValue: AuthContextType = {
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
setIsAdmin: () => false,
|
setIsAdmin: () => false,
|
||||||
isAuthenticated: false,
|
|
||||||
accessToken: null,
|
accessToken: null,
|
||||||
login: () => {},
|
login: () => {},
|
||||||
logout: () => new Promise(() => {}),
|
logout: () => new Promise(() => {}),
|
||||||
|
|
@ -44,9 +43,6 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const [accessToken, setAccessToken] = useState<string | null>(
|
const [accessToken, setAccessToken] = useState<string | null>(
|
||||||
cookies.get(LANGFLOW_ACCESS_TOKEN) ?? null,
|
cookies.get(LANGFLOW_ACCESS_TOKEN) ?? null,
|
||||||
);
|
);
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(
|
|
||||||
!!cookies.get(LANGFLOW_ACCESS_TOKEN),
|
|
||||||
);
|
|
||||||
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
||||||
const [userData, setUserData] = useState<Users | null>(null);
|
const [userData, setUserData] = useState<Users | null>(null);
|
||||||
const setLoading = useAlertStore((state) => state.setLoading);
|
const setLoading = useAlertStore((state) => state.setLoading);
|
||||||
|
|
@ -97,7 +93,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
function login(newAccessToken: string, autoLogin: string) {
|
function login(newAccessToken: string, autoLogin: string) {
|
||||||
cookies.set(LANGFLOW_AUTO_LOGIN_OPTION, autoLogin, { path: "/" });
|
cookies.set(LANGFLOW_AUTO_LOGIN_OPTION, autoLogin, { path: "/" });
|
||||||
setAccessToken(newAccessToken);
|
setAccessToken(newAccessToken);
|
||||||
setIsAuthenticated(true);
|
useAuthStore.getState().setIsAuthenticated(true);
|
||||||
getUser();
|
getUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,7 +108,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
setIsAdmin(false);
|
setIsAdmin(false);
|
||||||
setUserData(null);
|
setUserData(null);
|
||||||
setAccessToken(null);
|
setAccessToken(null);
|
||||||
setIsAuthenticated(false);
|
useAuthStore.getState().setIsAuthenticated(false);
|
||||||
setAllFlows([]);
|
setAllFlows([]);
|
||||||
setSelectedFolder(null);
|
setSelectedFolder(null);
|
||||||
navigate("/login");
|
navigate("/login");
|
||||||
|
|
@ -134,7 +130,6 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
value={{
|
value={{
|
||||||
isAdmin,
|
isAdmin,
|
||||||
setIsAdmin,
|
setIsAdmin,
|
||||||
isAuthenticated,
|
|
||||||
accessToken,
|
accessToken,
|
||||||
login,
|
login,
|
||||||
logout,
|
logout,
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ export default function LoginAdminPage() {
|
||||||
|
|
||||||
const [inputState, setInputState] =
|
const [inputState, setInputState] =
|
||||||
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
|
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
|
||||||
const { login, isAuthenticated, setUserData } = useContext(AuthContext);
|
const { login } = useContext(AuthContext);
|
||||||
const setLoading = useAlertStore((state) => state.setLoading);
|
const setLoading = useAlertStore((state) => state.setLoading);
|
||||||
|
|
||||||
const { password, username } = inputState;
|
const { password, username } = inputState;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
// authStore.js
|
// authStore.js
|
||||||
|
import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
|
||||||
import useFlowsManagerStore from "@/stores/flowsManagerStore";
|
import useFlowsManagerStore from "@/stores/flowsManagerStore";
|
||||||
import { AuthStoreType } from "@/types/zustand/auth";
|
import { AuthStoreType } from "@/types/zustand/auth";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
@ -19,8 +20,8 @@ const cookies = new Cookies();
|
||||||
|
|
||||||
const useAuthStore = create<AuthStoreType>((set, get) => ({
|
const useAuthStore = create<AuthStoreType>((set, get) => ({
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
isAuthenticated: !!cookies.get("access_token_lf"),
|
isAuthenticated: !!cookies.get(LANGFLOW_ACCESS_TOKEN),
|
||||||
accessToken: cookies.get("access_token_lf") ?? null,
|
accessToken: cookies.get(LANGFLOW_ACCESS_TOKEN) ?? null,
|
||||||
userData: null,
|
userData: null,
|
||||||
autoLogin: false,
|
autoLogin: false,
|
||||||
apiKey: cookies.get("apikey_tkn_lflw"),
|
apiKey: cookies.get("apikey_tkn_lflw"),
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ import { Users } from "../api";
|
||||||
export type AuthContextType = {
|
export type AuthContextType = {
|
||||||
isAdmin: boolean;
|
isAdmin: boolean;
|
||||||
setIsAdmin: (isAdmin: boolean) => void;
|
setIsAdmin: (isAdmin: boolean) => void;
|
||||||
isAuthenticated: boolean;
|
|
||||||
accessToken: string | null;
|
accessToken: string | null;
|
||||||
login: (accessToken: string, autoLogin: string) => void;
|
login: (accessToken: string, autoLogin: string) => void;
|
||||||
logout: () => Promise<void>;
|
logout: () => Promise<void>;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue