Skip to content

Commit 9717e15

Browse files
committed
feat:gameに直接アクセスした際にログイン状況をチェック
1 parent 721bc98 commit 9717e15

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

typing-app/src/components/pages/Game.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
"use client";
22
import { ResultScore } from "@/types/RegisterScore";
33
import { VStack } from "@chakra-ui/react";
4-
import React, { useState } from "react";
4+
import React, { useState, useEffect } from "react";
55
import GamePre from "../templates/GamePre";
66
import GameResult from "../templates/GameResult";
77
import GameTyping from "../templates/GameTyping";
8+
import { useRouter } from "next/navigation";
9+
import { showWarningToast } from "@/utils/toast";
10+
import { User } from "@/types/user";
11+
import { getCurrentUser } from "@/app/actions";
812

913
export interface GamePreProps {
1014
nextPage: () => void;
@@ -22,6 +26,22 @@ interface GamePageProps {
2226
}
2327

2428
const GamePage: React.FC<GamePageProps> = ({ subjectText }) => {
29+
//ログインしていなければ、トップページにリダイレクト
30+
const router = useRouter();
31+
const isUserLoggedIn = async () => {
32+
const user: User | undefined = await getCurrentUser();
33+
return user;
34+
}
35+
36+
useEffect(() => {
37+
isUserLoggedIn().then((user) => {
38+
if (!user) {
39+
showWarningToast("ログインしてください");
40+
router.push("/");
41+
}
42+
});
43+
}, []);
44+
2545
const ScreenIndex = {
2646
IDX_PRE: 0,
2747
IDX_TYPING: 1,

0 commit comments

Comments
 (0)