From 794c18a7e6f63c63a2e0a324e36c9795a3dab28f Mon Sep 17 00:00:00 2001 From: javayhu Date: Tue, 2 Sep 2025 23:51:29 +0800 Subject: [PATCH] fix: fix localized callback url after login --- src/components/auth/login-form.tsx | 10 +++++----- src/components/auth/register-form.tsx | 10 +++++----- src/components/auth/social-login-button.tsx | 7 ++----- src/lib/urls/urls.ts | 2 +- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/components/auth/login-form.tsx b/src/components/auth/login-form.tsx index 8951a7c..aa75bde 100644 --- a/src/components/auth/login-form.tsx +++ b/src/components/auth/login-form.tsx @@ -17,7 +17,7 @@ import { Input } from '@/components/ui/input'; import { websiteConfig } from '@/config/website'; import { LocaleLink } from '@/i18n/navigation'; import { authClient } from '@/lib/auth-client'; -import { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls'; +import { getUrlWithLocale } from '@/lib/urls/urls'; import { cn } from '@/lib/utils'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; import { zodResolver } from '@hookform/resolvers/zod'; @@ -45,10 +45,10 @@ export const LoginForm = ({ const paramCallbackUrl = searchParams.get('callbackUrl'); // Use prop callback URL or param callback URL if provided, otherwise use the default login redirect const locale = useLocale(); - const defaultCallbackUrl = getUrlWithLocaleInCallbackUrl( - DEFAULT_LOGIN_REDIRECT, - locale - ); + const defaultCallbackUrl = getUrlWithLocale(DEFAULT_LOGIN_REDIRECT, locale); + // console.log('login form, propCallbackUrl', propCallbackUrl); + // console.log('login form, paramCallbackUrl', paramCallbackUrl); + // console.log('login form, defaultCallbackUrl', defaultCallbackUrl); const callbackUrl = propCallbackUrl || paramCallbackUrl || defaultCallbackUrl; console.log('login form, callbackUrl', callbackUrl); diff --git a/src/components/auth/register-form.tsx b/src/components/auth/register-form.tsx index d7b5d7a..5b9638e 100644 --- a/src/components/auth/register-form.tsx +++ b/src/components/auth/register-form.tsx @@ -16,7 +16,7 @@ import { import { Input } from '@/components/ui/input'; import { websiteConfig } from '@/config/website'; import { authClient } from '@/lib/auth-client'; -import { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls'; +import { getUrlWithLocale } from '@/lib/urls/urls'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; import { zodResolver } from '@hookform/resolvers/zod'; import { EyeIcon, EyeOffIcon, Loader2Icon } from 'lucide-react'; @@ -40,10 +40,10 @@ export const RegisterForm = ({ const paramCallbackUrl = searchParams.get('callbackUrl'); // Use prop callback URL or param callback URL if provided, otherwise use the default login redirect const locale = useLocale(); - const defaultCallbackUrl = getUrlWithLocaleInCallbackUrl( - DEFAULT_LOGIN_REDIRECT, - locale - ); + const defaultCallbackUrl = getUrlWithLocale(DEFAULT_LOGIN_REDIRECT, locale); + // console.log('register form, propCallbackUrl', propCallbackUrl); + // console.log('register form, paramCallbackUrl', paramCallbackUrl); + // console.log('register form, defaultCallbackUrl', defaultCallbackUrl); const callbackUrl = propCallbackUrl || paramCallbackUrl || defaultCallbackUrl; console.log('register form, callbackUrl', callbackUrl); diff --git a/src/components/auth/social-login-button.tsx b/src/components/auth/social-login-button.tsx index 5418b52..769f402 100644 --- a/src/components/auth/social-login-button.tsx +++ b/src/components/auth/social-login-button.tsx @@ -6,7 +6,7 @@ import { GoogleIcon } from '@/components/icons/google'; import { Button } from '@/components/ui/button'; import { websiteConfig } from '@/config/website'; import { authClient } from '@/lib/auth-client'; -import { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls'; +import { getUrlWithLocale } from '@/lib/urls/urls'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; import { Loader2Icon } from 'lucide-react'; import { useLocale, useTranslations } from 'next-intl'; @@ -37,10 +37,7 @@ export const SocialLoginButton = ({ const paramCallbackUrl = searchParams.get('callbackUrl'); // Use prop callback URL or param callback URL if provided, otherwise use the default login redirect const locale = useLocale(); - const defaultCallbackUrl = getUrlWithLocaleInCallbackUrl( - DEFAULT_LOGIN_REDIRECT, - locale - ); + const defaultCallbackUrl = getUrlWithLocale(DEFAULT_LOGIN_REDIRECT, locale); const callbackUrl = propCallbackUrl || paramCallbackUrl || defaultCallbackUrl; const [isLoading, setIsLoading] = useState<'google' | 'github' | null>(null); console.log('social login button, callbackUrl', callbackUrl); diff --git a/src/lib/urls/urls.ts b/src/lib/urls/urls.ts index 886e170..9dcbd7e 100644 --- a/src/lib/urls/urls.ts +++ b/src/lib/urls/urls.ts @@ -35,7 +35,7 @@ export function getUrlWithLocale(url: string, locale?: Locale | null): string { * Input: http://localhost:3000/api/auth/reset-password/token?callbackURL=/auth/reset-password * Output: http://localhost:3000/api/auth/reset-password/token?callbackURL=/zh/auth/reset-password * - * http://localhost:3000/api/auth/verify-email?token=eyJhbGciOiJIUzI1NiJ9&callbackURL=/dashboard + * Input: http://localhost:3000/api/auth/verify-email?token=eyJhbGciOiJIUzI1NiJ9&callbackURL=/dashboard * Output: http://localhost:3000/api/auth/verify-email?token=eyJhbGciOiJIUzI1NiJ9&callbackURL=/zh/dashboard * * @param url - The original URL with callbackURL parameter