Browse Source

neon: Allow empty scheme for login

pull/244/head
jld3103 2 years ago
parent
commit
512c0b038a
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 12
      packages/neon/lib/src/pages/login.dart
  2. 18
      packages/neon/lib/src/utils/validators.dart

12
packages/neon/lib/src/pages/login.dart

@ -191,10 +191,18 @@ class _LoginPageState extends State<LoginPage> {
hintText: 'https://...', hintText: 'https://...',
), ),
initialValue: widget.serverURL, initialValue: widget.serverURL,
validator: (final input) => validateHttpUrl(context, input), validator: (final input) => validateHttpUrl(
context,
input,
allowEmptyScheme: true,
),
onFieldSubmitted: (final input) { onFieldSubmitted: (final input) {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_loginBloc.setServerURL(input); var url = input;
if (!(url.startsWith('http://') || url.startsWith('https://'))) {
url = 'http://$url';
}
_loginBloc.setServerURL(url);
} else { } else {
_focusNode.requestFocus(); _focusNode.requestFocus();
} }

18
packages/neon/lib/src/utils/validators.dart

@ -4,20 +4,24 @@ String? validateHttpUrl(
final BuildContext context, final BuildContext context,
final String? input, { final String? input, {
final bool httpsOnly = false, final bool httpsOnly = false,
final bool allowEmptyScheme = false,
}) { }) {
if (input == null || input == '') { if (input == null || input == '') {
return AppLocalizations.of(context).errorInvalidURL; return AppLocalizations.of(context).errorInvalidURL;
} }
try { try {
final uri = Uri.parse(input); final uri = Uri.parse(input);
// TODO: Maybe make a better error message for http URLs if only https is allowed if (uri.isScheme('https')) {
if (!(!httpsOnly && (uri.isScheme('http')) || uri.isScheme('https'))) { return null;
return AppLocalizations.of(context).errorInvalidURL;
} }
} catch (e) { if (uri.isScheme('http') && !httpsOnly) {
return AppLocalizations.of(context).errorInvalidURL; return null;
} }
return null; if (uri.isScheme('') && allowEmptyScheme) {
return null;
}
} catch (_) {}
return AppLocalizations.of(context).errorInvalidURL;
} }
String? validateNotEmpty(final BuildContext context, final String? input) { String? validateNotEmpty(final BuildContext context, final String? input) {

Loading…
Cancel
Save