このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
kikikan/backend/resources/js/context.js

49 行
1.1 KiB
JavaScript
Raw 通常表示 履歴

2022-02-01 13:24:01 +09:00
import { createContext, useState } from "react";
2022-02-03 02:33:22 +09:00
import { useLocation, useNavigate } from "react-router-dom";
2022-02-01 13:24:01 +09:00
export const HeaderContext = createContext({});
export const HeaderContextProvider = ({ children }) => {
2022-02-03 02:33:22 +09:00
const {pathname} = useLocation();
const navigator = useNavigate();
const acc_type = pathname.split('/')[1];
2022-02-01 13:24:01 +09:00
const [selected_item_sidebar, SetSelectedItemOfSidebar] = useState('');
2022-02-03 02:33:22 +09:00
const handleLogout = () => {
axios.get(`/${acc_type}/logout`)
.then(() => {
localStorage.removeItem(`${acc_type}_token`);
navigator(`/${acc_type}/login`);
})
}
const isAuthenticate = () => {
let token = localStorage.getItem(`${acc_type}_token`);
if(!token){
navigator(`/${acc_type}/login`);
}
else{
token = JSON.parse(token);
expires = token.expires;
if(new Date().getTime() >= expires){
handleLogout();
}
}
}
2022-02-01 13:24:01 +09:00
return (
<HeaderContext.Provider
value={{
2022-02-03 02:33:22 +09:00
selected_item_sidebar, SetSelectedItemOfSidebar,
handleLogout,
isAuthenticate
2022-02-01 13:24:01 +09:00
}}
>
{children}
</HeaderContext.Provider>
);
};