diff --git a/src/components/apis/authApiClients.tsx b/src/components/apis/authApiClients.tsx index 083bf1e..71c8394 100644 --- a/src/components/apis/authApiClients.tsx +++ b/src/components/apis/authApiClients.tsx @@ -131,7 +131,8 @@ const addTokenRefreshInterceptor = (client: typeof authApiClient) => { safeLocalStorage.removeItem('codenuk_user'); // Prevent redirect loops by checking current location if (typeof window !== 'undefined' && !window.location.pathname.includes('/signin')) { - window.location.href = '/signin?error=Please sign in to continue.'; + const returnUrl = encodeURIComponent(window.location.pathname + window.location.search) + window.location.href = `/signin?error=Please sign in to continue.&returnUrl=${returnUrl}`; } return Promise.reject(error); } catch (refreshError) { @@ -140,7 +141,8 @@ const addTokenRefreshInterceptor = (client: typeof authApiClient) => { safeLocalStorage.removeItem('codenuk_user'); // Prevent redirect loops by checking current location if (typeof window !== 'undefined' && !window.location.pathname.includes('/signin')) { - window.location.href = '/signin?error=Session expired. Please sign in again.'; + const returnUrl = encodeURIComponent(window.location.pathname + window.location.search) + window.location.href = `/signin?error=Session expired. Please sign in again.&returnUrl=${returnUrl}`; } return Promise.reject(refreshError); } diff --git a/src/components/auth/signin-form.tsx b/src/components/auth/signin-form.tsx index b49b6fa..b689790 100644 --- a/src/components/auth/signin-form.tsx +++ b/src/components/auth/signin-form.tsx @@ -27,6 +27,16 @@ export function SignInForm({ }: SignInFormProps) { const router = useRouter() const { setUserFromApi } = useAuth() + + // Get return URL from query parameters + const getReturnUrl = () => { + if (typeof window !== 'undefined') { + const urlParams = new URLSearchParams(window.location.search) + const returnUrl = urlParams.get('returnUrl') + return returnUrl ? decodeURIComponent(returnUrl) : null + } + return null + } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() @@ -48,9 +58,12 @@ export function SignInForm({ }: SignInFormProps) { // Persist for refresh localStorage.setItem("codenuk_user", JSON.stringify(response.data.user)) - // Redirect based on user role with error handling + // Redirect based on return URL or user role try { - if (response.data.user.role === 'admin') { + const returnUrl = getReturnUrl() + if (returnUrl && returnUrl !== '/signin' && returnUrl !== '/signup') { + router.push(returnUrl) + } else if (response.data.user.role === 'admin') { router.push("/admin") } else { router.push("/") @@ -58,7 +71,12 @@ export function SignInForm({ }: SignInFormProps) { } catch (redirectError) { console.error('Redirect failed:', redirectError) // Fallback redirect - window.location.href = response.data.user.role === 'admin' ? '/admin' : '/' + const returnUrl = getReturnUrl() + if (returnUrl && returnUrl !== '/signin' && returnUrl !== '/signup') { + window.location.href = returnUrl + } else { + window.location.href = response.data.user.role === 'admin' ? '/admin' : '/' + } } } else { setError("Invalid response from server. Please try again.") diff --git a/src/components/auth/signup-form.tsx b/src/components/auth/signup-form.tsx index 7882788..2aaa38c 100644 --- a/src/components/auth/signup-form.tsx +++ b/src/components/auth/signup-form.tsx @@ -73,12 +73,16 @@ export function SignUpForm({ onSignUpSuccess }: SignUpFormProps) { // Default behavior - redirect to signin with message if (typeof window !== 'undefined') { const message = encodeURIComponent("Account created successfully! Please check your email to verify your account.") + // Preserve return URL if it exists + const urlParams = new URLSearchParams(window.location.search) + const returnUrl = urlParams.get('returnUrl') + const redirectUrl = returnUrl ? `/signin?message=${message}&returnUrl=${returnUrl}` : `/signin?message=${message}` try { - router.push(`/signin?message=${message}`) + router.push(redirectUrl) } catch (redirectError) { console.error('Signup form redirect failed:', redirectError) // Fallback redirect - window.location.href = `/signin?message=${message}` + window.location.href = redirectUrl } } } diff --git a/src/components/auth/signup-page.tsx b/src/components/auth/signup-page.tsx index b03b2f6..db116e0 100644 --- a/src/components/auth/signup-page.tsx +++ b/src/components/auth/signup-page.tsx @@ -16,12 +16,16 @@ export function SignUpPage() { setTimeout(() => { if (typeof window !== 'undefined') { const message = encodeURIComponent("Please check your email to verify your account") + // Preserve return URL if it exists + const urlParams = new URLSearchParams(window.location.search) + const returnUrl = urlParams.get('returnUrl') + const redirectUrl = returnUrl ? `/signin?message=${message}&returnUrl=${returnUrl}` : `/signin?message=${message}` try { - router.push(`/signin?message=${message}`) + router.push(redirectUrl) } catch (redirectError) { console.error('Signup redirect failed:', redirectError) // Fallback redirect - window.location.href = `/signin?message=${message}` + window.location.href = redirectUrl } } }, 3000) diff --git a/src/components/main-dashboard.tsx b/src/components/main-dashboard.tsx index ecdf34f..ae4fae2 100644 --- a/src/components/main-dashboard.tsx +++ b/src/components/main-dashboard.tsx @@ -339,7 +339,10 @@ function TemplateSelectionStep({ onNext }: { onNext: (template: Template) => voi
{error}