refactor: authentication logic and move isAdmin to zustand store (#2888)

* refactor: remove isAuthenticated from context and move it to zustand store

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Cristhian Zanforlin Lousa <72977554+Cristhianzl@users.noreply.github.com>
This commit is contained in:
anovazzi1 2024-07-23 11:59:13 -03:00 • committed by GitHub
commit b7bc36d32a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 6 additions and 12 deletions

View file

@ -4,10 +4,10 @@ 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, logout, userData } = useContext(AuthContext); const { logout, userData } = useContext(AuthContext);
const isAuthenticated = useAuthStore((state) => state.isAuthenticated); const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
const autoLogin = useAuthStore((state) => state.autoLogin); const autoLogin = useAuthStore((state) => state.autoLogin);
const isAdmin = useAuthStore((state) => state.isAdmin);
if (!isAuthenticated) { if (!isAuthenticated) {
logout(); logout();
} else if ((userData && !isAdmin) || autoLogin) { } else if ((userData && !isAdmin) || autoLogin) {

View file

@ -35,7 +35,8 @@ export default function Header(): JSX.Element {
const notificationCenter = useAlertStore((state) => state.notificationCenter); const notificationCenter = useAlertStore((state) => state.notificationCenter);
const location = useLocation(); const location = useLocation();
const { logout, isAdmin, userData } = useContext(AuthContext); const { logout, userData } = useContext(AuthContext);
const isAdmin = useAuthStore((state) => state.isAdmin);
const autoLogin = useAuthStore((state) => state.autoLogin); const autoLogin = useAuthStore((state) => state.autoLogin);
const navigate = useNavigate(); const navigate = useNavigate();

View file

@ -21,8 +21,6 @@ import { Users } from "../types/api";
import { AuthContextType } from "../types/contexts/auth"; import { AuthContextType } from "../types/contexts/auth";
const initialValue: AuthContextType = { const initialValue: AuthContextType = {
isAdmin: false,
setIsAdmin: () => false,
accessToken: null, accessToken: null,
login: () => {}, login: () => {},
logout: () => new Promise(() => {}), logout: () => new Promise(() => {}),
@ -43,7 +41,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 [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);
const [apiKey, setApiKey] = useState<string | null>( const [apiKey, setApiKey] = useState<string | null>(
@ -78,7 +75,7 @@ export function AuthProvider({ children }): React.ReactElement {
.then(async (user) => { .then(async (user) => {
setUserData(user); setUserData(user);
const isSuperUser = user!.is_superuser; const isSuperUser = user!.is_superuser;
setIsAdmin(isSuperUser); useAuthStore.getState().setIsAdmin(isSuperUser);
getFoldersApi(true, true); getFoldersApi(true, true);
const res = await getGlobalVariables(); const res = await getGlobalVariables();
setGlobalVariables(res); setGlobalVariables(res);
@ -105,7 +102,7 @@ export function AuthProvider({ children }): React.ReactElement {
await requestLogout(); await requestLogout();
cookies.remove(LANGFLOW_API_TOKEN, { path: "/" }); cookies.remove(LANGFLOW_API_TOKEN, { path: "/" });
cookies.remove(LANGFLOW_AUTO_LOGIN_OPTION, { path: "/" }); cookies.remove(LANGFLOW_AUTO_LOGIN_OPTION, { path: "/" });
setIsAdmin(false); useAuthStore.getState().setIsAdmin(false);
setUserData(null); setUserData(null);
setAccessToken(null); setAccessToken(null);
useAuthStore.getState().setIsAuthenticated(false); useAuthStore.getState().setIsAuthenticated(false);
@ -128,8 +125,6 @@ export function AuthProvider({ children }): React.ReactElement {
// !! to convert string to boolean // !! to convert string to boolean
<AuthContext.Provider <AuthContext.Provider
value={{ value={{
isAdmin,
setIsAdmin,
accessToken, accessToken,
login, login,
logout, logout,

View file

@ -1,8 +1,6 @@
import { Users } from "../api"; import { Users } from "../api";
export type AuthContextType = { export type AuthContextType = {
isAdmin: boolean;
setIsAdmin: (isAdmin: boolean) => void;
accessToken: string | null; accessToken: string | null;
login: (accessToken: string, autoLogin: string) => void; login: (accessToken: string, autoLogin: string) => void;
logout: () => Promise<void>; logout: () => Promise<void>;