All files / src/pages Callback.tsx

0% Statements 0/21
0% Branches 0/6
0% Functions 0/4
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                   
import React, { useEffect, useState } from "react";
import { useNavigate, useLocation } from "react-router-dom";
import { useAuth0 } from "@auth0/auth0-react";
 
interface CallbackResult {
  appState?: {
    returnTo?: string;
  };
}
 
const CallbackPage: React.FC = () => {
  const { handleRedirectCallback, user, getAccessTokenSilently } = useAuth0();
  const navigate = useNavigate();
  const location = useLocation();
 
  const [loading, setLoading] = useState(true);
 
  useEffect(() => {
    const processAuth = async () => {
      try {
        // Process the Auth0 redirect callback
        const result: CallbackResult = await handleRedirectCallback();
 
        // Optional: get access token silently
        const token = await getAccessTokenSilently();
 
 
        // Navigate to the original page or default "/"
        const returnTo = result.appState?.returnTo || "/";
        void navigate(returnTo, { replace: true });
      } catch (err) {
        console.error("Error handling Auth0 callback:", err);
        void navigate("/error", { replace: true });
      } finally {
        setLoading(false);
      }
    };
 
    processAuth().catch((err) => {
      console.error("Auth error:", err);
      void navigate("/error", { replace: true });
    });
  }, [handleRedirectCallback, getAccessTokenSilently, navigate, user]);
 
  if (loading) {
    return <div>Logging in...</div>;
  }
 
  return (
    <div>
      <h1>Welcome, {user?.name ?? "User"}</h1>
      <p>Redirecting...</p>
    </div>
  );
};
 
export default CallbackPage;