All files / src/authentication PortalMismatchLogout.jsx

100% Statements 14/14
100% Branches 0/0
100% Functions 2/2
100% Lines 14/14

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                                12x 12x   12x 11x 11x 11x 11x 11x 11x 11x         11x   11x 11x     12x    
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { useAuth0 } from "@auth0/auth0-react";
import SimpleLoading from "@/utils/components/SimpleLoading";
import { logoutSession } from "@/authentication/session";
import {
    getAuthPortalIntent,
    clearAuthPortalIntent,
    AUTH_PORTAL_REJECTED_MSG_KEY,
    getPortalMismatchMessage,
} from "@/authentication/authPortalIntent";
 
/**
 * Ends the session when the account role does not match the portal used to start Auth0 login.
 */
export default function PortalMismatchLogout() {
    const { logout } = useAuth0();
    const navigate = useNavigate();
 
    useEffect(() => {
        const intent = getAuthPortalIntent();
        clearAuthPortalIntent();
        localStorage.removeItem("role");
        localStorage.removeItem("access_token");
        const message = getPortalMismatchMessage(intent);
        try {
            sessionStorage.setItem(AUTH_PORTAL_REJECTED_MSG_KEY, message);
        } catch {
            // ignore
        }
        // BFF (Phase 4): revoke the refresh token + clear cookies server-side.
        logoutSession();
        // Local logout via the Auth0 API (clears the local session, no hosted redirect); land on home in-app.
        logout({ openUrl: false });
        navigate("/", { replace: true });
    }, [logout, navigate]);
 
    return <SimpleLoading showLoading={true} />;
}