forked from Nexus/nexus
better login flow
Co-authored-by: Henry-Hiles <henry@henryhiles.com>
This commit is contained in:
parent
621bb74cc9
commit
27dca24889
10 changed files with 292 additions and 328 deletions
|
|
@ -1,205 +1,95 @@
|
|||
import "package:flutter/material.dart";
|
||||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
import "package:flutter_svg/flutter_svg.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/client_controller.dart";
|
||||
import "package:nexus/helpers/launch_helper.dart";
|
||||
import "package:nexus/models/homeserver.dart";
|
||||
import "package:nexus/widgets/appbar.dart";
|
||||
import "package:nexus/widgets/divider_text.dart";
|
||||
import "package:nexus/widgets/loading.dart";
|
||||
import "package:nexus/helpers/required_validator_helper.dart";
|
||||
|
||||
class LoginPage extends HookConsumerWidget {
|
||||
const LoginPage({super.key});
|
||||
final Uri homeserver;
|
||||
const LoginPage({super.key, required this.homeserver});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final theme = Theme.of(context);
|
||||
final client = ref.watch(ClientController.provider.notifier);
|
||||
|
||||
final isLoading = useState(false);
|
||||
final homeserver = useState<String?>(null);
|
||||
|
||||
final launch = ref.watch(LaunchHelper.provider).launchUrl;
|
||||
|
||||
Future<void> setHomeserver(Uri? newHomeserver) async {
|
||||
isLoading.value = true;
|
||||
|
||||
homeserver.value = newHomeserver == null
|
||||
? null
|
||||
: await client.discoverHomeserver(
|
||||
newHomeserver.hasScheme
|
||||
? newHomeserver
|
||||
: Uri.https(newHomeserver.path),
|
||||
);
|
||||
|
||||
if (homeserver.value == null && context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
.new(
|
||||
content: Text(
|
||||
"Homeserver verification failed. Is your homeserver down?",
|
||||
style: TextStyle(color: theme.colorScheme.onErrorContainer),
|
||||
),
|
||||
backgroundColor: theme.colorScheme.errorContainer,
|
||||
),
|
||||
);
|
||||
}
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
final homeserverUrl = useTextEditingController();
|
||||
final username = useTextEditingController();
|
||||
final password = useTextEditingController();
|
||||
|
||||
final inputError = useState<String?>(null);
|
||||
final formKey = useRef(GlobalKey<FormState>());
|
||||
|
||||
Future<void> tryLogin() async {
|
||||
isLoading.value = true;
|
||||
|
||||
try {
|
||||
if (formKey.value.currentState?.validate() != true) return;
|
||||
|
||||
final error = await client.login(
|
||||
.new(
|
||||
username: username.text,
|
||||
password: password.text,
|
||||
homeserverUrl: homeserver.origin,
|
||||
),
|
||||
);
|
||||
|
||||
if (error != null) {
|
||||
inputError.value = error;
|
||||
isLoading.value = false;
|
||||
} else {
|
||||
if (context.mounted) Navigator.of(context).pop();
|
||||
}
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: Appbar(),
|
||||
body: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: .new(maxWidth: 600),
|
||||
child: ListView(
|
||||
padding: .symmetric(horizontal: 16, vertical: 64),
|
||||
appBar: Appbar(
|
||||
leading: IconButton(
|
||||
icon: Icon(Icons.arrow_back),
|
||||
onPressed: Navigator.of(context).pop,
|
||||
),
|
||||
),
|
||||
body: AlertDialog(
|
||||
title: Text("Login to ${homeserver.host}"),
|
||||
content: Form(
|
||||
key: formKey.value,
|
||||
child: Column(
|
||||
mainAxisSize: .min,
|
||||
crossAxisAlignment: .start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
SvgPicture.asset("assets/icon.svg", width: 128),
|
||||
SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: .start,
|
||||
children: [
|
||||
Text("Nexus", style: theme.textTheme.displayMedium),
|
||||
Text(
|
||||
"A Simple Matrix Client",
|
||||
style: theme.textTheme.headlineMedium,
|
||||
overflow: .ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
TextFormField(
|
||||
autofocus: true,
|
||||
textInputAction: .next,
|
||||
autovalidateMode: .onUserInteraction,
|
||||
validator: requiredValidator,
|
||||
decoration: .new(label: Text("Username")),
|
||||
controller: username,
|
||||
),
|
||||
Padding(padding: .symmetric(vertical: 12), child: Divider()),
|
||||
|
||||
DividerText("Enter a homeserver domain:"),
|
||||
Row(
|
||||
spacing: 8,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: homeserverUrl,
|
||||
decoration: .new(
|
||||
labelText: "Homeserver URL (e.g. matrix.org)",
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton.filled(
|
||||
tooltip: "Confirm homeserver choice",
|
||||
onPressed: isLoading.value
|
||||
? null
|
||||
: () => setHomeserver(.tryParse(homeserverUrl.text)),
|
||||
icon: Icon(Icons.check),
|
||||
),
|
||||
],
|
||||
SizedBox(height: 12),
|
||||
TextFormField(
|
||||
textInputAction: .done,
|
||||
decoration: .new(
|
||||
label: Text("Password"),
|
||||
errorText: inputError.value,
|
||||
errorMaxLines: 5,
|
||||
),
|
||||
autovalidateMode: .onUserInteraction,
|
||||
validator: requiredValidator,
|
||||
controller: password,
|
||||
obscureText: true,
|
||||
),
|
||||
|
||||
DividerText("Or, choose from some popular homeservers:"),
|
||||
...(<Homeserver>[
|
||||
.new(
|
||||
name: "Matrix.org",
|
||||
description:
|
||||
"The Matrix.org Foundation offers the matrix.org homeserver as an easy entry point for anyone wanting to try out Matrix.",
|
||||
url: .https("matrix.org"),
|
||||
iconUrl:
|
||||
"https://raw.githubusercontent.com/element-hq/logos/refs/heads/master/matrix/matrix-favicon${Theme.brightnessOf(context) == Brightness.dark ? "-white" : ""}.png",
|
||||
),
|
||||
.new(
|
||||
name: "Federated Nexus",
|
||||
description:
|
||||
"Federated Nexus is a community resource hosting multiple FOSS (especially federated) services, including Matrix and Forgejo. By the same developers who made Nexus client.",
|
||||
url: .https("federated.nexus"),
|
||||
iconUrl: "https://federated.nexus/images/icon.png",
|
||||
),
|
||||
.new(
|
||||
name: "Unredacted",
|
||||
description:
|
||||
"Unredacted is a 501(c)(3) non-profit organization that builds Internet infrastructure and services to help people evade censorship and protect their right to privacy.",
|
||||
url: .https("unredacted.org", "services/si/matrix"),
|
||||
iconUrl: "https://unredacted.org/favicon.ico",
|
||||
),
|
||||
].map(
|
||||
(homeserver) => Card(
|
||||
child: ListTile(
|
||||
title: Text(homeserver.name),
|
||||
leading: Image.network(
|
||||
homeserver.iconUrl,
|
||||
errorBuilder: (_, _, _) => SizedBox.shrink(),
|
||||
height: 32,
|
||||
),
|
||||
subtitle: Text(homeserver.description),
|
||||
onTap: isLoading.value
|
||||
? null
|
||||
: () => setHomeserver(homeserver.url),
|
||||
trailing: IconButton(
|
||||
tooltip: "Launch homeserver info page",
|
||||
onPressed: () => launch(homeserver.url),
|
||||
icon: Icon(Icons.info_outline),
|
||||
),
|
||||
),
|
||||
),
|
||||
)),
|
||||
SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: () => launch(.https("servers.joinmatrix.org")),
|
||||
child: Text("See more homeservers..."),
|
||||
),
|
||||
if (isLoading.value)
|
||||
Padding(padding: .only(top: 32), child: Loading())
|
||||
else if (homeserver.value != null) ...[
|
||||
DividerText("Then, sign in:"),
|
||||
SizedBox(height: 4),
|
||||
TextField(
|
||||
decoration: .new(label: Text("Username")),
|
||||
controller: username,
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
TextField(
|
||||
decoration: .new(label: Text("Password")),
|
||||
controller: password,
|
||||
obscureText: true,
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
isLoading.value = true;
|
||||
final error = await client.login(
|
||||
.new(
|
||||
username: username.text,
|
||||
password: password.text,
|
||||
homeserverUrl: homeserver.value!,
|
||||
),
|
||||
);
|
||||
|
||||
if (error != null && context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
.new(
|
||||
content: Text(
|
||||
"Login failed. Is your password right?\nError: $error",
|
||||
style: .new(
|
||||
color: theme.colorScheme.onErrorContainer,
|
||||
),
|
||||
),
|
||||
backgroundColor: theme.colorScheme.errorContainer,
|
||||
),
|
||||
);
|
||||
isLoading.value = false;
|
||||
}
|
||||
},
|
||||
child: Text("Sign In"),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: isLoading.value ? null : tryLogin,
|
||||
child: Text("Sign In"),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
169
lib/pages/select_server_page.dart
Normal file
169
lib/pages/select_server_page.dart
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
import "package:flutter/material.dart";
|
||||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
import "package:flutter_svg/flutter_svg.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/client_controller.dart";
|
||||
import "package:nexus/helpers/launch_helper.dart";
|
||||
import "package:nexus/models/homeserver.dart";
|
||||
import "package:nexus/pages/login_page.dart";
|
||||
import "package:nexus/widgets/appbar.dart";
|
||||
import "package:nexus/widgets/divider_text.dart";
|
||||
|
||||
class SelectServerPage extends HookConsumerWidget {
|
||||
const SelectServerPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
final launch = ref.watch(LaunchHelper.provider).launchUrl;
|
||||
|
||||
final isLoading = useState(false);
|
||||
final homeserverUrl = useTextEditingController();
|
||||
|
||||
Future<void> setHomeserver(Uri? newHomeserver) async {
|
||||
isLoading.value = true;
|
||||
|
||||
try {
|
||||
if (newHomeserver?.hasScheme == false) {
|
||||
newHomeserver = Uri.https(newHomeserver!.path);
|
||||
}
|
||||
|
||||
final newUrl = newHomeserver == null
|
||||
? null
|
||||
: await ref
|
||||
.watch(ClientController.provider.notifier)
|
||||
.discoverHomeserver(newHomeserver);
|
||||
|
||||
if (context.mounted) {
|
||||
if (newUrl == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Homeserver verification failed. Is your homeserver down?",
|
||||
style: .new(color: theme.colorScheme.onErrorContainer),
|
||||
),
|
||||
backgroundColor: theme.colorScheme.errorContainer,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => LoginPage(homeserver: newUrl)),
|
||||
);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: Appbar(),
|
||||
body: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: .new(maxWidth: 600),
|
||||
child: ListView(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
SvgPicture.asset("assets/icon.svg", width: 128),
|
||||
SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: .start,
|
||||
children: [
|
||||
Text("Nexus", style: theme.textTheme.displayMedium),
|
||||
Text(
|
||||
"A Simple Matrix Client",
|
||||
style: theme.textTheme.headlineMedium,
|
||||
overflow: .ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Padding(padding: .symmetric(vertical: 12), child: Divider()),
|
||||
DividerText("Enter a homeserver domain:"),
|
||||
Row(
|
||||
spacing: 8,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
textInputAction: .done,
|
||||
autofocus: true,
|
||||
onSubmitted: (text) => setHomeserver(.tryParse(text)),
|
||||
controller: homeserverUrl,
|
||||
decoration: .new(
|
||||
labelText: "Homeserver URL",
|
||||
hintText: "matrix.org",
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton.filled(
|
||||
tooltip: "Confirm homeserver choice",
|
||||
onPressed: isLoading.value
|
||||
? null
|
||||
: () => setHomeserver(.tryParse(homeserverUrl.text)),
|
||||
icon: Icon(Icons.check),
|
||||
),
|
||||
],
|
||||
),
|
||||
DividerText("Or, choose from some popular homeservers:"),
|
||||
...(<Homeserver>[
|
||||
.new(
|
||||
name: "Matrix.org",
|
||||
description:
|
||||
"The Matrix.org Foundation offers the matrix.org homeserver as an easy entry point for anyone wanting to try out Matrix.",
|
||||
url: .https("matrix.org"),
|
||||
iconUrl:
|
||||
"https://raw.githubusercontent.com/element-hq/logos/refs/heads/master/matrix/matrix-favicon${Theme.brightnessOf(context) == Brightness.dark ? "-white" : ""}.png",
|
||||
),
|
||||
.new(
|
||||
name: "Federated Nexus",
|
||||
description:
|
||||
"Federated Nexus is a community resource hosting multiple FOSS (especially federated) services, including Matrix and Forgejo. By the same developers who made Nexus client.",
|
||||
url: .https("federated.nexus"),
|
||||
iconUrl: "https://federated.nexus/images/icon.png",
|
||||
),
|
||||
.new(
|
||||
name: "Unredacted",
|
||||
description:
|
||||
"Unredacted is a 501(c)(3) non-profit organization that builds Internet infrastructure and services to help people evade censorship and protect their right to privacy.",
|
||||
url: .https("unredacted.org", "services/si/matrix"),
|
||||
iconUrl: "https://unredacted.org/favicon.ico",
|
||||
),
|
||||
].map(
|
||||
(homeserver) => Card(
|
||||
child: ListTile(
|
||||
enabled: !isLoading.value,
|
||||
title: Text(homeserver.name),
|
||||
leading: Image.network(
|
||||
homeserver.iconUrl,
|
||||
errorBuilder: (_, _, _) => SizedBox.shrink(),
|
||||
height: 32,
|
||||
),
|
||||
subtitle: Text(homeserver.description),
|
||||
onTap: isLoading.value
|
||||
? null
|
||||
: () => setHomeserver(homeserver.url),
|
||||
trailing: IconButton(
|
||||
tooltip: "Launch homeserver info page",
|
||||
onPressed: () => launch(homeserver.url),
|
||||
icon: Icon(Icons.info_outline),
|
||||
),
|
||||
),
|
||||
),
|
||||
)),
|
||||
|
||||
TextButton(
|
||||
onPressed: () => launch(.https("servers.joinmatrix.org")),
|
||||
child: Text("See more homeservers..."),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ import "package:flutter_hooks/flutter_hooks.dart";
|
|||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/client_controller.dart";
|
||||
import "package:nexus/widgets/appbar.dart";
|
||||
import "package:nexus/widgets/form_text_input.dart";
|
||||
import "package:nexus/helpers/required_validator_helper.dart";
|
||||
|
||||
class VerifyPage extends HookConsumerWidget {
|
||||
const VerifyPage({super.key});
|
||||
|
|
@ -11,70 +11,56 @@ class VerifyPage extends HookConsumerWidget {
|
|||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final passphraseController = useTextEditingController();
|
||||
final isVerifying = useState(false);
|
||||
final isLoading = useState(false);
|
||||
final inputError = useState<String?>(null);
|
||||
final formKey = useRef(GlobalKey<FormState>());
|
||||
|
||||
return Scaffold(
|
||||
appBar: Appbar(),
|
||||
body: AlertDialog(
|
||||
title: Text("Verify"),
|
||||
content: Column(
|
||||
mainAxisSize: .min,
|
||||
crossAxisAlignment: .start,
|
||||
children: [
|
||||
Text(
|
||||
"Enter your recovery key or passphrase below to unlock encrypted events.\nYour passphrase is usually not the same as your password.",
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
FormTextInput(
|
||||
required: false,
|
||||
autofocus: true,
|
||||
capitalize: true,
|
||||
controller: passphraseController,
|
||||
obscure: true,
|
||||
title: "Recovery Key or Passphrase",
|
||||
),
|
||||
],
|
||||
content: Form(
|
||||
key: formKey.value,
|
||||
child: Column(
|
||||
mainAxisSize: .min,
|
||||
crossAxisAlignment: .start,
|
||||
children: [
|
||||
Text(
|
||||
"Enter your recovery key or passphrase below to unlock encrypted events.\nYour passphrase is usually not the same as your password.",
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
TextFormField(
|
||||
autofocus: true,
|
||||
controller: passphraseController,
|
||||
textInputAction: .done,
|
||||
autovalidateMode: .onUserInteraction,
|
||||
validator: requiredValidator,
|
||||
obscureText: true,
|
||||
decoration: .new(
|
||||
label: Text("Recovery Key or Passphrase"),
|
||||
errorText: inputError.value,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: isVerifying.value
|
||||
onPressed: isLoading.value
|
||||
? null
|
||||
: () async {
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
final snackbar = scaffoldMessenger.showSnackBar(
|
||||
.new(
|
||||
content: Text(
|
||||
"Attempting to verify with recovery key...",
|
||||
),
|
||||
duration: .new(days: 999),
|
||||
),
|
||||
);
|
||||
isLoading.value = true;
|
||||
|
||||
isVerifying.value = true;
|
||||
|
||||
final error = await ref
|
||||
.watch(ClientController.provider.notifier)
|
||||
.verify(passphraseController.text);
|
||||
|
||||
snackbar.close();
|
||||
if (error != null) {
|
||||
isVerifying.value = false;
|
||||
if (context.mounted) {
|
||||
scaffoldMessenger.showSnackBar(
|
||||
.new(
|
||||
backgroundColor: Theme.of(
|
||||
context,
|
||||
).colorScheme.errorContainer,
|
||||
content: Text(
|
||||
"Verification failed. Is your passphrase correct?\nError: $error",
|
||||
style: .new(
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.onErrorContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
try {
|
||||
if (formKey.value.currentState?.validate() != true) {
|
||||
return;
|
||||
}
|
||||
|
||||
inputError.value = await ref
|
||||
.watch(ClientController.provider.notifier)
|
||||
.verify(passphraseController.text);
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
},
|
||||
child: Text("Verify"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue