Skip to content

Commit 97d81ad

Browse files
author
Deepak Pandey
committed
Fix OAuth redirect to preserve returnUrl through complete-profile flow
- Pass returnUrl parameter from OAuth callback to complete-profile page - Update complete-profile page to handle returnUrl parameter - Redirect users back to their original destination after profile completion - Ensures users land on the page they were trying to access before auth
1 parent 268a829 commit 97d81ad

2 files changed

Lines changed: 24 additions & 26 deletions

File tree

app/auth/callback/page.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ function OAuthCallbackContent() {
4343
try {
4444
await profileService.getProfile(session.user.id);
4545
console.log('Profile created via profileService, redirecting to complete profile');
46-
router.replace('/complete-profile');
46+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
4747
return;
4848
} catch (profileError) {
4949
console.error('Error creating profile:', profileError);
5050
// Continue to complete profile page anyway
51-
router.replace('/complete-profile');
51+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
5252
return;
5353
}
5454
}
@@ -119,12 +119,12 @@ function OAuthCallbackContent() {
119119
try {
120120
await profileService.getProfile(retrySession.user.id);
121121
console.log('Profile created via profileService on retry, redirecting to complete profile');
122-
router.replace('/complete-profile');
122+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
123123
return;
124124
} catch (profileError) {
125125
console.error('Error creating profile on retry:', profileError);
126126
// Continue to complete profile page anyway
127-
router.replace('/complete-profile');
127+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
128128
return;
129129
}
130130
}
@@ -137,13 +137,13 @@ function OAuthCallbackContent() {
137137

138138
if (!isProfileComplete) {
139139
console.log('Profile incomplete on retry, redirecting to complete profile');
140-
router.replace('/complete-profile');
140+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
141141
return;
142142
}
143143
} catch (profileError) {
144144
console.error('Error checking profile for OAuth user on retry:', profileError);
145145
// If there's an error, redirect to complete profile to be safe
146-
router.replace('/complete-profile');
146+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
147147
return;
148148
}
149149

@@ -170,12 +170,12 @@ function OAuthCallbackContent() {
170170
try {
171171
await profileService.getProfile(finalSession.user.id);
172172
console.log('Profile created via profileService on final try, redirecting to complete profile');
173-
router.replace('/complete-profile');
173+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
174174
return;
175175
} catch (profileError) {
176176
console.error('Error creating profile on final try:', profileError);
177177
// Continue to complete profile page anyway
178-
router.replace('/complete-profile');
178+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
179179
return;
180180
}
181181
}
@@ -188,13 +188,13 @@ function OAuthCallbackContent() {
188188

189189
if (!isProfileComplete) {
190190
console.log('Profile incomplete on final try, redirecting to complete profile');
191-
router.replace('/complete-profile');
191+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
192192
return;
193193
}
194194
} catch (profileError) {
195195
console.error('Error checking profile for OAuth user on final try:', profileError);
196196
// If there's an error, redirect to complete profile to be safe
197-
router.replace('/complete-profile');
197+
router.replace(`/complete-profile?returnUrl=${encodeURIComponent(returnUrl)}`);
198198
return;
199199
}
200200

app/complete-profile/page.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import { useState, useEffect, useCallback, useRef } from 'react';
4-
import { useRouter } from 'next/navigation';
4+
import { useRouter, useSearchParams } from 'next/navigation';
55
import Link from 'next/link';
66
import { createClient } from '@/lib/supabase/client';
77
import { toast } from 'sonner';
@@ -23,6 +23,10 @@ interface User {
2323
}
2424

2525
export default function CompleteProfile() {
26+
const router = useRouter();
27+
const searchParams = useSearchParams();
28+
const returnUrl = searchParams.get('returnUrl') || '/protected/dashboard';
29+
2630
const [firstName, setFirstName] = useState('');
2731
const [lastName, setLastName] = useState('');
2832
const [username, setUsername] = useState('');
@@ -32,7 +36,6 @@ export default function CompleteProfile() {
3236
const [usernameError, setUsernameError] = useState<string>('');
3337
const [user, setUser] = useState<User | null>(null);
3438
const [isValidating, setIsValidating] = useState(true);
35-
const router = useRouter();
3639
const usernameCheckTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
3740

3841
const getSupabaseClient = () => {
@@ -60,7 +63,7 @@ export default function CompleteProfile() {
6063

6164
if (isProfileComplete) {
6265
// Profile is already complete, redirect to dashboard
63-
router.push('/protected/dashboard');
66+
router.push(returnUrl);
6467
return;
6568
}
6669

@@ -139,20 +142,15 @@ export default function CompleteProfile() {
139142
.single();
140143

141144
if (error && error.code !== 'PGRST116') {
142-
console.error('Username check error:', error);
143-
// If there's a database error, assume username is available to allow form submission
144-
setUsernameAvailable(true);
145-
setUsernameError('');
146-
return;
145+
throw error;
147146
}
148147

149148
// If no data found, username is available
150149
setUsernameAvailable(!data);
151150
} catch (error) {
152151
console.error('Error checking username:', error);
153-
// On any error, assume username is available to allow form submission
154-
setUsernameAvailable(true);
155-
setUsernameError('');
152+
setUsernameAvailable(null);
153+
setUsernameError('Unable to check username availability');
156154
} finally {
157155
setIsCheckingUsername(false);
158156
}
@@ -191,7 +189,7 @@ export default function CompleteProfile() {
191189
return;
192190
}
193191

194-
if (usernameAvailable === false) {
192+
if (!usernameAvailable) {
195193
toast.error('Username is not available');
196194
return;
197195
}
@@ -228,7 +226,7 @@ export default function CompleteProfile() {
228226
}
229227

230228
toast.success('Profile completed successfully! Welcome to CodeUnia! 🎉');
231-
router.push('/protected/dashboard');
229+
router.push(returnUrl);
232230
} catch (error) {
233231
console.error('Error updating profile:', error);
234232
toast.error('Error completing profile setup');
@@ -280,7 +278,7 @@ export default function CompleteProfile() {
280278
Welcome! Let&apos;s set up your profile
281279
</h1>
282280
<p className="text-gray-600 leading-relaxed">
283-
Complete your profile to get started with Codeunia. This will only take a moment.
281+
Complete your profile to get started with CodeUnia. This will only take a moment.
284282
</p>
285283
</div>
286284

@@ -424,9 +422,9 @@ export default function CompleteProfile() {
424422
{/* Submit Button */}
425423
<button
426424
type="submit"
427-
disabled={isLoading || !firstName.trim() || !lastName.trim() || !username || usernameAvailable === false}
425+
disabled={isLoading || !firstName.trim() || !lastName.trim() || !username || !usernameAvailable || !!usernameError}
428426
className={`w-full py-4 px-6 rounded-xl font-semibold text-sm transition-all duration-200 ${
429-
isLoading || !firstName.trim() || !lastName.trim() || !username || usernameAvailable === false
427+
isLoading || !firstName.trim() || !lastName.trim() || !username || !usernameAvailable || !!usernameError
430428
? 'bg-gray-200 text-gray-400 cursor-not-allowed'
431429
: 'bg-gradient-to-r from-blue-600 to-indigo-600 text-white hover:from-blue-700 hover:to-indigo-700 hover:shadow-lg hover:scale-[1.02] active:scale-[0.98]'
432430
}`}

0 commit comments

Comments
 (0)