@@ -87,6 +87,16 @@ export default function AccountSettingsDelete() {
8787 return ;
8888 }
8989
90+ // Get the current session token for API authentication
91+ const {
92+ data : { session } ,
93+ } = await supabase . auth . getSession ( ) ;
94+
95+ if ( ! session ) {
96+ toast . error ( "No active session" ) ;
97+ return ;
98+ }
99+
90100 // Delete user data from your codev table first
91101 const { error : dbError } = await supabase
92102 . from ( "codev" )
@@ -95,16 +105,37 @@ export default function AccountSettingsDelete() {
95105
96106 if ( dbError ) {
97107 toast . error ( "Failed to delete user data" ) ;
108+ console . error ( "Database deletion error:" , dbError ) ;
98109 return ;
99110 }
100111
101- // Delete the authentication user
102- const { error : authError } = await supabase . auth . admin . deleteUser (
103- user . id ,
104- ) ;
112+ // Call the API route to delete the authentication user
113+ const response = await fetch ( "/api/delete-auth-user" , {
114+ method : "POST" ,
115+ headers : {
116+ "Content-Type" : "application/json" ,
117+ "Authorization" : `Bearer ${ session . access_token } ` ,
118+ } ,
119+ body : JSON . stringify ( {
120+ userId : user . id ,
121+ } ) ,
122+ } ) ;
123+
124+ // Parse response with proper type handling
125+ const rawData : unknown = await response . json ( ) ;
126+
127+ // Type guard for API response
128+ interface ApiResponse {
129+ success ?: boolean ;
130+ message ?: string ;
131+ error ?: string ;
132+ }
133+
134+ const data = rawData as ApiResponse ;
105135
106- if ( authError ) {
107- toast . error ( "Failed to delete account" ) ;
136+ if ( ! response . ok ) {
137+ toast . error ( data . error || "Failed to delete account" ) ;
138+ console . error ( "API error:" , data ) ;
108139 return ;
109140 }
110141
@@ -163,78 +194,68 @@ export default function AccountSettingsDelete() {
163194 </ CardDescription >
164195 </ CardHeader >
165196 < CardContent >
166- { /* <Alert className="background-box mb-4 border border-red-600 text-red-600">
167- <AlertCircle className="h-4 w-4" color="red" />
168- <AlertTitle>Warning</AlertTitle>
169- <AlertDescription className="text-xs sm:text-sm">
170- This action is irreversible. All your data will be permanently
171- deleted.
172- </AlertDescription>
173- </Alert> */ }
174-
175- < Dialog
176- open = { isOpen }
177- onOpenChange = { ( open ) => {
178- setIsOpen ( open ) ;
179- if ( ! open ) {
180- form . reset ( ) ;
181- }
182- } }
183- >
184- < DialogTrigger asChild >
185- < Button variant = "destructive" > Delete Account</ Button >
186- </ DialogTrigger >
187- < DialogContent className = "text-dark100_light900 background-box w-[90%] sm:w-full" >
188- < DialogHeader >
189- < DialogTitle > Are you absolutely sure?</ DialogTitle >
190- < DialogDescription >
191- This action cannot be undone. This will permanently delete
192- your account and remove your data from our servers.
193- </ DialogDescription >
194- </ DialogHeader >
195- < Form { ...form } >
196- < form onSubmit = { form . handleSubmit ( onSubmit ) } noValidate >
197- < FormField
198- control = { form . control }
199- name = "confirmation"
200- render = { ( { field } ) => (
201- < FormItem >
202- < FormControl >
203- < Input
204- placeholder = "Type DELETE to confirm"
205- labelClassName = "sm:text-base text-sm"
206- label = "To confirm, type DELETE in the box below:"
207- parentClassName = "flex flex-col gap-2"
208- variant = "lightgray"
209- disabled = { isLoading }
210- { ...field }
211- />
212- </ FormControl >
213- < FormMessage className = "text-red-600" />
214- </ FormItem >
215- ) }
216- />
217- < DialogFooter className = "mt-4 gap-2" >
218- < DialogClose asChild >
219- < Button variant = "outline" disabled = { isLoading } >
220- Cancel
221- </ Button >
222- </ DialogClose >
223- < Button
224- variant = "destructive"
225- type = "submit"
226- disabled = { ! form . formState . isValid || isLoading }
227- >
228- { isLoading ? "Deleting..." : "Delete Account" }
197+ < Dialog
198+ open = { isOpen }
199+ onOpenChange = { ( open ) => {
200+ setIsOpen ( open ) ;
201+ if ( ! open ) {
202+ form . reset ( ) ;
203+ }
204+ } }
205+ >
206+ < DialogTrigger asChild >
207+ < Button variant = "destructive" > Delete Account</ Button >
208+ </ DialogTrigger >
209+ < DialogContent className = "text-dark100_light900 background-box w-[90%] sm:w-full" >
210+ < DialogHeader >
211+ < DialogTitle > Are you absolutely sure?</ DialogTitle >
212+ < DialogDescription >
213+ This action cannot be undone. This will permanently delete
214+ your account and remove your data from our servers.
215+ </ DialogDescription >
216+ </ DialogHeader >
217+ < Form { ...form } >
218+ < form onSubmit = { form . handleSubmit ( onSubmit ) } noValidate >
219+ < FormField
220+ control = { form . control }
221+ name = "confirmation"
222+ render = { ( { field } ) => (
223+ < FormItem >
224+ < FormControl >
225+ < Input
226+ placeholder = "Type DELETE to confirm"
227+ labelClassName = "sm:text-base text-sm"
228+ label = "To confirm, type DELETE in the box below:"
229+ parentClassName = "flex flex-col gap-2"
230+ variant = "lightgray"
231+ disabled = { isLoading }
232+ { ...field }
233+ />
234+ </ FormControl >
235+ < FormMessage className = "text-red-600" />
236+ </ FormItem >
237+ ) }
238+ />
239+ < DialogFooter className = "mt-4 gap-2" >
240+ < DialogClose asChild >
241+ < Button variant = "outline" disabled = { isLoading } >
242+ Cancel
229243 </ Button >
230- </ DialogFooter >
231- </ form >
232- </ Form >
233- </ DialogContent >
234- </ Dialog >
235-
244+ </ DialogClose >
245+ < Button
246+ variant = "destructive"
247+ type = "submit"
248+ disabled = { ! form . formState . isValid || isLoading }
249+ >
250+ { isLoading ? "Deleting..." : "Delete Account" }
251+ </ Button >
252+ </ DialogFooter >
253+ </ form >
254+ </ Form >
255+ </ DialogContent >
256+ </ Dialog >
236257 </ CardContent >
237258 </ Card >
238259 </ >
239260 ) ;
240- }
261+ }
0 commit comments