import React, { useEffect } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import { setStoredAuth } from "hse-auth";

export default function AuthCallback() {
  const navigate = useNavigate();
  const [params] = useSearchParams();

  useEffect(() => {
    const token = params.get("token") || "";
    const success = params.get("success");
    if (token && success !== "false") {
      setStoredAuth({ token }, { scope: "master" });
      navigate("/", { replace: true });
      return;
    }
    navigate("/login", { replace: true });
  }, [navigate, params]);

  return <div className="master-loading">Signing you in...</div>;
}
