From 35292cf0aae29a2522a33132ab927d3446bcdf4f Mon Sep 17 00:00:00 2001 From: Janaka-Steph Date: Tue, 31 Oct 2023 14:28:09 +0100 Subject: [PATCH] Login screen --- src/App.tsx | 13 +++-- src/AppRouter.tsx | 2 - src/modules/onboarding/components/Login.tsx | 58 +++++++++++++++++++++ src/shared/api/authd.ts | 23 ++++++++ src/shared/store/setting.ts | 2 + src/shared/types/index.ts | 2 + 6 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/modules/onboarding/components/Login.tsx create mode 100644 src/shared/api/authd.ts diff --git a/src/App.tsx b/src/App.tsx index aa6b53d6..93200849 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,8 +3,10 @@ import AppRouter from "AppRouter"; import Onboarding from "modules/onboarding/components/Onboarding"; import { useEffect } from "react"; import Modal from "react-modal"; +import { ToastContainer } from "react-toastify"; import { isIP, isBrowserEnv, isDesktopEnv } from "shared/helpers/utils"; +import Login from "./modules/onboarding/components/Login"; import useSettingStore, { getBackendUrl } from "./shared/store/setting"; Modal.setAppElement("#root"); @@ -57,11 +59,12 @@ function App() { })(); }, [isSettingStoreHydrated]); - if (!isBrowser) { - return } />; - } - - return ; + return ( + <> + {isBrowser ? } /> : } />} + + + ); } export default App; diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index 4258547a..c7da5ad5 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -8,7 +8,6 @@ import Service from "modules/service/components/Service"; import ServiceDetail from "modules/service-detail/components/ServiceDetail"; import Settings from "modules/settings/components/Settings"; import { Route, BrowserRouter as Router, Routes } from "react-router-dom"; -import { ToastContainer } from "react-toastify"; import NotFound from "shared/components/NotFound"; import "react-toastify/dist/ReactToastify.css"; @@ -36,7 +35,6 @@ const AppRouter = () => { } /> - ); }; diff --git a/src/modules/onboarding/components/Login.tsx b/src/modules/onboarding/components/Login.tsx new file mode 100644 index 00000000..1ec4aa86 --- /dev/null +++ b/src/modules/onboarding/components/Login.tsx @@ -0,0 +1,58 @@ +import type { FormEvent, ReactElement } from "react"; +import { useState } from "react"; +import { toast } from "react-toastify"; + +import apiAuthd from "../../../shared/api/authd"; +import PrimaryButton from "../../../shared/components/PrimaryButton"; +import useSettingStore from "../../../shared/store/setting"; + +const Login = ({ redirectTo }: { redirectTo: ReactElement }) => { + const [isAuthenticated, setIsAuthenticated] = useState(false); + const setApiKey = useSettingStore((state) => state.setApiKey); + + const onSubmit = async (e: FormEvent) => { + e.preventDefault(); + const target = e.target as HTMLFormElement & { + user: { value: string }; + password: { value: string }; + }; + try { + const user = target.user.value; + const password = target.password.value; + await apiAuthd() + .get(`/login?user=${user}&pass=${password}`) + .then((res) => { + console.log("res", res); + setIsAuthenticated(true); + setApiKey(res.data); + }); + } catch (error: any) { + toast.error(error.message, { toastId: "login-error" }); + } finally { + target.reset(); + } + }; + + if (isAuthenticated) { + return redirectTo; + } + + return ( +
+

Login

+
+ + + Submit +
+
+ ); +}; + +export default Login; diff --git a/src/shared/api/authd.ts b/src/shared/api/authd.ts new file mode 100644 index 00000000..e96e3a3c --- /dev/null +++ b/src/shared/api/authd.ts @@ -0,0 +1,23 @@ +import axios from "axios"; + +import { isProxyEnabled } from "../helpers/utils"; +import useSettingStore from "../store/setting"; + +const apiAuthd = () => { + const isIP = useSettingStore.getState().isIP; + const headers = { "Content-Type": "application/json" }; + let baseURL; + if (isProxyEnabled()) { + if (isIP) { + baseURL = `${useSettingStore.getState().backendUrl}authd/auth`; + } else { + baseURL = `${window.location.protocol}//authd.${window.location.host}/auth`; + } + } + return axios.create({ + baseURL, + headers, + }); +}; + +export default apiAuthd; diff --git a/src/shared/store/setting.ts b/src/shared/store/setting.ts index 61117e5d..a4c297c0 100644 --- a/src/shared/store/setting.ts +++ b/src/shared/store/setting.ts @@ -91,6 +91,8 @@ const useSettingStore = create()( swarmInfo: null, setSwarmInfo: (swarmInfo: SwarmInfo | null) => set(() => ({ swarmInfo }), false, "setSwarmInfo"), + apiKey: null, + setApiKey: (apiKey: string) => set(() => ({ apiKey }), false, "setApiKey"), }), { name: "setting", diff --git a/src/shared/types/index.ts b/src/shared/types/index.ts index d3e73ee9..dc277775 100644 --- a/src/shared/types/index.ts +++ b/src/shared/types/index.ts @@ -112,6 +112,8 @@ export type SettingStore = { setSwarmMode: (mode: Swarm) => void; swarmInfo: SwarmInfo | null; setSwarmInfo: (swarmInfo: SwarmInfo | null) => void; + apiKey: string | null; + setApiKey: (apiKey: string) => void; }; export type HeaderProps = {