diff --git a/src/components/Authentication/Kratos/KratosLogin.tsx b/src/components/Authentication/Kratos/KratosLogin.tsx index 4102f281c8..2876d72008 100644 --- a/src/components/Authentication/Kratos/KratosLogin.tsx +++ b/src/components/Authentication/Kratos/KratosLogin.tsx @@ -4,19 +4,28 @@ import type { NextPage } from "next"; import Link from "next/link"; import Router, { useRouter } from "next/router"; import { useCallback, useEffect, useState } from "react"; -import { Flow, HandleError, useCreateLogoutHandler } from "../../ory"; +import { Flow, HandleError } from "../../ory"; import { SetUriFlow } from "../../ory/helpers"; import ory from "../../ory/sdk"; const Login: NextPage = () => { + const [returnTo, setReturnTo] = useState(); const [flow, setFlow] = useState(); const { query, push, isReady } = useRouter(); - const returnTo = String(query.return_to || ""); - console.log("Return to: " + returnTo); + const returnToFromQuery = (query.return_to as string) || ""; const flowId = String(query.flow || ""); + // If we have a return_to query parameter, we want to redirect the user to + // that URL after a successful login. if set, and return_to is empty, we don't + // overwrite it. + useEffect(() => { + if (returnToFromQuery && !returnTo) { + setReturnTo(returnToFromQuery); + } + }, [returnTo, returnToFromQuery]); + // Refresh means we want to refresh the session. This is needed, for example, when we want to update the password // of a user. const refresh = Boolean(query.refresh); @@ -54,16 +63,12 @@ const Login: NextPage = () => { }) .then(({ data }) => { setFlow(data); - SetUriFlow(Router, data.id); + SetUriFlow(Router, data.id, returnTo); }) .catch(handleError), [handleError] ); - // This might be confusing, but we want to show the user an option - // to sign out if they are performing two-factor authentication! - const onLogout = useCreateLogoutHandler([aal, refresh]); - useEffect(() => { if (!isReady) { return; @@ -71,13 +76,13 @@ const Login: NextPage = () => { if (flowId) { getFlow(flowId).catch(() => { - createFlow(refresh, aal, returnTo); + createFlow(refresh, aal, returnTo ?? "/"); }); return; } // Otherwise we initialize it - createFlow(refresh, aal, returnTo); + createFlow(refresh, aal, returnTo ?? "/"); // eslint-disable-next-line react-hooks/exhaustive-deps }, [isReady]); @@ -113,21 +118,13 @@ const Login: NextPage = () => {
- {aal || refresh ? ( -
-
- Log out +
+ +
+ Reset password
-
- ) : ( -
- -
- Reset password -
- -
- )} + +
); diff --git a/src/components/Authentication/Kratos/KratosLogoutButton.tsx b/src/components/Authentication/Kratos/KratosLogoutButton.tsx index 02463952fa..dc7984af58 100644 --- a/src/components/Authentication/Kratos/KratosLogoutButton.tsx +++ b/src/components/Authentication/Kratos/KratosLogoutButton.tsx @@ -1,12 +1,17 @@ +import { useLocation } from "react-router-dom"; import { useCreateLogoutHandler } from "../../ory"; export default function KratosLogoutButton() { + const { pathname, search } = useLocation(); + + const returnTo = pathname + search; + const onLogout = useCreateLogoutHandler([]); return (