From 66d2a2724e935a511a0b716a8a4ba469fdec86b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B0=95=EC=A4=80?= Date: Sun, 27 Nov 2022 16:12:25 +0900 Subject: [PATCH] ref(frontend): add ~/const path --- frontend/components/RouteGuard.js | 134 +++++++++--------- .../components/utilities/SecurityClient.js | 36 ++--- frontend/jsconfig.json | 3 + frontend/pages/_app.js | 66 ++++----- frontend/pages/api/auth/ChangePassword2.js | 54 +++---- frontend/pages/api/auth/CheckAuth.js | 26 ++-- .../api/auth/CheckEmailVerificationCode.js | 22 +-- .../auth/CompleteAccountInformationSignup.js | 62 ++++---- .../CompleteAccountInformationSignupInvite.js | 58 ++++---- .../pages/api/auth/IssueBackupPrivateKey.js | 62 ++++---- frontend/pages/api/auth/Login1.js | 22 +-- frontend/pages/api/auth/Login2.js | 39 +++-- frontend/pages/api/auth/Logout.js | 44 +++--- frontend/pages/api/auth/SRP1.js | 32 ++--- .../pages/api/auth/SendVerificationEmail.js | 20 +-- frontend/pages/api/auth/Token.js | 29 ++-- frontend/pages/api/auth/VerifySignupInvite.js | 24 ++-- frontend/pages/api/auth/publicKeyInfisical.js | 14 +- frontend/pages/api/files/GetSecrets.js | 46 +++--- frontend/pages/api/files/UploadSecrets.js | 38 ++--- .../integrations/ChangeHerokuConfigVars.js | 40 +++--- .../api/integrations/DeleteIntegration.js | 32 ++--- .../api/integrations/DeleteIntegrationAuth.js | 32 ++--- .../api/integrations/GetIntegrationApps.js | 32 ++--- .../pages/api/integrations/GetIntegrations.js | 26 ++-- .../api/integrations/StartIntegration.js | 46 +++--- .../api/integrations/authorizeIntegration.js | 42 +++--- .../getWorkspaceAuthorizations.js | 32 ++--- .../integrations/getWorkspaceIntegrations.js | 32 ++--- frontend/pages/api/organization/GetOrg.js | 29 ++-- .../pages/api/organization/GetOrgProjects.js | 32 ++--- .../api/organization/GetOrgSubscription.js | 32 ++--- .../api/organization/GetOrgUserProjects.js | 32 ++--- .../pages/api/organization/GetOrgUsers.js | 32 ++--- .../pages/api/organization/StripeRedirect.js | 32 ++--- .../api/organization/addIncidentContact.js | 38 ++--- .../pages/api/organization/addUserToOrg.js | 34 ++--- .../api/organization/deleteIncidentContact.js | 38 ++--- .../deleteUserFromOrganization.js | 32 ++--- .../api/organization/getIncidentContacts.js | 32 ++--- frontend/pages/api/organization/getOrgs.js | 26 ++-- frontend/pages/api/organization/renameOrg.js | 38 ++--- .../pages/api/serviceToken/addServiceToken.js | 58 ++++---- .../api/serviceToken/getServiceTokens.js | 32 ++--- frontend/pages/api/user/getUser.js | 26 ++-- .../pages/api/userActions/checkUserAction.js | 42 +++--- .../api/userActions/registerUserAction.js | 32 ++--- .../pages/api/workspace/addUserToWorkspace.js | 38 ++--- .../workspace/changeUserRoleInWorkspace.js | 38 ++--- .../pages/api/workspace/createWorkspace.js | 34 ++--- .../api/workspace/deleteUserFromWorkspace.js | 29 ++-- .../pages/api/workspace/deleteWorkspace.js | 26 ++-- .../pages/api/workspace/getLatestFileKey.js | 34 +++-- .../pages/api/workspace/getWorkspaceInfo.js | 32 ++--- .../pages/api/workspace/getWorkspaceKeys.js | 34 +++-- .../pages/api/workspace/getWorkspaceUsers.js | 32 ++--- frontend/pages/api/workspace/getWorkspaces.js | 26 ++-- .../pages/api/workspace/renameWorkspace.js | 38 ++--- frontend/pages/api/workspace/uploadKeys.js | 40 +++--- 59 files changed, 1077 insertions(+), 1086 deletions(-) diff --git a/frontend/components/RouteGuard.js b/frontend/components/RouteGuard.js index 71bfd2562..3a7b89ec6 100644 --- a/frontend/components/RouteGuard.js +++ b/frontend/components/RouteGuard.js @@ -1,85 +1,85 @@ -import { useEffect,useState } from "react"; +import { useEffect, useState } from "react"; import Image from "next/image"; import { useRouter } from "next/router"; import checkAuth from "~/pages/api/auth/CheckAuth"; -import { publicPaths } from "../const"; +import { publicPaths } from "~/const"; // #TODO: finish spinner only when the data loads fully // #TODO: Redirect somewhere if the page does not exist export default function RouteGuard({ children }) { - const router = useRouter(); - const [authorized, setAuthorized] = useState(false); + const router = useRouter(); + const [authorized, setAuthorized] = useState(false); - useEffect(() => { - // on initial load - run auth check - (async () => { - await authCheck(router.asPath); - })(); + useEffect(() => { + // on initial load - run auth check + (async () => { + await authCheck(router.asPath); + })(); - // on route change start - hide page content by setting authorized to false - // #TODO: add the loading page when not yet authorized. - const hideContent = () => setAuthorized(false); - // const onError = () => setAuthorized(true) - router.events.on("routeChangeStart", hideContent); - // router.events.on("routeChangeError", onError); + // on route change start - hide page content by setting authorized to false + // #TODO: add the loading page when not yet authorized. + const hideContent = () => setAuthorized(false); + // const onError = () => setAuthorized(true) + router.events.on("routeChangeStart", hideContent); + // router.events.on("routeChangeError", onError); - // on route change complete - run auth check - router.events.on("routeChangeComplete", authCheck); + // on route change complete - run auth check + router.events.on("routeChangeComplete", authCheck); - // unsubscribe from events in useEffect return function - return () => { - router.events.off("routeChangeStart", hideContent); - router.events.off("routeChangeComplete", authCheck); - // router.events.off("routeChangeError", onError); - }; - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + // unsubscribe from events in useEffect return function + return () => { + router.events.off("routeChangeStart", hideContent); + router.events.off("routeChangeComplete", authCheck); + // router.events.off("routeChangeError", onError); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); - /** - * redirect to login page if accessing a private page and not logged in - * @param {*} url - the url of the page we are trying to go to - */ - async function authCheck(url) { - // Make sure that we don't redirect when the user is on the following pages. - const path = "/" + url.split("?")[0].split("/")[1]; + /** + * redirect to login page if accessing a private page and not logged in + * @param {*} url - the url of the page we are trying to go to + */ + async function authCheck(url) { + // Make sure that we don't redirect when the user is on the following pages. + const path = "/" + url.split("?")[0].split("/")[1]; - // Check if the user is authenticated - const response = await checkAuth(); - // #TODO: figure our why sometimes it doesn't output a response - if (!publicPaths.includes(path)) { - try { - if (response.status !== 200) { - router.push("/login"); - console.log("Unauthorized to access."); - setAuthorized(false); - } else { - setAuthorized(true); - console.log("Authorized to access."); - } - } catch (error) { - console.log( - "Error (probably the authCheck route is stuck again...):", - error - ); - } - } - } + // Check if the user is authenticated + const response = await checkAuth(); + // #TODO: figure our why sometimes it doesn't output a response + if (!publicPaths.includes(path)) { + try { + if (response.status !== 200) { + router.push("/login"); + console.log("Unauthorized to access."); + setAuthorized(false); + } else { + setAuthorized(true); + console.log("Authorized to access."); + } + } catch (error) { + console.log( + "Error (probably the authCheck route is stuck again...):", + error + ); + } + } + } - if (authorized) { - return children; - } else { - return ( -
- google logo -
- ); - } + if (authorized) { + return children; + } else { + return ( +
+ google logo +
+ ); + } } diff --git a/frontend/components/utilities/SecurityClient.js b/frontend/components/utilities/SecurityClient.js index 783b44633..587b43517 100644 --- a/frontend/components/utilities/SecurityClient.js +++ b/frontend/components/utilities/SecurityClient.js @@ -1,28 +1,28 @@ import token from "~/pages/api/auth/Token"; -import { PATH } from "../../const"; +import { PATH } from "~/const"; export default class SecurityClient { - static authOrigins = [PATH]; - static #token = ""; + static authOrigins = [PATH]; + static #token = ""; - contructor() {} + contructor() {} - static setToken(token) { - this.#token = token; - } + static setToken(token) { + this.#token = token; + } - static async fetchCall(resource, options) { - let req = new Request(resource, options); - const destOrigin = new URL(req.url).origin; + static async fetchCall(resource, options) { + let req = new Request(resource, options); + const destOrigin = new URL(req.url).origin; - if (this.#token == "") { - this.setToken(await token()); - } + if (this.#token == "") { + this.setToken(await token()); + } - if (this.#token && this.authOrigins.includes(destOrigin)) { - req.headers.set("Authorization", "Bearer " + this.#token); - return fetch(req); - } - } + if (this.#token && this.authOrigins.includes(destOrigin)) { + req.headers.set("Authorization", "Bearer " + this.#token); + return fetch(req); + } + } } diff --git a/frontend/jsconfig.json b/frontend/jsconfig.json index 6f859d11a..19a4b9b7a 100644 --- a/frontend/jsconfig.json +++ b/frontend/jsconfig.json @@ -8,6 +8,9 @@ "~/utilities/*": [ "components/utilities/*" ], + "~/*": [ + "const" + ], "~/pages/*": [ "pages/*" ], diff --git a/frontend/pages/_app.js b/frontend/pages/_app.js index 838d407b9..ff8c2b7e3 100644 --- a/frontend/pages/_app.js +++ b/frontend/pages/_app.js @@ -7,7 +7,7 @@ import Layout from "~/components/basic/layout"; import RouteGuard from "~/components/RouteGuard"; import { ENV } from "~/utilities/config"; -import { publicPaths } from "../const.js"; +import { publicPaths } from "~/const"; import "@fortawesome/fontawesome-svg-core/styles.css"; import "../styles/globals.css"; @@ -15,49 +15,49 @@ import "../styles/globals.css"; config.autoAddCss = false; const App = ({ Component, pageProps, ...appProps }) => { - const router = useRouter(); - const posthog = initPostHog(); + const router = useRouter(); + const posthog = initPostHog(); - useEffect(() => { - // Init for auto capturing - const posthog = initPostHog(); + useEffect(() => { + // Init for auto capturing + const posthog = initPostHog(); - const handleRouteChange = () => { - if (typeof window !== "undefined") { - if (ENV == "production") { - posthog.capture("$pageview"); - } - } - }; + const handleRouteChange = () => { + if (typeof window !== "undefined") { + if (ENV == "production") { + posthog.capture("$pageview"); + } + } + }; - router.events.on("routeChangeComplete", handleRouteChange); + router.events.on("routeChangeComplete", handleRouteChange); - return () => { - router.events.off("routeChangeComplete", handleRouteChange); - }; - }, [router.events]); + return () => { + router.events.off("routeChangeComplete", handleRouteChange); + }; + }, [router.events]); - // If it's one of these routes, don't add the layout (e.g., these routes are external) - if ( - publicPaths.includes("/" + appProps.router.pathname.split("/")[1]) || - !Component.requireAuth - ) { - return ; - } + // If it's one of these routes, don't add the layout (e.g., these routes are external) + if ( + publicPaths.includes("/" + appProps.router.pathname.split("/")[1]) || + !Component.requireAuth + ) { + return ; + } - return ( - - - - - - ); + return ( + + + + + + ); }; export default App; { - /*