import { Button } from "@/shared/libs/shadcn/ui/button"; import { DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/shared/libs/shadcn/ui/dialog"; import { Input } from "@/shared/libs/shadcn/ui/input"; import { Label } from "@/shared/libs/shadcn/ui/label"; import { Separator } from "@/shared/libs/shadcn/ui/separator"; import { useCallback, useEffect, useState } from "react"; import { getAllThirdPartyAuth, GetALlThirdPartyAuthCallback, } from "../actions/getAllThirdPartyAuth"; import { Icon } from "@iconify/react"; import { Spinner } from "@/shared/libs/shadcn/ui/spinner"; import { getOauthEndpoint } from "../actions/getOauthEndpoint"; import { toast } from "sonner"; const SignInCard = () => { const [isLoading, setIsLoading] = useState(false); const [oAuthProviders, setOAuthProviders] = useState(null); // Fetch available third-party auth providers on component mount useEffect(() => { (async () => { const res = await getAllThirdPartyAuth(); setOAuthProviders(res); })(); }, []); // Open OAuth endpoint in a new popup window const getOauthEndpointUrl = async ( providerReqEndpoint: string, providerName: string, ) => { const res = await getOauthEndpoint({ endpointUrl: providerReqEndpoint, providerName: providerName, }); setIsLoading(true); window.open(res.data?.endpointUrl, "oauthPopup", "width=500,height=600"); }; // Handle the feedback from popup window for OAuth const handleMessage = useCallback((event: MessageEvent) => { if (event.origin !== window.location.origin) return; if (event.data.type === "oauth-success") { toast.success("Authentication successful! Redirecting...", { description: event.data.message, richColors: true, }); window.location.reload(); } if (event.data.type === "oauth-failed") { toast.error("Authentication failed.", { description: event.data.message || "Please try again.", duration: 5000, richColors: true, }); setIsLoading(false); } }, []); useEffect(() => { window.addEventListener("message", handleMessage); return () => { window.removeEventListener("message", handleMessage); }; }, [handleMessage]); return ( Get Started Sign in to access personalized features and stay updated with the latest content.

or continue with

{oAuthProviders ? (
{oAuthProviders.data?.map((provider, index) => ( ))}
) : ( )}
); }; export default SignInCard;