From 07e175e94ce2b0978c46f117e022d9ef13b895e8 Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Thu, 7 Sep 2023 11:01:39 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3=E5=91=A8?= =?UTF-8?q?=E3=82=8A=E3=80=80=E6=95=B4=E5=82=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/auth.ts | 3 +- src/contexts/AuthContext.tsx | 48 +++++++++++------------------- src/layouts/dashbord/navigator.tsx | 2 +- src/pages/common/Page404.tsx | 11 ++++--- src/pages/dashboard/index.tsx | 2 +- 5 files changed, 27 insertions(+), 39 deletions(-) diff --git a/src/api/auth.ts b/src/api/auth.ts index 9f61e36..c33cff3 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -3,8 +3,7 @@ import { getUrl } from "./url"; type MeResponse = { data: { - id: string; - name: string; + customer_name: string; email: string; }; } & APICommonResponse; diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 1a37e91..36ce7bd 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -8,69 +8,57 @@ type Auth = { initialized: boolean; authenticated: boolean; - userId: string | null; name: string; email: string; login: (email: string, password: string) => Promise; logout: VoidFunction; + me: VoidFunction; }; export const AuthContext = createContext({ initialized: false, authenticated: false, - userId: null, name: "", email: "", login: async (email: string, password: string) => false, logout: () => {}, + me: () => {}, }); type Props = HasChildren; function AuthContextProvider({ children }: Props) { const [initialized, setInitialized] = useState(false); - const [userId, setUserId] = useState(null); const [name, setName] = useState(""); const [email, setEmail] = useState(""); - const testLogin = () => { - //TODO MOCK対応 - setInitialized(true); - setUserId("testing"); - setName("testuser"); - setEmail("test@test.com"); - }; - const authenticated = useMemo(() => { - // return !!userId; - return true; - }, [userId]); + return !!email; + }, [email]); const { callAPI: callMe } = useAPICall({ apiMethod: me, backDrop: true, - onSuccess: (res) => { + onSuccess: ({ data }) => { setInitialized(true); - //削除予定 - testLogin(); + setEmail(data.email); + setName(data.customer_name); }, onFailed: () => { clear(); setInitialized(true); - - //削除予定 - testLogin(); }, }); const { callAPI: callLogin } = useAPICall({ apiMethod: APILogin, backDrop: true, - onSuccess: (res) => { - setInitialized(true); + onSuccess: ({ data }) => { + setEmail(data.email); + setName(data.customer_name); }, }); @@ -82,25 +70,23 @@ function AuthContextProvider({ children }: Props) { }); const clear = () => { - setUserId(null); setName(""); setEmail(""); }; const login = async (email: string, password: string) => { - //削除予定 - testLogin(); - return true; - // const res = await callLogin({ email, password }); - // if (!res) return false; - - // return res.result === ResultCode.SUCCESS; + const res = await callLogin({ email, password }); + return res?.result === ResultCode.SUCCESS; }; const logout = () => { callLogout({}); console.info("ログアウト"); }; + const meFetch = () => { + callMe({}); + }; + useEffect(() => { callMe({}); }, []); @@ -111,13 +97,13 @@ function AuthContextProvider({ children }: Props) { // Value initialized, authenticated, - userId, name, email, // Func login, logout, + me: meFetch, }} > {children} diff --git a/src/layouts/dashbord/navigator.tsx b/src/layouts/dashbord/navigator.tsx index 442aa07..1b82eb5 100644 --- a/src/layouts/dashbord/navigator.tsx +++ b/src/layouts/dashbord/navigator.tsx @@ -64,7 +64,7 @@ const itemCategory = { export default function Navigator(props: DrawerProps) { const { ...other } = props; - const { userId, name } = useAuth(); + const { name } = useAuth(); const { navigateWhenChanged } = useNavigateCustom(); diff --git a/src/pages/common/Page404.tsx b/src/pages/common/Page404.tsx index 6cbc8da..ba85ee4 100644 --- a/src/pages/common/Page404.tsx +++ b/src/pages/common/Page404.tsx @@ -7,15 +7,18 @@ import { getPath } from "routes/path"; export default function Page404() { const { navigateWhenChanged } = useNavigateCustom(); - const { authenticated } = useAuth(); + const { authenticated, initialized } = useAuth(); - // ログインページにアクセス経験ある場合は、ログインページへ遷移させる useEffect(() => { + if (!initialized) return; if (authenticated) { navigateWhenChanged(getPath(PageID.DASHBOARD_OVERVIEW)); return; + } else { + navigateWhenChanged(getPath(PageID.LOGIN)); + return; } - }, []); + }, [initialized, authenticated]); - return NotFound.; + return null; } diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx index 62b3e27..be9e6b9 100644 --- a/src/pages/dashboard/index.tsx +++ b/src/pages/dashboard/index.tsx @@ -1,6 +1,6 @@ import { Box } from "@mui/material"; -import { PageID, TabID } from "pages"; import useDashboard from "hooks/useDashBoard"; +import { PageID, TabID } from "pages"; import { useEffect } from "react"; export default function Overview() {