1 Commits

Author SHA1 Message Date
Fran Jurmanović
8a5afee0e3 logout if unauthorized 2025-09-14 17:51:43 +02:00

View File

@@ -1,3 +1,6 @@
import { logout } from '@/lib/auth/server';
import { redirect } from 'next/navigation';
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080'; const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
export async function fetchServerAPI<T>( export async function fetchServerAPI<T>(
@@ -18,6 +21,10 @@ export async function fetchServerAPI<T>(
}); });
if (!response.ok) { if (!response.ok) {
if (response.status == 401) {
await logout();
redirect('/login');
}
throw new Error( throw new Error(
`API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}` `API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}`
); );