@@ -8,6 +8,7 @@ import { toast } from 'sonner';
88import CodeuniaLogo from '@/components/codeunia-logo' ;
99import { InputValidator } from '@/lib/security/input-validation' ;
1010import { CheckCircle , XCircle , AlertCircle , Loader2 , Sparkles } from 'lucide-react' ;
11+ import { profileService } from '@/lib/services/profile' ;
1112
1213interface User {
1314 id : string ;
@@ -47,29 +48,30 @@ export default function CompleteProfile() {
4748 }
4849 setUser ( user ) ;
4950
50- // Check if profile already exists and is complete
51- const { data : profile } = await getSupabaseClient ( )
52- . from ( 'profiles' )
53- . select ( 'first_name, last_name, username, profile_complete' )
54- . eq ( 'id' , user . id )
55- . single ( ) ;
56-
57- if ( profile ) {
58- const isProfileComplete = profile . first_name &&
59- profile . last_name &&
60- profile . username &&
61- profile . profile_complete ;
51+ // Check if profile already exists and is complete using profileService
52+ try {
53+ const profile = await profileService . getProfile ( user . id ) ;
6254
63- if ( isProfileComplete ) {
64- // Profile is already complete, redirect to dashboard
65- router . push ( '/protected/dashboard' ) ;
66- return ;
55+ if ( profile ) {
56+ const isProfileComplete = profile . first_name &&
57+ profile . last_name &&
58+ profile . username &&
59+ profile . profile_complete ;
60+
61+ if ( isProfileComplete ) {
62+ // Profile is already complete, redirect to dashboard
63+ router . push ( '/protected/dashboard' ) ;
64+ return ;
65+ }
66+
67+ // Pre-fill existing data
68+ if ( profile . first_name ) setFirstName ( profile . first_name ) ;
69+ if ( profile . last_name ) setLastName ( profile . last_name ) ;
70+ if ( profile . username ) setUsername ( profile . username ) ;
6771 }
68-
69- // Pre-fill existing data
70- if ( profile . first_name ) setFirstName ( profile . first_name ) ;
71- if ( profile . last_name ) setLastName ( profile . last_name ) ;
72- if ( profile . username ) setUsername ( profile . username ) ;
72+ } catch ( profileError ) {
73+ console . error ( 'Error checking profile:' , profileError ) ;
74+ // Continue with the form - profileService will handle creation if needed
7375 }
7476
7577 // Pre-fill from OAuth provider data if available
@@ -186,30 +188,32 @@ export default function CompleteProfile() {
186188
187189 setIsLoading ( true ) ;
188190 try {
189- // Update profile with the provided information using upsert to handle missing profiles
190- const { data : upserted , error } = await getSupabaseClient ( )
191+ // First update the basic profile information using profileService
192+ const updatedProfile = await profileService . updateProfile ( user . id , {
193+ first_name : firstName . trim ( ) ,
194+ last_name : lastName . trim ( ) ,
195+ username : username . trim ( )
196+ } ) ;
197+
198+ if ( ! updatedProfile ) {
199+ console . error ( 'Profile update failed: No data returned' ) ;
200+ toast . error ( 'Failed to update profile. Please try again.' ) ;
201+ return ;
202+ }
203+
204+ // Then update the completion status fields directly
205+ const { error : completionError } = await getSupabaseClient ( )
191206 . from ( 'profiles' )
192- . upsert ( [ {
193- id : user . id ,
194- first_name : firstName . trim ( ) ,
195- last_name : lastName . trim ( ) ,
196- username : username . trim ( ) ,
207+ . update ( {
197208 profile_complete : true ,
198209 username_set : true ,
199210 username_editable : false
200- } ] , { onConflict : 'id' } )
201- . select ( 'id' )
202- . single ( ) ;
203-
204- if ( error ) {
205- console . error ( 'Profile update error:' , error ) ;
206- toast . error ( error . message || 'Failed to update profile. Please try again.' ) ;
207- return ;
208- }
211+ } )
212+ . eq ( 'id' , user . id ) ;
209213
210- if ( ! upserted ) {
211- console . error ( 'Profile update failed: No data returned' ) ;
212- toast . error ( 'Failed to update profile . Please try again.' ) ;
214+ if ( completionError ) {
215+ console . error ( 'Error updating completion status:' , completionError ) ;
216+ toast . error ( 'Profile updated but completion status failed . Please try again.' ) ;
213217 return ;
214218 }
215219
0 commit comments