diff --git a/client/pages/DiscordOAuthCallback.tsx b/client/pages/DiscordOAuthCallback.tsx index 22333cfc..5d41b933 100644 --- a/client/pages/DiscordOAuthCallback.tsx +++ b/client/pages/DiscordOAuthCallback.tsx @@ -1,154 +1,44 @@ -import { useEffect, useState } from "react"; - -const API_BASE = import.meta.env.VITE_API_BASE || ""; -import { useNavigate, useSearchParams } from "react-router-dom"; -import { useAuth } from "@/contexts/AuthContext"; +import { useEffect } from "react"; +import { useSearchParams } from "react-router-dom"; import Layout from "@/components/Layout"; import SEO from "@/components/SEO"; -import { Card, CardContent } from "@/components/ui/card"; -import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; -import { Loader, AlertTriangle, CheckCircle } from "lucide-react"; +import LoadingScreen from "@/components/LoadingScreen"; export default function DiscordOAuthCallback() { - const navigate = useNavigate(); const [searchParams] = useSearchParams(); - const { user, signIn } = useAuth(); - - const [status, setStatus] = useState<"loading" | "success" | "error">( - "loading", - ); - const [message, setMessage] = useState("Connecting to Discord..."); useEffect(() => { - const handleCallback = async () => { - const code = searchParams.get("code"); - const state = searchParams.get("state"); - const error = searchParams.get("error"); + const code = searchParams.get("code"); + const state = searchParams.get("state"); + const error = searchParams.get("error"); - if (error) { - setStatus("error"); - setMessage(`Discord authorization failed: ${error}`); - return; - } + if (error) { + window.location.href = `/login?error=${error}`; + return; + } - if (!code) { - setStatus("error"); - setMessage("No authorization code received from Discord"); - return; - } + if (!code) { + window.location.href = "/login?error=no_code"; + return; + } - try { - setMessage("Processing authentication..."); + const params = new URLSearchParams({ + code, + state: state || "/dashboard", + ...(error && { error }), + }); - // Call backend to handle OAuth exchange - const response = await fetch(`${API_BASE}/api/discord/oauth/callback`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - code, - state: state || "/dashboard", - }), - credentials: "include", // Include cookies in request - }); - - const data = await response.json(); - - if (!response.ok) { - setStatus("error"); - setMessage(data.message || "Failed to authenticate with Discord"); - return; - } - - // Success - setStatus("success"); - setMessage(data.message || "Successfully authenticated!"); - - // Save session tokens to localStorage if provided - if (data.session?.access_token) { - localStorage.setItem("sb-access-token", data.session.access_token); - localStorage.setItem("sb-refresh-token", data.session.refresh_token); - } - - // Redirect to next page - const nextPath = - state && state.startsWith("/") - ? state - : data.isNewUser - ? "/onboarding" - : "/dashboard"; - setTimeout(() => { - navigate(nextPath); - window.location.reload(); // Reload to pick up new auth context - }, 1500); - } catch (error) { - setStatus("error"); - setMessage( - error instanceof Error - ? error.message - : "An unexpected error occurred", - ); - } - }; - - handleCallback(); - }, [searchParams, navigate]); + window.location.href = `/api/discord/oauth/callback?${params.toString()}`; + }, [searchParams]); return ( - -
- - - {status === "loading" && ( -
- -
-

{message}

-

- Please wait while we secure your account... -

-
-
- )} - - {status === "success" && ( -
- -
-

{message}

-

- You'll be taken to your dashboard shortly... -

-
-
- )} - - {status === "error" && ( -
-
- -
-

- Authentication Failed -

-

{message}

-
-
- -
- -
-
- )} -
-
-
+
); }