@@ -7,7 +7,7 @@ import { Badge } from '@/components/ui/badge';
77import { Tabs , TabsContent , TabsList , TabsTrigger } from '@/components/ui/tabs' ;
88import { ScrollArea } from '@/components/ui/scroll-area' ;
99import { useToast } from '@/hooks/use-toast' ;
10- import { supabase } from '@/lib/supabase/client' ;
10+ import { supabase , SUPABASE_URL } from '@/lib/supabase/client' ;
1111import { BarChart , Bar , XAxis , YAxis , CartesianGrid , Tooltip , ResponsiveContainer , PieChart , Pie , Cell } from 'recharts' ;
1212import { Activity , Users , DollarSign , AlertCircle , Shield , Settings2 , Loader2 } from 'lucide-react' ;
1313
@@ -51,7 +51,7 @@ export default function AdminPage() {
5151 if ( ! session ) throw new Error ( 'Not authenticated' ) ;
5252
5353 const response = await fetch (
54- `${ import . meta . env . VITE_SUPABASE_URL } /functions/v1/admin?action=${ action } ` ,
54+ `${ SUPABASE_URL } /functions/v1/admin?action=${ action } ` ,
5555 {
5656 headers : {
5757 'Authorization' : `Bearer ${ session . access_token } ` ,
@@ -60,20 +60,36 @@ export default function AdminPage() {
6060 }
6161 ) ;
6262
63+ // Get response text first to handle HTML error pages
64+ const text = await response . text ( ) ;
65+
6366 if ( ! response . ok ) {
64- const error = await response . json ( ) ;
65- throw new Error ( error . error || 'Request failed' ) ;
67+ // Try to parse as JSON, fallback to text-based error
68+ try {
69+ const error = JSON . parse ( text ) ;
70+ throw new Error ( error . error || 'Request failed' ) ;
71+ } catch {
72+ // HTML error page or other non-JSON response
73+ if ( response . status === 401 ) throw new Error ( 'Not authenticated' ) ;
74+ if ( response . status === 403 ) throw new Error ( 'Admin access required' ) ;
75+ throw new Error ( `Request failed: ${ response . status } ` ) ;
76+ }
6677 }
6778
68- return response . json ( ) ;
79+ // Parse successful response as JSON
80+ try {
81+ return JSON . parse ( text ) ;
82+ } catch {
83+ throw new Error ( 'Invalid response from server' ) ;
84+ }
6985 } ;
7086
7187 const postAdminData = async ( action : string , body : unknown ) => {
7288 const { data : { session } } = await supabase . auth . getSession ( ) ;
7389 if ( ! session ) throw new Error ( 'Not authenticated' ) ;
7490
7591 const response = await fetch (
76- `${ import . meta . env . VITE_SUPABASE_URL } /functions/v1/admin?action=${ action } ` ,
92+ `${ SUPABASE_URL } /functions/v1/admin?action=${ action } ` ,
7793 {
7894 method : 'POST' ,
7995 headers : {
@@ -84,12 +100,25 @@ export default function AdminPage() {
84100 }
85101 ) ;
86102
103+ // Get response text first to handle HTML error pages
104+ const text = await response . text ( ) ;
105+
87106 if ( ! response . ok ) {
88- const error = await response . json ( ) ;
89- throw new Error ( error . error || 'Request failed' ) ;
107+ try {
108+ const error = JSON . parse ( text ) ;
109+ throw new Error ( error . error || 'Request failed' ) ;
110+ } catch {
111+ if ( response . status === 401 ) throw new Error ( 'Not authenticated' ) ;
112+ if ( response . status === 403 ) throw new Error ( 'Admin access required' ) ;
113+ throw new Error ( `Request failed: ${ response . status } ` ) ;
114+ }
90115 }
91116
92- return response . json ( ) ;
117+ try {
118+ return JSON . parse ( text ) ;
119+ } catch {
120+ return { success : true } ; // Some POST actions may not return JSON
121+ }
93122 } ;
94123
95124 useEffect ( ( ) => {
@@ -107,10 +136,16 @@ export default function AdminPage() {
107136 setIsAdmin ( true ) ;
108137 } catch ( err ) {
109138 console . error ( 'Admin load error:' , err ) ;
110- if ( err instanceof Error && err . message . includes ( 'Admin access required' ) ) {
139+ const message = err instanceof Error ? err . message : '' ;
140+ // Check for any access denial error
141+ if ( message . includes ( 'Admin access required' ) ||
142+ message . includes ( '403' ) ||
143+ message . includes ( 'Not authenticated' ) ||
144+ message . includes ( '401' ) ) {
111145 setIsAdmin ( false ) ;
112146 } else {
113- toast ( { title : 'Error' , description : err instanceof Error ? err . message : 'Failed to load admin data' , variant : 'destructive' } ) ;
147+ setIsAdmin ( false ) ; // Default to denying access on any error
148+ toast ( { title : 'Error' , description : message || 'Failed to load admin data' , variant : 'destructive' } ) ;
114149 }
115150 } finally {
116151 setLoading ( false ) ;
0 commit comments