Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: 認証情報をReact hooksを用いて実装 #32

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 7 additions & 58 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,74 +1,23 @@
import "./App.css";
import { useState, useEffect } from "react";
import { BrowserRouter, Route, Routes, Link, Navigate } from "react-router-dom";
import { BrowserRouter, Route, Routes, Link } from "react-router-dom";
import Dashboard from "./Dashboard";
import Signin from "./signin/Signin";
import Register from "./register/Register";
import Signup from "./signup/Signup";

// 認証状態を管理
function useAuth() {
const [isAuthenticated, setIsAuthenticated] = useState(false);

useEffect(() => {
const checkAuth = () => {
const cookies = document.cookie.split(";").map((cookie) => cookie.trim());
const authenticated = cookies.some((cookie) =>
cookie.startsWith("token="),
);
setIsAuthenticated(authenticated);
};

checkAuth();

// クッキーの変更を監視
const interval = setInterval(checkAuth, 3000); // 1秒ごとにチェック
return () => clearInterval(interval);
}, []);

return isAuthenticated;
}

function App() {
const isAuthenticated = useAuth();

return (
<div className="App">
<BrowserRouter>
<nav>
<Link to="/signup">Signup</Link> | <Link to="/signin">Signin</Link> |{" "}
<Link to="/dashboard">Dashboard</Link> |{" "}
<Link to="/register">Register</Link>
</nav>
<Link to="/signup">Signup</Link> | <Link to="/signin">Signin</Link> |{" "}
<Link to="/dashboard">Dashboard</Link> |{" "}
<Link to="/Register">Register</Link>
<Routes>
<Route
path="/"
element={
isAuthenticated ? (
<Dashboard />
) : (
<Navigate to="/signin" replace />
)
}
/>
<Route
path="/dashboard"
element={
isAuthenticated ? (
<Dashboard />
) : (
<Navigate to="/signin" replace />
)
}
/>
<Route
path="/register"
element={
isAuthenticated ? <Register /> : <Navigate to="/signin" replace />
}
/>
<Route path="/" element={<Dashboard />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/signin" element={<Signin />} />
<Route path="/signup" element={<Signup />} />
<Route path="/register" element={<Register />} />
<Route path="*" element={<h1>Not Found Page</h1>} />
</Routes>
</BrowserRouter>
Expand Down
24 changes: 10 additions & 14 deletions client/src/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { useAuth } from "./hooks/useAuth";

function Dashboard() {
const navigate = useNavigate();
const { isAuthenticated, isLoading } = useAuth();

useEffect(() => {
fetch("http://localhost:8000/api/signin/check", {
method: "GET",
credentials: "include",
})
.then((response) => {
if (!response.ok) {
navigate("/signin");
}
})
.catch((error) => {
console.error(error);
navigate("/signin");
});
}, [navigate]);
if (!isLoading && !isAuthenticated) {
navigate("/signin");
}
}, [isLoading, isAuthenticated, navigate]);

if (isLoading || !isAuthenticated) {
return null;
}

return (
<div>
Expand Down
31 changes: 31 additions & 0 deletions client/src/hooks/useAuth.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useState, useEffect } from "react";

export const useAuth = () => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
const checkAuth = async () => {
try {
const response = await fetch("http://localhost:8000/api/signin/check", {
method: "GET",
credentials: "include",
});
if (response.ok) {
setIsAuthenticated(true);
} else {
setIsAuthenticated(false);
}
} catch (error) {
console.error(error);
setIsAuthenticated(false);
} finally {
setIsLoading(false);
}
};

checkAuth();
}, []);

return { isAuthenticated, isLoading };
};
7 changes: 1 addition & 6 deletions client/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./index.css";

createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>,
);
createRoot(document.getElementById("root")).render(<App />);
26 changes: 11 additions & 15 deletions client/src/register/Register.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./register.css";
import { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { useAuth } from "../hooks/useAuth";
import "./register.css";

function Register() {
// 状態管理
Expand All @@ -24,23 +25,18 @@ function Register() {
whiskey: 0,
});

const { isAuthenticated, isLoading } = useAuth();
const navigate = useNavigate();

useEffect(() => {
fetch("http://localhost:8000/api/signin/check", {
method: "GET",
credentials: "include",
})
.then((response) => {
if (!response.ok) {
navigate("/signin");
}
})
.catch((error) => {
console.error(error);
navigate("/signin");
});
}, [navigate]);
if (!isLoading && !isAuthenticated) {
navigate("/signin");
}
}, [isLoading, isAuthenticated, navigate]);

if (isLoading || !isAuthenticated) {
return null;
}

// 今日の日付を取得してフォーマットする関数
const getTodayDate = () => {
Expand Down