From 3b3b4f89ba3ec70de3f0e777aadbe30cedddf7b5 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 25 Sep 2025 22:06:18 +0100 Subject: [PATCH] feat: redirect to next param on authentication --- apps/web/src/components/AuthForm.tsx | 14 +++++++++----- apps/web/src/views/auth/login/index.tsx | 10 ++++++++-- apps/web/src/views/auth/signup/index.tsx | 8 ++++++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/apps/web/src/components/AuthForm.tsx b/apps/web/src/components/AuthForm.tsx index ca426b55..5d826e94 100644 --- a/apps/web/src/components/AuthForm.tsx +++ b/apps/web/src/components/AuthForm.tsx @@ -1,4 +1,5 @@ import type { SocialProvider } from "better-auth/social-providers"; +import { useSearchParams } from "next/navigation"; import { zodResolver } from "@hookform/resolvers/zod"; import { t } from "@lingui/core/macro"; import { Trans } from "@lingui/react/macro"; @@ -160,6 +161,9 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) { const { showPopup } = usePopup(); const oidcProviderName = "OIDC"; + const redirect = useSearchParams().get("next"); + const callbackURL = redirect ?? "/boards"; + // Safely get environment variables on client side to avoid hydration mismatch useEffect(() => { const credentialsAllowed = @@ -195,7 +199,7 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) { name, email, password, - callbackURL: "/boards", + callbackURL, }, { onSuccess: () => @@ -212,7 +216,7 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) { { email, password, - callbackURL: "/boards", + callbackURL, }, { onSuccess: () => @@ -229,7 +233,7 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) { await authClient.signIn.magicLink( { email, - callbackURL: "/boards", + callbackURL, }, { onSuccess: () => setIsMagicLinkSent(true, email), @@ -250,14 +254,14 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) { // Use oauth2 signin for OIDC provider const result = await authClient.signIn.oauth2({ providerId: "oidc", - callbackURL: "/boards", + callbackURL, }); error = result.error; } else { // Use social signin for traditional social providers const result = await authClient.signIn.social({ provider, - callbackURL: "/boards", + callbackURL, }); error = result.error; } diff --git a/apps/web/src/views/auth/login/index.tsx b/apps/web/src/views/auth/login/index.tsx index fbfd1361..19aa2cc9 100644 --- a/apps/web/src/views/auth/login/index.tsx +++ b/apps/web/src/views/auth/login/index.tsx @@ -1,5 +1,5 @@ import Link from "next/link"; -import { useRouter } from "next/navigation"; +import { useRouter, useSearchParams } from "next/navigation"; import { t } from "@lingui/core/macro"; import { Trans } from "@lingui/react/macro"; import { env } from "next-runtime-env"; @@ -17,6 +17,8 @@ export default function LoginPage() { const [isMagicLinkSent, setIsMagicLinkSent] = useState(false); const [magicLinkRecipient, setMagicLinkRecipient] = useState(""); + const redirect = useSearchParams().get("next"); + const handleMagicLinkSent = (value: boolean, recipient: string) => { setIsMagicLinkSent(value); setMagicLinkRecipient(recipient); @@ -61,7 +63,11 @@ export default function LoginPage() { Don't have an account?{" "} - Sign up + + Sign up +

diff --git a/apps/web/src/views/auth/signup/index.tsx b/apps/web/src/views/auth/signup/index.tsx index 56d3b4bc..42486465 100644 --- a/apps/web/src/views/auth/signup/index.tsx +++ b/apps/web/src/views/auth/signup/index.tsx @@ -1,5 +1,5 @@ import Link from "next/link"; -import { useRouter } from "next/navigation"; +import { useRouter, useSearchParams } from "next/navigation"; import { t } from "@lingui/core/macro"; import { Trans } from "@lingui/react/macro"; import { env } from "next-runtime-env"; @@ -17,6 +17,8 @@ export default function SignUpPage() { const [isMagicLinkSent, setIsMagicLinkSent] = useState(false); const [magicLinkRecipient, setMagicLinkRecipient] = useState(""); + const redirect = useSearchParams().get("next"); + const { data } = authClient.useSession(); if (data?.user.id) router.push("/boards"); @@ -86,7 +88,9 @@ export default function SignUpPage() { Already have an account?{" "} - Sign in + + Sign in +