From 3d566086003efd952cd1535ee9d971dab5d58a15 Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Thu, 1 Aug 2024 22:43:27 +0900
Subject: refactor(frontend): simplify login check

---
 frontend/app/routes/admin.games_.$gameId.tsx | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

(limited to 'frontend/app/routes/admin.games_.$gameId.tsx')

diff --git a/frontend/app/routes/admin.games_.$gameId.tsx b/frontend/app/routes/admin.games_.$gameId.tsx
index 34860ab..0d2cac6 100644
--- a/frontend/app/routes/admin.games_.$gameId.tsx
+++ b/frontend/app/routes/admin.games_.$gameId.tsx
@@ -5,7 +5,7 @@ import type {
 } from "@remix-run/node";
 import { Form, useLoaderData } from "@remix-run/react";
 import { adminApiGetGame, adminApiPutGame } from "../.server/api/client";
-import { isAuthenticated } from "../.server/auth";
+import { ensureAdminUserLoggedIn } from "../.server/auth";
 
 export const meta: MetaFunction<typeof loader> = ({ data }) => {
 	return [
@@ -18,24 +18,14 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {
 };
 
 export async function loader({ request, params }: LoaderFunctionArgs) {
-	const { user, token } = await isAuthenticated(request, {
-		failureRedirect: "/login",
-	});
-	if (!user.is_admin) {
-		throw new Error("Unauthorized");
-	}
+	const { token } = await ensureAdminUserLoggedIn(request);
 	const { gameId } = params;
 	const { game } = await adminApiGetGame(token, Number(gameId));
 	return { game };
 }
 
 export async function action({ request, params }: ActionFunctionArgs) {
-	const { user, token } = await isAuthenticated(request, {
-		failureRedirect: "/login",
-	});
-	if (!user.is_admin) {
-		throw new Error("Unauthorized");
-	}
+	const { token } = await ensureAdminUserLoggedIn(request);
 	const { gameId } = params;
 
 	const formData = await request.formData();
-- 
cgit v1.2.3-70-g09d2

