@@ -196,14 +196,17 @@ const PledgeBar = React.forwardRef<HTMLDivElement, PledgeBarProps>(({
196196
197197 // Validate budget when subscription or user changes
198198 useEffect ( ( ) => {
199- if ( currentAccount && isSubscriptionEnabled ) {
199+ if ( currentAccount && currentAccount . uid && isSubscriptionEnabled ) {
200200 const validateBudget = async ( ) => {
201201 try {
202202 const validation = await validatePledgeBudget ( currentAccount . uid ) ;
203203 setBudgetValidation ( validation ) ;
204204 setShowBudgetWarning ( validation . isOverBudget ) ;
205205 } catch ( error ) {
206206 console . error ( 'Error validating pledge budget:' , error ) ;
207+ // Reset budget validation on error
208+ setBudgetValidation ( null ) ;
209+ setShowBudgetWarning ( false ) ;
207210 }
208211 } ;
209212
@@ -252,7 +255,10 @@ const PledgeBar = React.forwardRef<HTMLDivElement, PledgeBarProps>(({
252255
253256 // Load user subscription and token data with automatic initialization
254257 const loadUserData = async ( ) => {
255- if ( ! currentAccount ) return ;
258+ if ( ! currentAccount || ! currentAccount . uid ) {
259+ console . log ( 'PledgeBar: No authenticated user, skipping API calls' ) ;
260+ return ;
261+ }
256262
257263 try {
258264 // Load subscription
@@ -263,7 +269,9 @@ const PledgeBar = React.forwardRef<HTMLDivElement, PledgeBarProps>(({
263269 if ( ! isPageOwner ) {
264270 // Try to load existing token balance using API
265271 try {
266- const balanceResponse = await fetch ( '/api/tokens/balance' ) ;
272+ const balanceResponse = await fetch ( '/api/tokens/balance' , {
273+ credentials : 'include'
274+ } ) ;
267275 if ( balanceResponse . ok ) {
268276 const balanceData = await balanceResponse . json ( ) ;
269277
@@ -272,7 +280,9 @@ const PledgeBar = React.forwardRef<HTMLDivElement, PledgeBarProps>(({
272280
273281 if ( balanceData . balance ) {
274282 // Load current page allocation only if balance exists
275- const allocationResponse = await fetch ( `/api/tokens/page-allocation?pageId=${ pageId } ` ) ;
283+ const allocationResponse = await fetch ( `/api/tokens/page-allocation?pageId=${ pageId } ` , {
284+ credentials : 'include'
285+ } ) ;
276286 if ( allocationResponse . ok ) {
277287 const allocationData = await allocationResponse . json ( ) ;
278288 setCurrentTokenAllocation ( allocationData . currentAllocation ) ;
@@ -311,6 +321,7 @@ const PledgeBar = React.forwardRef<HTMLDivElement, PledgeBarProps>(({
311321 // Use API endpoint for token initialization to avoid permission issues
312322 const response = await fetch ( '/api/tokens/balance' , {
313323 method : 'POST' ,
324+ credentials : 'include' ,
314325 headers : {
315326 'Content-Type' : 'application/json' } ,
316327 body : JSON . stringify ( {
@@ -329,7 +340,9 @@ const PledgeBar = React.forwardRef<HTMLDivElement, PledgeBarProps>(({
329340 setTokenBalance ( result . balance ) ;
330341
331342 // Load current page allocation using API
332- const allocationResponse = await fetch ( `/api/tokens/page-allocation?pageId=${ pageId } ` ) ;
343+ const allocationResponse = await fetch ( `/api/tokens/page-allocation?pageId=${ pageId } ` , {
344+ credentials : 'include'
345+ } ) ;
333346 if ( allocationResponse . ok ) {
334347 const allocationData = await allocationResponse . json ( ) ;
335348 setCurrentTokenAllocation ( allocationData . currentAllocation ) ;
0 commit comments