Feroze Mohideen пре 2 година
родитељ
комит
e5bb3717ad

+ 1 - 1
dashboard/src/legacy/main/home/Home.tsx

@@ -23,6 +23,7 @@ import { useCustomerPlan } from "lib/hooks/useLago";
 import { checkIfProjectHasPayment } from "lib/hooks/useStripe";
 
 import api from "shared/api";
+import { useAuthn } from "shared/auth/AuthnContext";
 import { withAuth, type WithAuthProps } from "shared/auth/AuthorizationHoc";
 import { fakeGuardedRoute } from "shared/auth/RouteGuard";
 import { Context } from "shared/Context";
@@ -36,7 +37,6 @@ import {
   type ProjectType,
 } from "shared/types";
 
-import { useAuthn } from "../../shared/auth/AuthnContext";
 import OryLogin from "../auth/OryLogin";
 import AddonDashboard from "./add-on-dashboard/AddOnDashboard";
 import AddonTemplates from "./add-on-dashboard/AddonTemplates";

+ 1 - 1
dashboard/src/legacy/main/home/navbar/Navbar.tsx

@@ -20,7 +20,7 @@ const Navbar: React.FC<PropsType> = ({ logOut }) => {
   const { capabilities, user, setCurrentModal } = useContext(Context);
   const [showDropdown, setShowDropdown] = useState(false);
 
-  const { session } = useAuthn();
+  // const { session } = useAuthn();
 
   const renderSettingsDropdown = (): JSX.Element | null => {
     if (!showDropdown) {

+ 0 - 215
dashboard/src/legacy/shared/auth/AuthnContext.tsx

@@ -1,215 +0,0 @@
-import React, { useContext, useEffect, useState } from "react";
-import { type Session } from "@ory/client";
-import { Redirect, Route, Switch } from "react-router-dom";
-
-import api from "shared/api";
-import { Context } from "shared/Context";
-
-import Loading from "../../components/Loading";
-import LoginWrapper from "../../main/auth/LoginWrapper";
-import Register from "../../main/auth/Register";
-import ResetPasswordFinalize from "../../main/auth/ResetPasswordFinalize";
-import ResetPasswordInit from "../../main/auth/ResetPasswordInit";
-import SetInfo from "../../main/auth/SetInfo";
-import VerifyEmail from "../../main/auth/VerifyEmail";
-import CurrentError from "../../main/CurrentError";
-import { ory } from "./ory";
-
-type AuthnState = {
-  userId: number;
-  authenticate: () => Promise<void>;
-  handleLogOut: () => void;
-  session: Session | null;
-};
-
-export const AuthnContext = React.createContext<AuthnState | null>(null);
-
-export const useAuthn = (): AuthnState => {
-  const context = useContext(AuthnContext);
-  if (context == null) {
-    throw new Error("useAuthn must be used within an AuthnContext");
-  }
-  return context;
-};
-
-const AuthnProvider = ({
-  children,
-}: {
-  children: JSX.Element;
-}): JSX.Element => {
-  const { setUser, clearContext, setCurrentError, currentError } =
-    useContext(Context);
-  const [isLoggedIn, setIsLoggedIn] = useState(false);
-  const [isEmailVerified, setIsEmailVerified] = useState(false);
-  const [isLoading, setIsLoading] = useState(true);
-  const [userId, setUserId] = useState(-1);
-  const [logoutUrl, setLogoutUrl] = useState<string>("");
-  const [hasInfo, setHasInfo] = useState(false);
-  const [session, setSession] = useState<Session | null>(null);
-  const [local, setLocal] = useState(false);
-
-  const authenticate = async (): Promise<void> => {
-    try {
-      const { data: authData } = await api.checkAuth("", {}, {});
-      if (authData) {
-        setUser?.(authData.id, authData.email);
-        setIsLoggedIn(true);
-        setIsEmailVerified(authData.email_verified);
-        setHasInfo(authData.company_name && true);
-        setIsLoading(false);
-        setUserId(authData.id);
-      } else {
-        setIsLoggedIn(false);
-        setIsEmailVerified(false);
-        setHasInfo(false);
-        setIsLoading(false);
-        setUserId(-1);
-      }
-    } catch {
-      setIsLoggedIn(false);
-      setIsEmailVerified(false);
-      setHasInfo(false);
-      setIsLoading(false);
-      setUserId(-1);
-    }
-
-    try {
-      const { data: orySession } = await ory.toSession();
-      const { data: logOutData } = await ory.createBrowserLogoutFlow();
-      setLogoutUrl(logOutData.logout_url);
-      setSession(orySession);
-    } catch {
-      setSession(null);
-      setLogoutUrl("");
-    }
-  };
-
-  const handleLogOut = (): void => {
-    // Clears local storage for proper rendering of clusters
-    // Attempt user logout
-    api
-      .logOutUser("<token>", {}, {})
-      .then(() => {
-        setIsLoggedIn(false);
-        setIsEmailVerified(false);
-        clearContext?.();
-        localStorage.clear();
-      })
-      .catch((err) => {
-        setCurrentError?.(err.response?.data.errors[0]);
-      });
-
-    window.location.replace(logoutUrl);
-  };
-
-  useEffect(() => {
-    authenticate().catch(() => {});
-
-    // check if porter server is local (does not require email verification)
-    api
-      .getMetadata("", {}, {})
-      .then((res) => {
-        setLocal(!res.data?.provisioner);
-      })
-      .catch(() => {});
-  }, []);
-
-  if (isLoading) {
-    return <Loading />;
-  }
-
-  // return unauthenticated routes
-  if (!isLoggedIn) {
-    return (
-      <>
-        <Switch>
-          <Route
-            path="/login"
-            render={() => {
-              return <LoginWrapper authenticate={authenticate} />;
-            }}
-          />
-          <Route
-            path="/register"
-            render={() => {
-              return <Register authenticate={authenticate} />;
-            }}
-          />
-          <Route
-            path="/password/reset/finalize"
-            render={() => {
-              return <ResetPasswordFinalize />;
-            }}
-          />
-          <Route
-            path="/password/reset"
-            render={() => {
-              return <ResetPasswordInit />;
-            }}
-          />
-          <Route
-            path="*"
-            render={() => {
-              return <Redirect to="/login" />;
-            }}
-          />
-        </Switch>
-        <CurrentError currentError={currentError} />
-      </>
-    );
-  }
-
-  // if logged in but not verified, block until email verification
-  if (!local && !isEmailVerified) {
-    return (
-      <>
-        <Switch>
-          <Route
-            path="/"
-            render={() => {
-              return <VerifyEmail handleLogOut={handleLogOut} />;
-            }}
-          />
-        </Switch>
-        <CurrentError currentError={currentError} />
-      </>
-    );
-  }
-
-  // Handle case where new user signs up via OAuth and has not set name and company
-  if (!hasInfo && userId > 9312) {
-    return (
-      <>
-        <Switch>
-          <Route
-            path="/"
-            render={() => {
-              return (
-                <SetInfo
-                  handleLogOut={handleLogOut}
-                  authenticate={authenticate}
-                />
-              );
-            }}
-          />
-        </Switch>
-        <CurrentError currentError={currentError} />
-      </>
-    );
-  }
-
-  return (
-    <AuthnContext.Provider
-      value={{
-        userId,
-        authenticate,
-        handleLogOut,
-        session,
-      }}
-    >
-      {children}
-    </AuthnContext.Provider>
-  );
-};
-
-export default AuthnProvider;

+ 0 - 59
dashboard/src/legacy/shared/auth/AuthzContext.tsx

@@ -1,59 +0,0 @@
-import React, { useContext, useEffect, useState } from "react";
-
-import api from "shared/api";
-import { Context } from "shared/Context";
-
-import { POLICY_HIERARCHY_TREE, populatePolicy } from "./authorization-helpers";
-import { type PolicyDocType } from "./types";
-
-type AuthzContext = {
-  currentPolicy: PolicyDocType;
-};
-
-export const AuthzContext = React.createContext<AuthzContext>(
-  {} as AuthzContext
-);
-
-const AuthzProvider = ({
-  children,
-}: {
-  children: JSX.Element;
-}): JSX.Element => {
-  const { user, currentProject } = useContext(Context);
-  const [currentPolicy, setCurrentPolicy] = useState(null);
-
-  useEffect(() => {
-    let isSubscribed = true;
-    if (!user || !currentProject?.id) {
-      setCurrentPolicy(null);
-    } else {
-      api
-        .getPolicyDocument("<token>", {}, { project_id: currentProject?.id })
-        .then((res) => {
-          if (!isSubscribed) {
-            return;
-          }
-          const currentPolicy = res.data[0];
-          setCurrentPolicy(
-            populatePolicy(
-              currentPolicy,
-              POLICY_HIERARCHY_TREE,
-              currentPolicy.scope,
-              currentPolicy.verbs
-            )
-          );
-        });
-    }
-    return () => {
-      isSubscribed = false;
-    };
-  }, [user, currentProject?.id]);
-
-  return (
-    <AuthzContext.Provider value={{ currentPolicy }}>
-      {children}
-    </AuthzContext.Provider>
-  );
-};
-
-export default AuthzProvider;