diff --git a/backend/middleware/auth.go b/backend/middleware/auth.go index 1850581..5cf541d 100644 --- a/backend/middleware/auth.go +++ b/backend/middleware/auth.go @@ -59,8 +59,14 @@ func AuthMiddleware(jwtSecret string) fiber.Handler { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "invalid token type for query parameter"}) } } else { - if tokenType != "access" { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "invalid token type for authorization header"}) + if c.Path() == "/api/auth/refresh" { + if tokenType != "refresh" { + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "invalid token type for refresh endpoint"}) + } + } else { + if tokenType != "access" { + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "invalid token type for authorization header"}) + } } } diff --git a/frontend/src/app/page.tsx b/frontend/src/app/page.tsx index 6643dc9..fabac41 100644 --- a/frontend/src/app/page.tsx +++ b/frontend/src/app/page.tsx @@ -13,6 +13,13 @@ export default function Home() { useEffect(() => { checkState(); + + const handleAuthError = () => { + api.clearTokens(); + setState('login'); + }; + window.addEventListener('auth_error', handleAuthError); + return () => window.removeEventListener('auth_error', handleAuthError); }, []); async function checkState() { diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 300dea6..05b53be 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -81,6 +81,9 @@ class ApiClient { } if (!response.ok && endpoint !== '/api/auth/refresh') { + if (response.status === 401) { + if (typeof window !== 'undefined') window.dispatchEvent(new Event('auth_error')); + } let errorMsg = `HTTP ${response.status}`; try { const errData = await response.clone().json(); @@ -111,6 +114,7 @@ class ApiClient { } this.clearTokens(); + if (typeof window !== 'undefined') window.dispatchEvent(new Event('auth_error')); return false; }