From 512c0b038aea14ff6e6a1f038c9901c9c68d5c2f Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 10 Apr 2023 16:49:11 +0200 Subject: [PATCH] neon: Allow empty scheme for login --- packages/neon/lib/src/pages/login.dart | 12 ++++++++++-- packages/neon/lib/src/utils/validators.dart | 18 +++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/neon/lib/src/pages/login.dart b/packages/neon/lib/src/pages/login.dart index 52a30441..e1f4ee2e 100644 --- a/packages/neon/lib/src/pages/login.dart +++ b/packages/neon/lib/src/pages/login.dart @@ -191,10 +191,18 @@ class _LoginPageState extends State { hintText: 'https://...', ), initialValue: widget.serverURL, - validator: (final input) => validateHttpUrl(context, input), + validator: (final input) => validateHttpUrl( + context, + input, + allowEmptyScheme: true, + ), onFieldSubmitted: (final input) { if (_formKey.currentState!.validate()) { - _loginBloc.setServerURL(input); + var url = input; + if (!(url.startsWith('http://') || url.startsWith('https://'))) { + url = 'http://$url'; + } + _loginBloc.setServerURL(url); } else { _focusNode.requestFocus(); } diff --git a/packages/neon/lib/src/utils/validators.dart b/packages/neon/lib/src/utils/validators.dart index 9d615c8b..3da20514 100644 --- a/packages/neon/lib/src/utils/validators.dart +++ b/packages/neon/lib/src/utils/validators.dart @@ -4,20 +4,24 @@ String? validateHttpUrl( final BuildContext context, final String? input, { final bool httpsOnly = false, + final bool allowEmptyScheme = false, }) { if (input == null || input == '') { return AppLocalizations.of(context).errorInvalidURL; } try { final uri = Uri.parse(input); - // TODO: Maybe make a better error message for http URLs if only https is allowed - if (!(!httpsOnly && (uri.isScheme('http')) || uri.isScheme('https'))) { - return AppLocalizations.of(context).errorInvalidURL; + if (uri.isScheme('https')) { + return null; } - } catch (e) { - return AppLocalizations.of(context).errorInvalidURL; - } - return null; + if (uri.isScheme('http') && !httpsOnly) { + return null; + } + if (uri.isScheme('') && allowEmptyScheme) { + return null; + } + } catch (_) {} + return AppLocalizations.of(context).errorInvalidURL; } String? validateNotEmpty(final BuildContext context, final String? input) {