%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/ui/
Upload File :
Create Path :
Current File : //home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/ui/GoogleAuthButton.tsx

import { useCallback, useEffect, useState } from "react";
import Button from "@/components/ui/Button";
import { apiRequest } from "@/lib/api";
import type {
  GoogleAuthConfigResponse,
  GoogleAuthUrlResponse,
} from "@/types/api";

/**
 * Tombol "Lanjutkan dengan Google".
 * - Otomatis mengecek /auth/google/config; disembunyikan jika OAuth belum
 *   dikonfigurasi di backend (env GOOGLE_* kosong).
 * - Mengarahkan browser ke URL Google (backend-initiated + PKCE).
 */
export default function GoogleAuthButton() {
  const [enabled, setEnabled] = useState(false);
  const [busy, setBusy] = useState(false);

  useEffect(() => {
    let cancelled = false;
    apiRequest<GoogleAuthConfigResponse>("/auth/google/config")
      .then((res) => {
        if (!cancelled) setEnabled(res.enabled);
      })
      .catch(() => {
        if (!cancelled) setEnabled(false);
      });
    return () => {
      cancelled = true;
    };
  }, []);

  const start = useCallback(async () => {
    if (busy) return;
    setBusy(true);
    try {
      const res = await apiRequest<GoogleAuthUrlResponse>("/auth/google/url");
      window.location.assign(res.url);
    } catch {
      // Redirect gagal — biarkan tombol bisa diklik ulang.
      setBusy(false);
    }
  }, [busy]);

  if (!enabled) return null;

  return (
    <>
      <div className="flex items-center gap-3">
        <span className="h-px flex-1 bg-white/[0.08]" />
        <span className="text-xs text-slate-500">atau</span>
        <span className="h-px flex-1 bg-white/[0.08]" />
      </div>
      <Button
        type="button"
        variant="ghost"
        size="lg"
        className="w-full"
        onClick={start}
        disabled={busy}
      >
        <svg viewBox="0 0 24 24" className="h-4 w-4" aria-hidden="true">
          <path
            fill="#4285F4"
            d="M23.49 12.27c0-.79-.07-1.54-.19-2.27H12v4.51h6.47c-.29 1.48-1.14 2.73-2.4 3.58v3h3.86c2.26-2.09 3.56-5.17 3.56-8.82z"
          />
          <path
            fill="#34A853"
            d="M12 24c3.24 0 5.95-1.08 7.93-2.91l-3.86-3c-1.08.72-2.45 1.16-4.07 1.16-3.13 0-5.78-2.11-6.73-4.96H1.29v3.09C3.26 21.3 7.31 24 12 24z"
          />
          <path
            fill="#FBBC05"
            d="M5.27 14.29c-.25-.72-.38-1.49-.38-2.29s.14-1.57.38-2.29V6.62H1.29C.47 8.24 0 10.06 0 12s.47 3.76 1.29 5.38l3.98-3.09z"
          />
          <path
            fill="#EA4335"
            d="M12 4.75c1.77 0 3.35.61 4.6 1.8l3.42-3.42C17.95 1.19 15.24 0 12 0 7.31 0 3.26 2.7 1.29 6.62l3.98 3.09C6.22 6.86 8.87 4.75 12 4.75z"
          />
        </svg>
        Lanjutkan dengan Google
      </Button>
    </>
  );
}

Zerion Mini Shell 1.0