Browse Source

Merge pull request #244 from provokateurin/feature/allow-empty-scheme-login

neon: Allow empty scheme for login
pull/249/head
Kate 2 years ago committed by GitHub
parent
commit
bfb5187d93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      packages/neon/lib/src/pages/login.dart
  2. 14
      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();
} }

14
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;
} }
if (uri.isScheme('') && allowEmptyScheme) {
return null; 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