🐛 Keep redirect in url when switching pages (#1810)

This commit is contained in:
Luke Vella 2025-07-11 11:42:28 +01:00 committed by GitHub
parent c2701a4d4f
commit 968e513dba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 4 deletions

View file

@ -0,0 +1,30 @@
"use client";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
export function LinkWithRedirectTo({
href,
className,
children,
}: {
href: string;
className?: string;
children?: React.ReactNode;
}) {
const searchParams = useSearchParams();
const redirectTo = searchParams.get("redirectTo");
return (
<Link
className={className}
href={
redirectTo
? `${href}?redirectTo=${encodeURIComponent(redirectTo)}`
: href
}
>
{children}
</Link>
);
}

View file

@ -1,4 +1,3 @@
import Link from "next/link";
import { Trans } from "react-i18next/TransWithoutContext";
import { GoogleProvider } from "@/auth/providers/google";
@ -15,6 +14,7 @@ import {
AuthPageHeader,
AuthPageTitle,
} from "../components/auth-page";
import { LinkWithRedirectTo } from "../components/link-with-redirect-to";
import { AuthErrors } from "./components/auth-errors";
import { LoginWithEmailForm } from "./components/login-email-form";
import { LoginWithOIDC } from "./components/login-with-oidc";
@ -96,7 +96,7 @@ export default async function LoginPage(props: {
i18nKey="loginFooter"
defaults="Don't have an account? <a>Sign up</a>"
components={{
a: <Link className="text-link" href="/register" />,
a: <LinkWithRedirectTo className="text-link" href="/register" />,
}}
/>
</AuthPageExternal>

View file

@ -1,4 +1,3 @@
import Link from "next/link";
import { Trans } from "react-i18next/TransWithoutContext";
import { getTranslation } from "@/i18n/server";
@ -13,6 +12,7 @@ import {
AuthPageHeader,
AuthPageTitle,
} from "../components/auth-page";
import { LinkWithRedirectTo } from "../components/link-with-redirect-to";
import { RegisterNameForm } from "./components/register-name-form";
export default async function Register(props: {
@ -56,7 +56,7 @@ export default async function Register(props: {
i18nKey="alreadyHaveAccount"
defaults="Already have an account? <a>Log in</a>"
components={{
a: <Link className="text-link" href="/login" />,
a: <LinkWithRedirectTo className="text-link" href="/login" />,
}}
/>
</AuthPageExternal>