Skip to content

Commit 9f1c287

Browse files
committed
ログインフォームを追加
1 parent 85c21e2 commit 9f1c287

File tree

6 files changed

+72
-40
lines changed

6 files changed

+72
-40
lines changed

server/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import csrf from "./lib/cross-origin/block-unknown-origin";
44
import cors from "./lib/cross-origin/multi-origin-cors";
55
import { initializeSocket } from "./lib/socket/socket";
66
import { allUrlMustBeValid, panic } from "./lib/utils";
7+
import adminRoutes from "./router/admin";
78
import chatRoutes from "./router/chat";
89
import coursesRoutes from "./router/courses";
910
import matchesRoutes from "./router/matches";
@@ -49,6 +50,7 @@ app.use("/courses", coursesRoutes);
4950
app.use("/requests", requestsRoutes);
5051
app.use("/matches", matchesRoutes);
5152
app.use("/chat", chatRoutes);
53+
app.use("/admin", adminRoutes);
5254

5355
export function main() {
5456
// サーバーの起動

server/src/router/admin.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { safeParseInt } from "common/lib/result/safeParseInt";
2+
import { serialize } from "cookie";
3+
import express from "express";
4+
import { z } from "zod";
5+
import { safeGetUserId } from "../firebase/auth/db";
6+
import * as core from "../functions/chat";
7+
import * as ws from "../lib/socket/socket";
8+
9+
const router = express.Router();
10+
11+
export const adminLoginForm = z.object({
12+
userName: z.string(),
13+
password: z.string(),
14+
});
15+
16+
router.post("/login", async (req, res) => {
17+
const user = await safeGetUserId(req);
18+
if (!user.ok) return res.status(401).send("auth error");
19+
20+
const form = adminLoginForm.safeParse(req.body);
21+
if (!form.success) {
22+
return res.status(422).send("invalid format");
23+
}
24+
if (form.data.userName !== "admin" && form.data.password !== "password") {
25+
return res.status(401).send("Failed to login Admin Page.");
26+
}
27+
// // 認証成功時にCookieを設定
28+
// const cookie = serialize("authToken", "admin-token", {
29+
// httpOnly: true,
30+
// secure: process.env.NODE_ENV === "production",
31+
// sameSite: "strict",
32+
// path: "/",
33+
// maxAge: 60 * 60 * 24, // 1日
34+
// });
35+
36+
res.status(201).json({ message: "Login successful" });
37+
});
38+
39+
export default router;

web/api/admin/login/route.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
import { serialize } from "cookie";
2-
import { NextResponse } from "next/server";
1+
import { credFetch } from "../../../firebase/auth/lib";
2+
import endpoints from "../../internal/endpoints";
33

4-
export async function POST(request) {
5-
const body = await request.json();
6-
const { name, password } = body;
7-
console.log("あああ", body);
8-
if (name === "admin" && password === "password123") {
9-
// 認証成功時にCookieを設定
10-
const cookie = serialize("authToken", "admin-token", {
11-
httpOnly: true,
12-
secure: process.env.NODE_ENV === "production",
13-
sameSite: "strict",
14-
path: "/",
15-
maxAge: 60 * 60 * 24, // 1日
16-
});
4+
export async function adminLogin(userName: string, password: string) {
5+
const body = { userName, password };
176

18-
const response = NextResponse.json({ message: "Login successful" });
19-
response.headers.set("Set-Cookie", cookie);
20-
return response;
7+
const res = await credFetch("POST", endpoints.adminLogin, body);
8+
9+
if (!res.ok) {
10+
const errorData = await res.json();
11+
throw new Error(errorData.message || "ログインに失敗しました。");
2112
}
2213

23-
return NextResponse.json({ message: "Invalid credentials" }, { status: 401 });
14+
return res.json();
2415
}

web/api/internal/endpoints.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ export const pictureOf = (guid: GUID) => `${origin}/picture/${guid}`;
366366
*/
367367
export const picture = `${origin}/picture`;
368368

369+
export const adminLogin = `${origin}/admin/login`;
370+
369371
export default {
370372
user,
371373
me,
@@ -395,4 +397,5 @@ export default {
395397
coursesMineOverlaps,
396398
pictureOf,
397399
picture,
400+
adminLogin,
398401
};

web/app/admin/login/page.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useRouter } from "next/navigation";
44
import { useState } from "react";
5+
import { adminLogin } from "../../../api/admin/login/route";
56

67
export default function LoginPage() {
78
const [name, setName] = useState("");
@@ -11,24 +12,20 @@ export default function LoginPage() {
1112
const handleSubmit = async (e: { preventDefault: () => void }) => {
1213
e.preventDefault();
1314

14-
const res = await fetch("/admin/login", {
15-
method: "POST",
16-
headers: { "Content-Type": "application/json" },
17-
body: JSON.stringify({ name, password }),
18-
});
19-
20-
if (res.ok) {
15+
try {
16+
await adminLogin(name, password);
17+
alert("成功しました。遷移します");
2118
router.replace("/admin");
22-
} else {
23-
alert("Invalid credentials");
19+
} catch (e) {
20+
alert("ログインに失敗しました");
2421
}
2522
};
2623

2724
return (
28-
<div className="flex min-h-screen items-center justify-center bg-gray-100">
25+
<div className="flex min-h-screen items-center justify-center">
2926
<div className="w-full max-w-md space-y-6 rounded-lg bg-white p-8 shadow-md">
3027
<h2 className="text-center font-bold text-2xl text-gray-700">
31-
管理者画面
28+
管理者画面 ログインページ
3229
</h2>
3330
<form onSubmit={handleSubmit} className="space-y-4">
3431
<div className="form-control">

web/app/admin/page.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import { useEffect } from "react";
66
export default function AdminPage() {
77
const router = useRouter();
88

9-
useEffect(() => {
10-
const checkAuth = async () => {
11-
const res = await fetch("/admin/validate");
12-
if (!res.ok) {
13-
router.replace("/admin/login");
14-
}
15-
};
16-
17-
checkAuth();
18-
}, [router]);
9+
// useEffect(() => {
10+
// const checkAuth = async () => {
11+
// console.log("こんにちはあああ")
12+
// const res = await fetch("/admin/validate");
13+
// if (!res.ok) {
14+
// router.replace("/admin/login");
15+
// }
16+
// };
17+
// checkAuth();
18+
// }, [router]);
1919

2020
return <div>Welcome to the Admin Dashboard</div>;
2121
}

0 commit comments

Comments
 (0)