From 223850922ebc0d244ac38647f2fd73441b938bda Mon Sep 17 00:00:00 2001 From: virtus Date: Fri, 24 Apr 2026 02:08:33 +0700 Subject: [PATCH] Add Google Client ID fetching in auth context and create auth config API route --- app/api/auth/config/route.ts | 16 ++++++++++++++++ docker-compose.yml | 3 +++ lib/auth-context.tsx | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 app/api/auth/config/route.ts diff --git a/app/api/auth/config/route.ts b/app/api/auth/config/route.ts new file mode 100644 index 0000000..11043f1 --- /dev/null +++ b/app/api/auth/config/route.ts @@ -0,0 +1,16 @@ +import { NextResponse } from "next/server" + +export const runtime = "nodejs" +export const dynamic = "force-dynamic" + +export async function GET() { + const googleClientId = + (process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || process.env.GOOGLE_CLIENT_ID || "").trim() + + return NextResponse.json( + { + googleClientId, + }, + { status: 200 }, + ) +} diff --git a/docker-compose.yml b/docker-compose.yml index 7ebc4fa..ca910db 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,9 @@ version: '3.8' services: web: + build: + context: . + dockerfile: Dockerfile image: fevirtus/reader:v0.0.1 container_name: reader-web ports: diff --git a/lib/auth-context.tsx b/lib/auth-context.tsx index d9b84c0..d4f5aa1 100644 --- a/lib/auth-context.tsx +++ b/lib/auth-context.tsx @@ -77,6 +77,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { export function useAuth() { const [sessionUser, setSessionUser] = useState(null) const [isLoading, setIsLoading] = useState(true) + const [googleClientId, setGoogleClientId] = useState("") const fetchSession = useCallback(async () => { try { @@ -100,6 +101,34 @@ export function useAuth() { fetchSession() }, [fetchSession]) + useEffect(() => { + let active = true + + const fetchAuthConfig = async () => { + try { + const res = await fetch("/api/auth/config", { cache: "no-store" }) + if (!res.ok) { + return + } + + const data = await res.json() + if (active) { + setGoogleClientId(String(data?.googleClientId || "").trim()) + } + } catch { + if (active) { + setGoogleClientId("") + } + } + } + + void fetchAuthConfig() + + return () => { + active = false + } + }, []) + const user: User | null = useMemo(() => { if (!sessionUser) return null return { @@ -114,9 +143,9 @@ export function useAuth() { }, [sessionUser]) const loginWithGoogle = async () => { - const clientId = (process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "").trim() + const clientId = googleClientId.trim() if (!clientId) { - throw new Error("NEXT_PUBLIC_GOOGLE_CLIENT_ID is not configured") + throw new Error("Google client id is not configured on server runtime") } const googleIdToken = await requestGoogleIdToken(clientId)