diff --git a/app/auth/callback/page.tsx b/app/auth/callback/page.tsx index 2ad7fbcb..8577fb6c 100644 --- a/app/auth/callback/page.tsx +++ b/app/auth/callback/page.tsx @@ -43,12 +43,12 @@ function OAuthCallbackContent() { try { await profileService.getProfile(session.user.id); console.log('Profile created via profileService, redirecting to complete profile'); - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } catch (profileError) { console.error('Error creating profile:', profileError); // Continue to complete profile page anyway - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } } @@ -119,12 +119,12 @@ function OAuthCallbackContent() { try { await profileService.getProfile(retrySession.user.id); console.log('Profile created via profileService on retry, redirecting to complete profile'); - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } catch (profileError) { console.error('Error creating profile on retry:', profileError); // Continue to complete profile page anyway - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } } @@ -137,13 +137,13 @@ function OAuthCallbackContent() { if (!isProfileComplete) { console.log('Profile incomplete on retry, redirecting to complete profile'); - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } } catch (profileError) { console.error('Error checking profile for OAuth user on retry:', profileError); // If there's an error, redirect to complete profile to be safe - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } @@ -170,12 +170,12 @@ function OAuthCallbackContent() { try { await profileService.getProfile(finalSession.user.id); console.log('Profile created via profileService on final try, redirecting to complete profile'); - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } catch (profileError) { console.error('Error creating profile on final try:', profileError); // Continue to complete profile page anyway - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } } @@ -188,13 +188,13 @@ function OAuthCallbackContent() { if (!isProfileComplete) { console.log('Profile incomplete on final try, redirecting to complete profile'); - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } } catch (profileError) { console.error('Error checking profile for OAuth user on final try:', profileError); // If there's an error, redirect to complete profile to be safe - router.replace('/complete-profile'); + router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`); return; } diff --git a/app/complete-profile/page.tsx b/app/complete-profile/page.tsx index ded953c4..9ebe8c24 100644 --- a/app/complete-profile/page.tsx +++ b/app/complete-profile/page.tsx @@ -1,7 +1,7 @@ 'use client'; import { useState, useEffect, useCallback, useRef } from 'react'; -import { useRouter } from 'next/navigation'; +import { useRouter, useSearchParams } from 'next/navigation'; import Link from 'next/link'; import { createClient } from '@/lib/supabase/client'; import { toast } from 'sonner'; @@ -23,6 +23,10 @@ interface User { } export default function CompleteProfile() { + const router = useRouter(); + const searchParams = useSearchParams(); + const returnUrl = searchParams.get('returnUrl') || '/protected/dashboard'; + const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [username, setUsername] = useState(''); @@ -32,7 +36,6 @@ export default function CompleteProfile() { const [usernameError, setUsernameError] = useState(''); const [user, setUser] = useState(null); const [isValidating, setIsValidating] = useState(true); - const router = useRouter(); const usernameCheckTimeout = useRef | null>(null); const getSupabaseClient = () => { @@ -60,7 +63,7 @@ export default function CompleteProfile() { if (isProfileComplete) { // Profile is already complete, redirect to dashboard - router.push('/protected/dashboard'); + router.push(returnUrl); return; } @@ -223,7 +226,7 @@ export default function CompleteProfile() { } toast.success('Profile completed successfully! Welcome to CodeUnia! 🎉'); - router.push('/protected/dashboard'); + router.push(returnUrl); } catch (error) { console.error('Error updating profile:', error); toast.error('Error completing profile setup');