From 1357016f9a8688f8c7444d8ae83bd50ee3995bc9 Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Tue, 18 Jul 2023 19:22:26 +0900 Subject: [PATCH] =?UTF-8?q?NotFound=E6=99=82=E3=81=AB=E3=83=AD=E3=82=B0?= =?UTF-8?q?=E3=82=A4=E3=83=B3=E7=94=BB=E9=9D=A2=E3=81=B8=E9=81=B7=E7=A7=BB?= =?UTF-8?q?=E3=81=95=E3=81=9B=E3=82=8B=E3=82=B1=E3=83=BC=E3=82=B9=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/auth/login.tsx | 9 +++++++-- src/pages/common/Page404.tsx | 16 ++++++++++++++++ src/storage/localstorage/index.ts | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/storage/localstorage/index.ts diff --git a/src/pages/auth/login.tsx b/src/pages/auth/login.tsx index d879c98..1b5c4e6 100644 --- a/src/pages/auth/login.tsx +++ b/src/pages/auth/login.tsx @@ -1,15 +1,16 @@ import { yupResolver } from "@hookform/resolvers/yup"; import { LoadingButton } from "@mui/lab"; -import { AppBar, Box, Grid, Stack, Typography } from "@mui/material"; +import { Box, Stack, Typography } from "@mui/material"; import { PageID } from "codes/page"; import InputAlert from "components/form/InputAlert"; import { FormProvider, RHFTextField } from "components/hook-form"; import useAuth from "hooks/useAuth"; import useNavigateCustom from "hooks/useNavigateCustom"; import useSnackbarCustom from "hooks/useSnackbarCustom"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { getPath } from "routes/path"; +import { StoreId, setStore } from "storage/localstorage"; import * as Yup from "yup"; type FormProps = { @@ -56,6 +57,10 @@ export default function Login() { } }; + useEffect(() => { + setStore(StoreId.LOGIN_ROUTE, true); + }, []); + return ( diff --git a/src/pages/common/Page404.tsx b/src/pages/common/Page404.tsx index 7766d71..d87104d 100644 --- a/src/pages/common/Page404.tsx +++ b/src/pages/common/Page404.tsx @@ -1,5 +1,21 @@ import { Box } from "@mui/material"; +import { PageID } from "codes/page"; +import useNavigateCustom from "hooks/useNavigateCustom"; +import { useEffect } from "react"; +import { getPath } from "routes/path"; +import { StoreId, getStore } from "storage/localstorage"; export default function Page404() { + const { navigateWhenChanged } = useNavigateCustom(); + + // ログインページにアクセス経験ある場合は、ログインページへ遷移させる + useEffect(() => { + const canLoginRoute = !!getStore(StoreId.LOGIN_ROUTE); + + if (canLoginRoute) { + navigateWhenChanged(getPath(PageID.LOGIN)); + } + }, []); + return NotFound.; } diff --git a/src/storage/localstorage/index.ts b/src/storage/localstorage/index.ts new file mode 100644 index 0000000..1252a0f --- /dev/null +++ b/src/storage/localstorage/index.ts @@ -0,0 +1,18 @@ +let id = 0; +export const StoreId = { + LOGIN_ROUTE: "LOGIN_ROUTE", +} as const; +export type StoreId = (typeof StoreId)[keyof typeof StoreId]; + +export const getStore = (id: StoreId): string | null => { + return localStorage.getItem(id); +}; +export const setStore = (id: StoreId, data: any) => { + localStorage.setItem(id, data); +}; +export const removeStore = (id: StoreId) => { + localStorage.removeItem(id); +}; +export const clearStore = () => { + localStorage.clear(); +};