fix: normal user logins calling admin endpoints
This commit is contained in:
@@ -62,6 +62,11 @@ class ApiAuthInterceptor extends Interceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Endpoints that support optional auth should not strictly require it
|
||||||
|
if (_hasOptionalAuth(path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// All other endpoints require authentication per OpenAPI spec
|
// All other endpoints require authentication per OpenAPI spec
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -135,9 +140,19 @@ class ApiAuthInterceptor extends Interceptor {
|
|||||||
|
|
||||||
// Handle authentication errors consistently
|
// Handle authentication errors consistently
|
||||||
if (statusCode == 401) {
|
if (statusCode == 401) {
|
||||||
// 401 always indicates invalid/expired auth token
|
// Do not clear the token for public or optional-auth endpoints.
|
||||||
DebugLogger.auth('401 Unauthorized on $path - clearing auth token');
|
// A 401 here may indicate endpoint-level permission or server config,
|
||||||
_clearAuthToken();
|
// not necessarily an expired/invalid token.
|
||||||
|
final requiresAuth = _requiresAuth(path);
|
||||||
|
final optionalAuth = _hasOptionalAuth(path);
|
||||||
|
if (requiresAuth && !optionalAuth) {
|
||||||
|
DebugLogger.auth('401 Unauthorized on $path - clearing auth token');
|
||||||
|
_clearAuthToken();
|
||||||
|
} else {
|
||||||
|
DebugLogger.auth(
|
||||||
|
'401 on public/optional endpoint $path - keeping auth token',
|
||||||
|
);
|
||||||
|
}
|
||||||
} else if (statusCode == 403) {
|
} else if (statusCode == 403) {
|
||||||
// 403 on protected endpoints indicates insufficient permissions or invalid token
|
// 403 on protected endpoints indicates insufficient permissions or invalid token
|
||||||
final requiresAuth = _requiresAuth(path);
|
final requiresAuth = _requiresAuth(path);
|
||||||
|
|||||||
@@ -219,13 +219,19 @@ class ApiService {
|
|||||||
|
|
||||||
DebugLogger.log('User settings retrieved successfully');
|
DebugLogger.log('User settings retrieved successfully');
|
||||||
|
|
||||||
final settings = response.data as Map<String, dynamic>;
|
final data = response.data;
|
||||||
|
if (data is! Map<String, dynamic>) {
|
||||||
|
DebugLogger.warning(
|
||||||
|
'User settings response is empty or unexpected type: ${data.runtimeType}',
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Extract default model from ui.models array
|
// Extract default model from ui.models array
|
||||||
final ui = settings['ui'] as Map<String, dynamic>?;
|
final ui = data['ui'];
|
||||||
if (ui != null) {
|
if (ui is Map<String, dynamic>) {
|
||||||
final models = ui['models'] as List?;
|
final models = ui['models'];
|
||||||
if (models != null && models.isNotEmpty) {
|
if (models is List && models.isNotEmpty) {
|
||||||
// Return the first model in the user's preferred models list
|
// Return the first model in the user's preferred models list
|
||||||
final defaultModel = models.first.toString();
|
final defaultModel = models.first.toString();
|
||||||
DebugLogger.log(
|
DebugLogger.log(
|
||||||
@@ -239,25 +245,8 @@ class ApiService {
|
|||||||
return null;
|
return null;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
DebugLogger.error('Error fetching default model from user settings', e);
|
DebugLogger.error('Error fetching default model from user settings', e);
|
||||||
// Fall back to trying the old endpoint
|
// Do not call admin-only configs endpoint here; let the caller
|
||||||
try {
|
// handle fallback (e.g., first available model from /api/models).
|
||||||
DebugLogger.log('Falling back to configs/models endpoint');
|
|
||||||
final response = await _dio.get('/api/v1/configs/models');
|
|
||||||
final config = response.data as Map<String, dynamic>;
|
|
||||||
|
|
||||||
final defaultModel =
|
|
||||||
config['DEFAULT_MODELS'] as String? ??
|
|
||||||
config['default_models'] as String? ??
|
|
||||||
config['default_model'] as String?;
|
|
||||||
|
|
||||||
if (defaultModel != null && defaultModel.isNotEmpty) {
|
|
||||||
DebugLogger.log('Found default model from fallback: $defaultModel');
|
|
||||||
return defaultModel;
|
|
||||||
}
|
|
||||||
} catch (fallbackError) {
|
|
||||||
DebugLogger.error('Fallback also failed', fallbackError);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user