From 257feba5bd748c5a935d6dad949f3e305889dc40 Mon Sep 17 00:00:00 2001 From: javayhu Date: Fri, 11 Apr 2025 12:41:50 +0800 Subject: [PATCH] fix: redirect to localized dashboard url after login - Updated login-form.tsx, register-form.tsx, and social-login-button.tsx to utilize useLocale for dynamic callback URL generation based on the user's locale. - Adjusted the default callback URL to include the locale, improving user experience and localization support in authentication flows. --- src/components/auth/login-form.tsx | 6 ++++-- src/components/auth/register-form.tsx | 7 +++++-- src/components/auth/social-login-button.tsx | 7 +++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/components/auth/login-form.tsx b/src/components/auth/login-form.tsx index adf53c1..cb40afb 100644 --- a/src/components/auth/login-form.tsx +++ b/src/components/auth/login-form.tsx @@ -20,7 +20,7 @@ import { cn } from '@/lib/utils'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; import { zodResolver } from '@hookform/resolvers/zod'; import { EyeIcon, EyeOffIcon } from 'lucide-react'; -import { useTranslations } from 'next-intl'; +import { useTranslations, useLocale } from 'next-intl'; import { useSearchParams } from 'next/navigation'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; @@ -38,7 +38,9 @@ export const LoginForm = ({ className, callbackUrl: propCallbackUrl }: LoginForm const urlError = searchParams.get('error'); const paramCallbackUrl = searchParams.get('callbackUrl'); // Use prop callback URL or param callback URL if provided, otherwise use the default login redirect - const callbackUrl = propCallbackUrl || paramCallbackUrl || DEFAULT_LOGIN_REDIRECT; + const locale = useLocale(); + const defaultCallbackUrl = `/${locale}${DEFAULT_LOGIN_REDIRECT}`; + const callbackUrl = propCallbackUrl || paramCallbackUrl || defaultCallbackUrl; console.log('login form, callbackUrl', callbackUrl); const [error, setError] = useState(''); diff --git a/src/components/auth/register-form.tsx b/src/components/auth/register-form.tsx index 8d6c709..d7ed831 100644 --- a/src/components/auth/register-form.tsx +++ b/src/components/auth/register-form.tsx @@ -18,7 +18,7 @@ import { authClient } from '@/lib/auth-client'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; import { zodResolver } from '@hookform/resolvers/zod'; import { EyeIcon, EyeOffIcon } from 'lucide-react'; -import { useTranslations } from 'next-intl'; +import { useTranslations, useLocale } from 'next-intl'; import { useSearchParams } from 'next/navigation'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; @@ -34,8 +34,11 @@ export const RegisterForm = ({ callbackUrl: propCallbackUrl }: RegisterFormProps const searchParams = useSearchParams(); const paramCallbackUrl = searchParams.get('callbackUrl'); // Use prop callback URL or param callback URL if provided, otherwise use the default login redirect - const callbackUrl = propCallbackUrl || paramCallbackUrl || DEFAULT_LOGIN_REDIRECT; + const locale = useLocale(); + const defaultCallbackUrl = `/${locale}${DEFAULT_LOGIN_REDIRECT}`; + const callbackUrl = propCallbackUrl || paramCallbackUrl || defaultCallbackUrl; console.log('register form, callbackUrl', callbackUrl); + const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const [isPending, setIsPending] = useState(false); diff --git a/src/components/auth/social-login-button.tsx b/src/components/auth/social-login-button.tsx index cdbf4a5..e855c40 100644 --- a/src/components/auth/social-login-button.tsx +++ b/src/components/auth/social-login-button.tsx @@ -8,7 +8,7 @@ import { GitHubIcon } from '@/components/icons/github'; import { GoogleIcon } from '@/components/icons/google'; import { authClient } from '@/lib/auth-client'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; -import { useTranslations } from 'next-intl'; +import { useLocale, useTranslations } from 'next-intl'; import { DividerWithText } from '@/components/auth/divider-with-text'; interface SocialLoginButtonProps { @@ -22,7 +22,10 @@ export const SocialLoginButton = ({ callbackUrl: propCallbackUrl }: SocialLoginB const t = useTranslations('AuthPage.login'); const searchParams = useSearchParams(); const paramCallbackUrl = searchParams.get('callbackUrl'); - const callbackUrl = propCallbackUrl || paramCallbackUrl || DEFAULT_LOGIN_REDIRECT; + // Use prop callback URL or param callback URL if provided, otherwise use the default login redirect + const locale = useLocale(); + const defaultCallbackUrl = `/${locale}${DEFAULT_LOGIN_REDIRECT}`; + const callbackUrl = propCallbackUrl || paramCallbackUrl || defaultCallbackUrl; const [isLoading, setIsLoading] = useState<'google' | 'github' | null>(null); console.log('social login button, callbackUrl', callbackUrl);