add login flow
This commit is contained in:
parent
ec40e76178
commit
f307fad074
3 changed files with 84 additions and 64 deletions
|
|
@ -7,8 +7,7 @@ import "package:sqflite_common_ffi/sqflite_ffi.dart";
|
||||||
|
|
||||||
class ClientController extends AsyncNotifier<Client> {
|
class ClientController extends AsyncNotifier<Client> {
|
||||||
@override
|
@override
|
||||||
Future<Client> build() async {
|
Future<Client> build() async => Client(
|
||||||
final client = Client(
|
|
||||||
"nexus",
|
"nexus",
|
||||||
logLevel: kReleaseMode ? Level.warning : Level.verbose,
|
logLevel: kReleaseMode ? Level.warning : Level.verbose,
|
||||||
importantStateEvents: {"im.ponies.room_emotes"},
|
importantStateEvents: {"im.ponies.room_emotes"},
|
||||||
|
|
@ -21,21 +20,31 @@ class ClientController extends AsyncNotifier<Client> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Save info
|
Future<bool> setHomeserver(Uri homeserverUrl) async {
|
||||||
// if (client.homeserver == null) {
|
final client = await future;
|
||||||
// await client.checkHomeserver(Uri.https("federated.nexus"));
|
try {
|
||||||
// }
|
await client.checkHomeserver(homeserverUrl);
|
||||||
// if (client.accessToken == null) {
|
return true;
|
||||||
// await client.login(
|
} catch (_) {
|
||||||
// LoginType.mLoginPassword,
|
return false;
|
||||||
// initialDeviceDisplayName: "Nexus Client",
|
}
|
||||||
// deviceId: "temp", // TODO
|
}
|
||||||
// identifier: AuthenticationUserIdentifier(user: "quadradical"),
|
|
||||||
// password: File("./password.txt").readAsStringSync(),
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
return client;
|
Future<bool> login(String username, String password) async {
|
||||||
|
final client = await future;
|
||||||
|
try {
|
||||||
|
await client.login(
|
||||||
|
LoginType.mLoginPassword,
|
||||||
|
initialDeviceDisplayName:
|
||||||
|
"Nexus Client login at ${DateTime.now().toIso8601String()}",
|
||||||
|
identifier: AuthenticationUserIdentifier(user: username),
|
||||||
|
password: password,
|
||||||
|
);
|
||||||
|
//TODO: refresh
|
||||||
|
return true;
|
||||||
|
} catch (_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final provider = AsyncNotifierProvider<ClientController, Client>(
|
static final provider = AsyncNotifierProvider<ClientController, Client>(
|
||||||
|
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
|
||||||
import "package:nexus/controllers/client_controller.dart";
|
|
||||||
|
|
||||||
class LoginHelper {
|
|
||||||
final Ref ref;
|
|
||||||
LoginHelper(this.ref);
|
|
||||||
|
|
||||||
Future<bool> setHomeserver(Uri homeserverUrl) async {
|
|
||||||
final client = await ref.watch(ClientController.provider.future);
|
|
||||||
try {
|
|
||||||
await client.checkHomeserver(homeserverUrl);
|
|
||||||
return true;
|
|
||||||
} catch (_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static final provider = Provider<LoginHelper>(LoginHelper.new);
|
|
||||||
}
|
|
||||||
|
|
@ -2,7 +2,7 @@ import "package:flutter/material.dart";
|
||||||
import "package:flutter_hooks/flutter_hooks.dart";
|
import "package:flutter_hooks/flutter_hooks.dart";
|
||||||
import "package:flutter_svg/flutter_svg.dart";
|
import "package:flutter_svg/flutter_svg.dart";
|
||||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||||
import "package:nexus/helpers/login_helper.dart";
|
import "package:nexus/controllers/client_controller.dart";
|
||||||
import "package:nexus/helpers/launch_helper.dart";
|
import "package:nexus/helpers/launch_helper.dart";
|
||||||
import "package:nexus/models/homeserver.dart";
|
import "package:nexus/models/homeserver.dart";
|
||||||
import "package:nexus/widgets/appbar.dart";
|
import "package:nexus/widgets/appbar.dart";
|
||||||
|
|
@ -14,14 +14,17 @@ class LoginPage extends HookConsumerWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final isChecking = useState(false);
|
final theme = Theme.of(context);
|
||||||
|
|
||||||
|
final isLoading = useState(false);
|
||||||
final allowLogin = useState(false);
|
final allowLogin = useState(false);
|
||||||
|
|
||||||
Future<void> setHomeserver(Uri? homeserver) async {
|
Future<void> setHomeserver(Uri? homeserver) async {
|
||||||
isChecking.value = true;
|
isLoading.value = true;
|
||||||
final succeeded = homeserver == null
|
final succeeded = homeserver == null
|
||||||
? false
|
? false
|
||||||
: await ref
|
: await ref
|
||||||
.watch(LoginHelper.provider)
|
.watch(ClientController.provider.notifier)
|
||||||
.setHomeserver(
|
.setHomeserver(
|
||||||
homeserver.hasScheme
|
homeserver.hasScheme
|
||||||
? homeserver
|
? homeserver
|
||||||
|
|
@ -35,18 +38,18 @@ class LoginPage extends HookConsumerWidget {
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text(
|
content: Text(
|
||||||
"Homeserver verification failed. Is your homeserver down?",
|
"Homeserver verification failed. Is your homeserver down?",
|
||||||
style: TextStyle(
|
style: TextStyle(color: theme.colorScheme.onErrorContainer),
|
||||||
color: Theme.of(context).colorScheme.onErrorContainer,
|
|
||||||
),
|
),
|
||||||
),
|
backgroundColor: theme.colorScheme.errorContainer,
|
||||||
backgroundColor: Theme.of(context).colorScheme.errorContainer,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
isChecking.value = false;
|
isLoading.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final homeserverUrl = useTextEditingController();
|
final homeserverUrl = useTextEditingController();
|
||||||
|
final username = useTextEditingController();
|
||||||
|
final password = useTextEditingController();
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: Appbar(),
|
appBar: Appbar(),
|
||||||
|
|
@ -64,13 +67,10 @@ class LoginPage extends HookConsumerWidget {
|
||||||
Column(
|
Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text("Nexus", style: theme.textTheme.displayMedium),
|
||||||
"Nexus",
|
|
||||||
style: Theme.of(context).textTheme.displayMedium,
|
|
||||||
),
|
|
||||||
Text(
|
Text(
|
||||||
"A Simple Matrix Client",
|
"A Simple Matrix Client",
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
style: theme.textTheme.headlineMedium,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
@ -94,7 +94,7 @@ class LoginPage extends HookConsumerWidget {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
IconButton.filled(
|
IconButton.filled(
|
||||||
onPressed: isChecking.value
|
onPressed: isLoading.value
|
||||||
? null
|
? null
|
||||||
: () => setHomeserver(Uri.tryParse(homeserverUrl.text)),
|
: () => setHomeserver(Uri.tryParse(homeserverUrl.text)),
|
||||||
icon: Icon(Icons.check),
|
icon: Icon(Icons.check),
|
||||||
|
|
@ -132,7 +132,7 @@ class LoginPage extends HookConsumerWidget {
|
||||||
title: Text(homeserver.name),
|
title: Text(homeserver.name),
|
||||||
leading: Image.network(homeserver.iconUrl, height: 32),
|
leading: Image.network(homeserver.iconUrl, height: 32),
|
||||||
subtitle: Text(homeserver.description),
|
subtitle: Text(homeserver.description),
|
||||||
onTap: isChecking.value
|
onTap: isLoading.value
|
||||||
? null
|
? null
|
||||||
: () => setHomeserver(homeserver.url),
|
: () => setHomeserver(homeserver.url),
|
||||||
trailing: IconButton(
|
trailing: IconButton(
|
||||||
|
|
@ -151,16 +151,46 @@ class LoginPage extends HookConsumerWidget {
|
||||||
.launchUrl(Uri.https("servers.joinmatrix.org")),
|
.launchUrl(Uri.https("servers.joinmatrix.org")),
|
||||||
child: Text("See more homeservers..."),
|
child: Text("See more homeservers..."),
|
||||||
),
|
),
|
||||||
if (isChecking.value)
|
if (isLoading.value)
|
||||||
Padding(padding: EdgeInsets.only(top: 32), child: Loading())
|
Padding(padding: EdgeInsets.only(top: 32), child: Loading())
|
||||||
else if (allowLogin.value) ...[
|
else if (allowLogin.value) ...[
|
||||||
DividerText("Then, sign in:"),
|
DividerText("Then, sign in:"),
|
||||||
SizedBox(height: 4),
|
SizedBox(height: 4),
|
||||||
TextField(decoration: InputDecoration(label: Text("Username"))),
|
TextField(
|
||||||
|
decoration: InputDecoration(label: Text("Username")),
|
||||||
|
controller: username,
|
||||||
|
),
|
||||||
SizedBox(height: 12),
|
SizedBox(height: 12),
|
||||||
TextField(decoration: InputDecoration(label: Text("Password"))),
|
TextField(
|
||||||
|
decoration: InputDecoration(label: Text("Password")),
|
||||||
|
controller: password,
|
||||||
|
obscureText: true,
|
||||||
|
),
|
||||||
SizedBox(height: 12),
|
SizedBox(height: 12),
|
||||||
ElevatedButton(onPressed: () {}, child: Text("Sign in")),
|
ElevatedButton(
|
||||||
|
onPressed: () async {
|
||||||
|
isLoading.value = true;
|
||||||
|
final succeeded = await ref
|
||||||
|
.watch(ClientController.provider.notifier)
|
||||||
|
.login(username.text, password.text);
|
||||||
|
|
||||||
|
if (!succeeded && context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
"Login failed. Is your password right?",
|
||||||
|
style: TextStyle(
|
||||||
|
color: theme.colorScheme.onErrorContainer,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
backgroundColor: theme.colorScheme.errorContainer,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
isLoading.value = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Text("Sign In"),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue