Skip to content

Commit 90bb655

Browse files
committed
Fix 401 authentication errors in PledgeBar for logged-out users
- Added credentials: 'include' to all API calls in PledgeBar component - Enhanced authentication checks in loadUserData function - Added better error handling for budget validation API calls - Fixed validatePledgeBudget service to include credentials in API requests - Added more robust authentication state checking before making API calls - Prevents 401 errors when logged-out users visit pages with pledge bars
1 parent 4f3daf2 commit 90bb655

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

app/components/payments/PledgeBar.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

app/services/pledgeBudgetService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ export const getUserPledges = async (userId: string): Promise<PledgeItem[]> => {
108108
*/
109109
export const validatePledgeBudget = async (userId: string): Promise<BudgetValidationResult> => {
110110
try {
111-
const response = await fetch('/api/pledges/validate-budget');
111+
const response = await fetch('/api/pledges/validate-budget', {
112+
credentials: 'include'
113+
});
112114
if (!response.ok) {
113115
throw new Error('Failed to validate pledge budget');
114116
}

0 commit comments

Comments
 (0)