clean up login page
This commit is contained in:
parent
7001796343
commit
827fcbbae6
1 changed files with 28 additions and 76 deletions
|
|
@ -2,58 +2,43 @@ import "package:flutter/material.dart";
|
||||||
import "package:flutter_hooks/flutter_hooks.dart";
|
import "package:flutter_hooks/flutter_hooks.dart";
|
||||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||||
import "package:nexus/controllers/client_controller.dart";
|
import "package:nexus/controllers/client_controller.dart";
|
||||||
import "package:nexus/models/requests/login_request.dart";
|
|
||||||
import "package:nexus/widgets/appbar.dart";
|
import "package:nexus/widgets/appbar.dart";
|
||||||
|
|
||||||
class LoginPage extends HookConsumerWidget {
|
class LoginPage extends HookConsumerWidget {
|
||||||
final Uri homeserver;
|
final Uri homeserver;
|
||||||
|
|
||||||
const LoginPage({super.key, required this.homeserver});
|
const LoginPage({super.key, required this.homeserver});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final client = ref.watch(ClientController.provider.notifier);
|
final client = ref.watch(ClientController.provider.notifier);
|
||||||
final isLoggingIn = useState(false);
|
|
||||||
final hasError = useState(false);
|
|
||||||
final passwordFocusNode = useFocusNode();
|
|
||||||
|
|
||||||
final theme = Theme.of(context);
|
|
||||||
|
|
||||||
|
final isLoading = useState(false);
|
||||||
final username = useTextEditingController();
|
final username = useTextEditingController();
|
||||||
final password = useTextEditingController();
|
final password = useTextEditingController();
|
||||||
|
|
||||||
|
final inputError = useState<String?>(null);
|
||||||
|
|
||||||
Future<void> tryLogin() async {
|
Future<void> tryLogin() async {
|
||||||
if (isLoggingIn.value) return;
|
isLoading.value = true;
|
||||||
isLoggingIn.value = true;
|
|
||||||
hasError.value = false;
|
|
||||||
|
|
||||||
final error = await client.login(
|
try {
|
||||||
LoginRequest(
|
final error = await client.login(
|
||||||
username: username.text,
|
.new(
|
||||||
password: password.text,
|
username: username.text,
|
||||||
homeserverUrl: homeserver.origin,
|
password: password.text,
|
||||||
),
|
homeserverUrl: homeserver.origin,
|
||||||
);
|
|
||||||
|
|
||||||
if (!context.mounted) return;
|
|
||||||
|
|
||||||
if (error != null) {
|
|
||||||
hasError.value = true;
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(
|
|
||||||
content: Text(
|
|
||||||
"Login failed. Is your password right?\nError: $error",
|
|
||||||
style: TextStyle(color: theme.colorScheme.onErrorContainer),
|
|
||||||
),
|
|
||||||
backgroundColor: theme.colorScheme.errorContainer,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
isLoggingIn.value = false;
|
|
||||||
} else {
|
if (error != null) {
|
||||||
Navigator.of(context).pop();
|
inputError.value = error;
|
||||||
|
isLoading.value = false;
|
||||||
|
} else {
|
||||||
|
if (context.mounted) Navigator.of(context).pop();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
isLoading.value = false;
|
||||||
}
|
}
|
||||||
passwordFocusNode.requestFocus();
|
|
||||||
isLoggingIn.value = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
|
@ -66,57 +51,24 @@ class LoginPage extends HookConsumerWidget {
|
||||||
body: AlertDialog(
|
body: AlertDialog(
|
||||||
title: Text("Login to ${homeserver.host}"),
|
title: Text("Login to ${homeserver.host}"),
|
||||||
content: Column(
|
content: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: .min,
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: .start,
|
||||||
children: [
|
children: [
|
||||||
Text("Enter your login credentials:"),
|
|
||||||
SizedBox(height: 12),
|
|
||||||
TextField(
|
TextField(
|
||||||
autofocus: true,
|
autofocus: true,
|
||||||
textInputAction: TextInputAction.next,
|
textInputAction: .next,
|
||||||
onChanged: (newVal) {
|
decoration: .new(label: Text("Username")),
|
||||||
if (hasError.value) {
|
|
||||||
hasError.value = false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
decoration: InputDecoration(
|
|
||||||
label: Text("Username"),
|
|
||||||
focusedBorder: hasError.value
|
|
||||||
? OutlineInputBorder(
|
|
||||||
borderSide: BorderSide(color: theme.colorScheme.error),
|
|
||||||
)
|
|
||||||
: null,
|
|
||||||
enabledBorder: hasError.value
|
|
||||||
? OutlineInputBorder(
|
|
||||||
borderSide: BorderSide(color: theme.colorScheme.error),
|
|
||||||
)
|
|
||||||
: null,
|
|
||||||
),
|
|
||||||
controller: username,
|
controller: username,
|
||||||
),
|
),
|
||||||
SizedBox(height: 12),
|
SizedBox(height: 12),
|
||||||
TextField(
|
TextField(
|
||||||
focusNode: passwordFocusNode,
|
textInputAction: .done,
|
||||||
textInputAction: TextInputAction.done,
|
|
||||||
onSubmitted: (_) => tryLogin(),
|
onSubmitted: (_) => tryLogin(),
|
||||||
onChanged: (newVal) {
|
|
||||||
if (hasError.value) {
|
|
||||||
hasError.value = false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
selectAllOnFocus: true,
|
selectAllOnFocus: true,
|
||||||
decoration: InputDecoration(
|
decoration: .new(
|
||||||
label: Text("Password"),
|
label: Text("Password"),
|
||||||
focusedBorder: hasError.value
|
errorText: inputError.value,
|
||||||
? OutlineInputBorder(
|
errorMaxLines: 5,
|
||||||
borderSide: BorderSide(color: theme.colorScheme.error),
|
|
||||||
)
|
|
||||||
: null,
|
|
||||||
enabledBorder: hasError.value
|
|
||||||
? OutlineInputBorder(
|
|
||||||
borderSide: BorderSide(color: theme.colorScheme.error),
|
|
||||||
)
|
|
||||||
: null,
|
|
||||||
),
|
),
|
||||||
controller: password,
|
controller: password,
|
||||||
obscureText: true,
|
obscureText: true,
|
||||||
|
|
@ -125,7 +77,7 @@ class LoginPage extends HookConsumerWidget {
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: hasError.value ? null : tryLogin,
|
onPressed: isLoading.value ? null : tryLogin,
|
||||||
child: Text("Sign In"),
|
child: Text("Sign In"),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue