playreel/frontend/components/auth-gate.tsx
2026-09-15 16:00:54 +09:00

43 lines
1.5 KiB
TypeScript

"use client";
/** 로그인하지 않았으면 화면 대신 로그인만 보여준다 */
import { useState } from "react";
import { usePathname } from "next/navigation";
import GoogleSignIn from "@/components/google-sign-in";
import { signIn, useMe } from "@/lib/auth";
// 완성본 구경은 로그인 없이 연다. 백엔드의 /api/archive 와 같은 규칙
const OPEN_PATHS = ["/archive", "/playreel/archive"];
export default function AuthGate({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
const { me, setMe } = useMe();
const [error, setError] = useState<string | null>(null);
if (OPEN_PATHS.some((open) => pathname === open || pathname.startsWith(`${open}/`))) {
return <>{children}</>;
}
if (me === null) {
return <p style={{ color: "var(--color-text-gray-400)" }}>확인하는 중…</p>;
}
if (me === false) {
return (
<div style={{ display: "flex", flexDirection: "column", alignItems: "center",
gap: "1.5rem", paddingTop: "6rem" }}>
<h1 className="page-title">로그인이 필요합니다</h1>
<p className="page-subtitle">구글 계정으로 들어와 주세요.</p>
<GoogleSignIn onCredential={(credential) => {
setError(null);
signIn(credential).then(setMe).catch((failure) => setError((failure as Error).message));
}} />
{error && <p style={{ color: "#ff7a7a", fontSize: "var(--text-sm)" }}>{error}</p>}
</div>
);
}
return <>{children}</>;
}