# Authentication Error Handling
## Overview
This document explains the improved authentication error handling mechanism that was implemented to prevent unnecessary logouts when non-authentication errors occur.
## Problem Statement
Previously, the application would log out users on **any** error with status codes 401 or 407, regardless of the actual cause. This was too aggressive and caused users to be logged out in scenarios that didn't warrant it, such as:
- **Authorization failures** (403 Forbidden) - User is authenticated but lacks permission
- **Temporary network issues** - Transient errors that could be retried
- **API validation errors** - Business logic errors unrelated to authentication
- **Resource-specific permission errors** - User can access other resources
## Solution
A new error classification system was implemented that analyzes errors to determine if they represent genuine authentication failures vs. other types of errors.
### Key Components
#### 1. Backend: `server/utils/authErrorHelper.ts`
Provides error classification logic for server-side code.
**Key Functions:**
- `classifyAuthError(error)` - Analyzes an error and returns classification
- `shouldLogoutOnError(error)` - Returns boolean indicating if logout is needed
**Error Types:**
- `token_expired` - Token has expired (logout required)
- `token_invalid` - Token is invalid or malformed (logout required)
- `authorization` - User authenticated but lacks permission (NO logout)
- `other` - Non-authentication error (NO logout)
- `none` - Not an auth-related error (NO logout)
#### 2. Backend: `server/utils/forceLogoutHelper.ts` (Refactored)
Updated to use the new classification logic instead of blindly logging out on 401/407.
**Changes:**
- Now calls `classifyAuthError()` before deciding to logout
- Returns boolean indicating if logout was performed
- Includes better error handling and logging
- Only logs out on true authentication failures
#### 3. Frontend: `composables/useAuthError.ts`
Provides the same error classification logic for Vue components.
**Usage Example:**
```vue
```
### Error Classification Logic
The system examines both the HTTP status code and the error message to classify errors:
#### Status Code Analysis
| Status | Default Action | Reasoning |
|--------|---------------|-----------|
| 401 | Logout | Unauthorized - typically means invalid/expired token |
| 403 | No Logout | Forbidden - user is authenticated but lacks permission |
| 407 | Logout | Proxy authentication required |
| Others | No Logout | Not authentication-related |
#### Message Analysis
The system also examines error messages for specific indicators:
**Token Expired Indicators** (→ Logout):
- "token expired"
- "jwt expired"
- "session expired"
- "authentication expired"
**Token Invalid Indicators** (→ Logout):
- "invalid token"
- "invalid credentials"
- "authentication failed"
- "token not found"
- "missing token"
**Authorization Indicators** (→ No Logout):
- "forbidden"
- "access denied"
- "insufficient permissions"
- "not authorized"
- "permission denied"
## Migration Guide
### For Backend API Endpoints
The existing server endpoints already use `forceLogoutHelper`, so they will automatically benefit from the improved logic. No changes needed.
### For Frontend Vue Pages
**Before:**
```vue