feat(server): Improve server health checks and authentication flow
This commit is contained in:
@@ -30,6 +30,7 @@ class ApiAuthInterceptor extends Interceptor {
|
|||||||
|
|
||||||
// Endpoints that have optional authentication (work without but better with)
|
// Endpoints that have optional authentication (work without but better with)
|
||||||
static const Set<String> _optionalAuthEndpoints = {
|
static const Set<String> _optionalAuthEndpoints = {
|
||||||
|
'/api/config',
|
||||||
'/api/models',
|
'/api/models',
|
||||||
'/api/v1/configs/models',
|
'/api/v1/configs/models',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -81,8 +81,14 @@ class RouterNotifier extends ChangeNotifier {
|
|||||||
final activeServer = activeServerAsync.asData?.value;
|
final activeServer = activeServerAsync.asData?.value;
|
||||||
final hasActiveServer = activeServer != null;
|
final hasActiveServer = activeServer != null;
|
||||||
if (!hasActiveServer) {
|
if (!hasActiveServer) {
|
||||||
// Allow auth-related routes while no server configured
|
// No server configured - redirect to server connection
|
||||||
if (_isAuthLocation(location)) return null;
|
// Exception: allow staying on server connection or authentication pages
|
||||||
|
// But always redirect away from connection issue page (user logged out)
|
||||||
|
if (location == Routes.serverConnection ||
|
||||||
|
location == Routes.authentication ||
|
||||||
|
location == Routes.login) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return Routes.serverConnection;
|
return Routes.serverConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ class ApiService {
|
|||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Health check
|
/// Basic health check - just verifies the server is reachable.
|
||||||
Future<bool> checkHealth() async {
|
Future<bool> checkHealth() async {
|
||||||
try {
|
try {
|
||||||
final response = await _dio.get('/health');
|
final response = await _dio.get('/health');
|
||||||
@@ -229,6 +229,35 @@ class ApiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Verifies this is actually an OpenWebUI server by checking the /api/config
|
||||||
|
/// endpoint for OpenWebUI-specific fields (version, status, features).
|
||||||
|
///
|
||||||
|
/// Returns `true` if the server appears to be a valid OpenWebUI instance.
|
||||||
|
Future<bool> verifyIsOpenWebUIServer() async {
|
||||||
|
try {
|
||||||
|
final response = await _dio.get('/api/config');
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final data = response.data;
|
||||||
|
if (data is! Map<String, dynamic>) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for OpenWebUI-specific fields
|
||||||
|
// The /api/config endpoint always returns these fields on OpenWebUI
|
||||||
|
final hasStatus = data['status'] == true;
|
||||||
|
final hasVersion =
|
||||||
|
data['version'] is String && (data['version'] as String).isNotEmpty;
|
||||||
|
final hasFeatures = data['features'] is Map;
|
||||||
|
|
||||||
|
return hasStatus && hasVersion && hasFeatures;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Enhanced health check with model availability
|
// Enhanced health check with model availability
|
||||||
Future<Map<String, dynamic>> checkServerStatus() async {
|
Future<Map<String, dynamic>> checkServerStatus() async {
|
||||||
final result = <String, dynamic>{
|
final result = <String, dynamic>{
|
||||||
|
|||||||
@@ -32,10 +32,18 @@ class _ErrorBoundaryState extends ConsumerState<ErrorBoundary> {
|
|||||||
void Function(FlutterErrorDetails details)? _previousOnError;
|
void Function(FlutterErrorDetails details)? _previousOnError;
|
||||||
|
|
||||||
bool _shouldIgnoreError(Object error) {
|
bool _shouldIgnoreError(Object error) {
|
||||||
// Ignore RenderFlex overflow errors (layout issues)
|
|
||||||
final errorString = error.toString();
|
final errorString = error.toString();
|
||||||
return errorString.contains('RenderFlex') ||
|
// Ignore RenderFlex overflow errors (layout issues)
|
||||||
errorString.contains('overflow') && errorString.contains('pixels');
|
if (errorString.contains('RenderFlex') ||
|
||||||
|
errorString.contains('overflow') && errorString.contains('pixels')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Ignore "Build scheduled during frame" errors - these are harmless
|
||||||
|
// framework warnings from animations during layout
|
||||||
|
if (errorString.contains('Build scheduled during frame')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _scheduleHandleError(Object error, StackTrace? stack) {
|
void _scheduleHandleError(Object error, StackTrace? stack) {
|
||||||
@@ -59,6 +67,10 @@ class _ErrorBoundaryState extends ConsumerState<ErrorBoundary> {
|
|||||||
// Set up Flutter error handling for this widget
|
// Set up Flutter error handling for this widget
|
||||||
_previousOnError = FlutterError.onError;
|
_previousOnError = FlutterError.onError;
|
||||||
FlutterError.onError = (FlutterErrorDetails details) {
|
FlutterError.onError = (FlutterErrorDetails details) {
|
||||||
|
// Check if this is a harmless error we should completely ignore
|
||||||
|
if (_shouldIgnoreError(details.exception)) {
|
||||||
|
return; // Don't forward or handle
|
||||||
|
}
|
||||||
// Forward to any previously registered handler to avoid interfering
|
// Forward to any previously registered handler to avoid interfering
|
||||||
_previousOnError?.call(details);
|
_previousOnError?.call(details);
|
||||||
// Defer handling to avoid setState during build
|
// Defer handling to avoid setState during build
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ class _AuthenticationPageState extends ConsumerState<AuthenticationPage> {
|
|||||||
bool _useApiKey = false;
|
bool _useApiKey = false;
|
||||||
String? _loginError;
|
String? _loginError;
|
||||||
bool _isSigningIn = false;
|
bool _isSigningIn = false;
|
||||||
|
bool _serverConfigSaved = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -72,13 +73,20 @@ class _AuthenticationPageState extends ConsumerState<AuthenticationPage> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Save server config on first sign-in attempt if it's a new config
|
||||||
|
// This persists the server so user can retry with different credentials
|
||||||
|
if (widget.serverConfig != null && !_serverConfigSaved) {
|
||||||
|
await _saveServerConfig(widget.serverConfig!);
|
||||||
|
_serverConfigSaved = true;
|
||||||
|
}
|
||||||
|
|
||||||
final actions = ref.read(authActionsProvider);
|
final actions = ref.read(authActionsProvider);
|
||||||
bool success;
|
bool success;
|
||||||
|
|
||||||
if (_useApiKey) {
|
if (_useApiKey) {
|
||||||
success = await actions.loginWithApiKey(
|
success = await actions.loginWithApiKey(
|
||||||
_apiKeyController.text.trim(),
|
_apiKeyController.text.trim(),
|
||||||
rememberCredentials: true, // Consistent with credentials method
|
rememberCredentials: true,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
success = await actions.login(
|
success = await actions.login(
|
||||||
@@ -95,6 +103,9 @@ class _AuthenticationPageState extends ConsumerState<AuthenticationPage> {
|
|||||||
|
|
||||||
// Success - navigation will be handled by auth state change
|
// Success - navigation will be handled by auth state change
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
// Don't clear server config on auth failure - user should be able to retry
|
||||||
|
// The server config is valid (passed OpenWebUI verification), only the
|
||||||
|
// credentials were wrong or there was a network issue
|
||||||
setState(() {
|
setState(() {
|
||||||
_loginError = _formatLoginError(e.toString());
|
_loginError = _formatLoginError(e.toString());
|
||||||
});
|
});
|
||||||
@@ -107,6 +118,14 @@ class _AuthenticationPageState extends ConsumerState<AuthenticationPage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _saveServerConfig(ServerConfig config) async {
|
||||||
|
final storage = ref.read(optimizedStorageServiceProvider);
|
||||||
|
await storage.saveServerConfigs([config]);
|
||||||
|
await storage.setActiveServerId(config.id);
|
||||||
|
ref.invalidate(serverConfigsProvider);
|
||||||
|
ref.invalidate(activeServerProvider);
|
||||||
|
}
|
||||||
|
|
||||||
String _formatLoginError(String error) {
|
String _formatLoginError(String error) {
|
||||||
if (error.contains('401') || error.contains('Unauthorized')) {
|
if (error.contains('401') || error.contains('Unauthorized')) {
|
||||||
return AppLocalizations.of(context)!.invalidCredentials;
|
return AppLocalizations.of(context)!.invalidCredentials;
|
||||||
@@ -164,6 +183,8 @@ class _AuthenticationPageState extends ConsumerState<AuthenticationPage> {
|
|||||||
// Main content
|
// Main content
|
||||||
Expanded(
|
Expanded(
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
|
keyboardDismissBehavior:
|
||||||
|
ScrollViewKeyboardDismissBehavior.onDrag,
|
||||||
child: ConstrainedBox(
|
child: ConstrainedBox(
|
||||||
constraints: const BoxConstraints(maxWidth: 480),
|
constraints: const BoxConstraints(maxWidth: 480),
|
||||||
child: Form(
|
child: Form(
|
||||||
|
|||||||
@@ -215,19 +215,31 @@ class _ConnectionIssuePageState extends ConsumerState<ConnectionIssuePage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _retryConnection() async {
|
Future<void> _retryConnection() async {
|
||||||
|
final l10n = AppLocalizations.of(context)!;
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_isRetrying = true;
|
_isRetrying = true;
|
||||||
_statusMessage = null;
|
_statusMessage = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Clear the error state and attempt to re-establish connection
|
|
||||||
final authManager = ref.read(authStateManagerProvider.notifier);
|
final authManager = ref.read(authStateManagerProvider.notifier);
|
||||||
|
final authState = ref.read(authStateManagerProvider);
|
||||||
|
final hasValidToken = authState.maybeWhen(
|
||||||
|
data: (state) => state.hasValidToken,
|
||||||
|
orElse: () => false,
|
||||||
|
);
|
||||||
|
|
||||||
// Reset retry counter for manual retry attempts
|
// Reset retry counter for manual retry attempts
|
||||||
authManager.resetRetryCounter();
|
authManager.resetRetryCounter();
|
||||||
|
|
||||||
|
if (hasValidToken) {
|
||||||
|
// User has a valid token - just refresh to verify connection
|
||||||
|
await authManager.refresh();
|
||||||
|
} else {
|
||||||
|
// No valid token - attempt silent login with saved credentials
|
||||||
await authManager.silentLogin();
|
await authManager.silentLogin();
|
||||||
|
}
|
||||||
|
|
||||||
// If successful, router will automatically navigate to chat
|
// If successful, router will automatically navigate to chat
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
@@ -237,7 +249,7 @@ class _ConnectionIssuePageState extends ConsumerState<ConnectionIssuePage> {
|
|||||||
} catch (_) {
|
} catch (_) {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() {
|
setState(() {
|
||||||
_statusMessage = 'Connection failed. Please try again.';
|
_statusMessage = l10n.couldNotConnectGeneric;
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import '../../../core/services/api_service.dart';
|
|||||||
import '../../../core/services/worker_manager.dart';
|
import '../../../core/services/worker_manager.dart';
|
||||||
import '../../../core/services/input_validation_service.dart';
|
import '../../../core/services/input_validation_service.dart';
|
||||||
import '../../../core/services/navigation_service.dart';
|
import '../../../core/services/navigation_service.dart';
|
||||||
|
import '../../../core/utils/debug_logger.dart';
|
||||||
import '../../../core/widgets/error_boundary.dart';
|
import '../../../core/widgets/error_boundary.dart';
|
||||||
import '../../../shared/services/brand_service.dart';
|
import '../../../shared/services/brand_service.dart';
|
||||||
import '../../../shared/theme/theme_extensions.dart';
|
import '../../../shared/theme/theme_extensions.dart';
|
||||||
@@ -63,7 +64,22 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _connectToServer() async {
|
Future<void> _connectToServer() async {
|
||||||
if (!_formKey.currentState!.validate()) return;
|
DebugLogger.log('Connect button pressed', scope: 'auth/connection');
|
||||||
|
|
||||||
|
final urlValue = _urlController.text.trim();
|
||||||
|
DebugLogger.log('URL value: "$urlValue"', scope: 'auth/connection');
|
||||||
|
|
||||||
|
// Check what validation would return
|
||||||
|
final validationResult = InputValidationService.validateUrl(urlValue);
|
||||||
|
DebugLogger.log(
|
||||||
|
'URL validation result: ${validationResult ?? "valid"}',
|
||||||
|
scope: 'auth/connection',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!_formKey.currentState!.validate()) {
|
||||||
|
DebugLogger.log('Form validation failed', scope: 'auth/connection');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_isConnecting = true;
|
_isConnecting = true;
|
||||||
@@ -87,21 +103,56 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
|
|||||||
serverConfig: tempConfig,
|
serverConfig: tempConfig,
|
||||||
workerManager: workerManager,
|
workerManager: workerManager,
|
||||||
);
|
);
|
||||||
final isHealthy = await api.checkHealth();
|
|
||||||
if (!isHealthy) {
|
// First check basic connectivity
|
||||||
|
DebugLogger.log('Checking server health...', scope: 'auth/connection');
|
||||||
|
final isReachable = await api.checkHealth();
|
||||||
|
DebugLogger.log(
|
||||||
|
'Health check result: $isReachable',
|
||||||
|
scope: 'auth/connection',
|
||||||
|
);
|
||||||
|
if (!isReachable) {
|
||||||
|
throw Exception(
|
||||||
|
'Could not reach the server. Please check the address.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then verify it's actually an OpenWebUI server
|
||||||
|
DebugLogger.log(
|
||||||
|
'Verifying OpenWebUI server...',
|
||||||
|
scope: 'auth/connection',
|
||||||
|
);
|
||||||
|
final isOpenWebUI = await api.verifyIsOpenWebUIServer();
|
||||||
|
DebugLogger.log(
|
||||||
|
'OpenWebUI verification result: $isOpenWebUI',
|
||||||
|
scope: 'auth/connection',
|
||||||
|
);
|
||||||
|
if (!isOpenWebUI) {
|
||||||
throw Exception('This does not appear to be an Open-WebUI server.');
|
throw Exception('This does not appear to be an Open-WebUI server.');
|
||||||
}
|
}
|
||||||
|
|
||||||
await _saveServerConfig(tempConfig);
|
DebugLogger.log(
|
||||||
|
'Server validation passed, navigating to auth page',
|
||||||
|
scope: 'auth/connection',
|
||||||
|
);
|
||||||
|
|
||||||
// Navigate to authentication page
|
// Don't save server config yet - wait until authentication succeeds
|
||||||
|
// The config is passed to the authentication page
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
context.pushNamed(RouteNames.authentication, extra: tempConfig);
|
context.pushNamed(RouteNames.authentication, extra: tempConfig);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e, stack) {
|
||||||
|
DebugLogger.error(
|
||||||
|
'server-connection-error',
|
||||||
|
scope: 'auth/connection',
|
||||||
|
error: e,
|
||||||
|
stackTrace: stack,
|
||||||
|
);
|
||||||
|
if (mounted) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_connectionError = _formatConnectionError(e.toString());
|
_connectionError = _formatConnectionError(e.toString());
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -111,14 +162,6 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _saveServerConfig(ServerConfig config) async {
|
|
||||||
final storage = ref.read(optimizedStorageServiceProvider);
|
|
||||||
await storage.saveServerConfigs([config]);
|
|
||||||
await storage.setActiveServerId(config.id);
|
|
||||||
ref.invalidate(serverConfigsProvider);
|
|
||||||
ref.invalidate(activeServerProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
String _validateAndFormatUrl(String input) {
|
String _validateAndFormatUrl(String input) {
|
||||||
if (input.isEmpty) {
|
if (input.isEmpty) {
|
||||||
throw Exception(AppLocalizations.of(context)!.serverUrlEmpty);
|
throw Exception(AppLocalizations.of(context)!.serverUrlEmpty);
|
||||||
@@ -244,6 +287,8 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
|
|||||||
// Main content
|
// Main content
|
||||||
Expanded(
|
Expanded(
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
|
keyboardDismissBehavior:
|
||||||
|
ScrollViewKeyboardDismissBehavior.onDrag,
|
||||||
child: ConstrainedBox(
|
child: ConstrainedBox(
|
||||||
constraints: const BoxConstraints(maxWidth: 480),
|
constraints: const BoxConstraints(maxWidth: 480),
|
||||||
child: Form(
|
child: Form(
|
||||||
@@ -528,12 +573,16 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
AnimatedSize(
|
// Use AnimatedCrossFade instead of AnimatedSize to avoid
|
||||||
|
// "Build scheduled during frame" errors
|
||||||
|
AnimatedCrossFade(
|
||||||
duration: AnimationDuration.microInteraction,
|
duration: AnimationDuration.microInteraction,
|
||||||
curve: Curves.easeInOutCubic,
|
sizeCurve: Curves.easeInOutCubic,
|
||||||
child: _showAdvancedSettings
|
crossFadeState: _showAdvancedSettings
|
||||||
? _buildAdvancedSettingsContent()
|
? CrossFadeState.showSecond
|
||||||
: const SizedBox.shrink(),
|
: CrossFadeState.showFirst,
|
||||||
|
firstChild: const SizedBox.shrink(),
|
||||||
|
secondChild: _buildAdvancedSettingsContent(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@@ -845,8 +894,8 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String? _validateHeaderKey(String key) {
|
String? _validateHeaderKey(String key) {
|
||||||
// RFC 7230 compliant header name validation
|
// Allow empty - header fields are optional
|
||||||
if (key.isEmpty) return AppLocalizations.of(context)!.headerNameEmpty;
|
if (key.isEmpty) return null;
|
||||||
if (key.length > 64) return AppLocalizations.of(context)!.headerNameTooLong;
|
if (key.length > 64) return AppLocalizations.of(context)!.headerNameTooLong;
|
||||||
|
|
||||||
// Check for valid characters (RFC 7230: token characters)
|
// Check for valid characters (RFC 7230: token characters)
|
||||||
@@ -879,7 +928,8 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String? _validateHeaderValue(String value) {
|
String? _validateHeaderValue(String value) {
|
||||||
if (value.isEmpty) return AppLocalizations.of(context)!.headerValueEmpty;
|
// Allow empty - header fields are optional
|
||||||
|
if (value.isEmpty) return null;
|
||||||
if (value.length > 1024) {
|
if (value.length > 1024) {
|
||||||
return AppLocalizations.of(context)!.headerValueTooLong;
|
return AppLocalizations.of(context)!.headerValueTooLong;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user