diff --git a/backend/resources/js/component/alert_state_msg.jsx b/backend/resources/js/component/alert_state_msg.jsx index 551bd339..9aeb2a3b 100644 --- a/backend/resources/js/component/alert_state_msg.jsx +++ b/backend/resources/js/component/alert_state_msg.jsx @@ -7,7 +7,6 @@ export default function AlertStateMessage() { const navigator = useNavigate(); const [_success, setSuccess] = useState(''); const [_400error, set400Error] = useState(''); - const acc_type = pathname.split('/')[1]; useEffect(() => { if(state){ @@ -19,16 +18,6 @@ export default function AlertStateMessage() { } navigator(pathname); } - - let token = localStorage.getItem(`${acc_type}_token`); - if(token){ - token = JSON.parse(token); - if(token.from_login) { - setSuccess('ログインに成功しました'); - token.from_login = false; - localStorage.setItem(`${acc_type}_token`, JSON.stringify(token)); - } - } }, [pathname]); return ( diff --git a/backend/resources/js/component/check_auth.jsx b/backend/resources/js/component/check_auth.jsx new file mode 100644 index 00000000..fa34828f --- /dev/null +++ b/backend/resources/js/component/check_auth.jsx @@ -0,0 +1,32 @@ +import { useEffect, useState } from "react"; +import { useLocation, useNavigate } from "react-router-dom"; +import Alert from './alert'; + +export default function CheckAuthenticate() { + const { pathname } = useLocation(); + const [_success, setSuccess] = useState(''); + const navigator = useNavigate(); + + useEffect(() => { + const acc_type = pathname.split('/')[1]; + + let token = localStorage.getItem(`${acc_type}_token`); + if(!token){ + navigator(`/${acc_type}/login`); + } + else{ + token = JSON.parse(token); + if(token.from_login) { + setSuccess('ログインに成功しました'); + token.from_login = false; + localStorage.setItem(`${acc_type}_token`, JSON.stringify(token)); + } + } + }, [pathname]); + + return ( + <> + { _success ? setSuccess('')}>{ _success } : null} + + ); +} \ No newline at end of file diff --git a/backend/resources/js/layout/DashboardLayout.js b/backend/resources/js/layout/DashboardLayout.js index 76bcbbcd..07059da0 100644 --- a/backend/resources/js/layout/DashboardLayout.js +++ b/backend/resources/js/layout/DashboardLayout.js @@ -1,4 +1,5 @@ import { Outlet } from 'react-router-dom'; +import CheckAuthenticate from '../component/check_auth'; import AlertStateMessage from '../component/alert_state_msg'; const DashboardLayout = ({side}) => { @@ -6,6 +7,7 @@ const DashboardLayout = ({side}) => { return (
+ {side}