diff --git a/pkg/errors.tsx b/pkg/errors.tsx index beedd59..bae2856 100644 --- a/pkg/errors.tsx +++ b/pkg/errors.tsx @@ -3,6 +3,15 @@ import { NextRouter } from "next/router" import { Dispatch, SetStateAction } from "react" import { toast } from "react-toastify" +function handleAal2Error(jsonResponse: any, router: NextRouter,) { + if (jsonResponse.error?.id === "session_aal2_required") { + window.location.href = "/auth/login?aal=aal2" + } + else { + router.push("/welcome") + } +} + // A small function to help us deal with errors coming from fetching a flow. export function handleGetFlowError( router: NextRouter, @@ -16,8 +25,10 @@ export function handleGetFlowError( window.location.href = (err.response?.data as any).redirect_browser_to return case "session_already_available": - // User is already signed in, let's redirect them home! - await router.push("/welcome") + await fetch('/.ory/kratos/public/sessions/whoami') + .then(response => response.json()) + .then((jsonResponse) => handleAal2Error(jsonResponse, router)) + .catch(() => { router.push("/welcome") }); return case "session_refresh_required": // We need to re-authenticate to perform this action