feat: Multi account login working

This commit is contained in:
Christian Pauly 2021-09-18 12:50:02 +02:00
parent 5eb31c7a80
commit a2d37102a4
8 changed files with 41 additions and 38 deletions

View File

@ -47,13 +47,13 @@ class HomeserverPickerController extends State<HomeserverPicker> {
showFutureLoadingDialog(
context: context,
future: () async {
if (Matrix.of(context).client.homeserver == null) {
await Matrix.of(context).client.checkHomeserver(
if (Matrix.of(context).getLoginClient().homeserver == null) {
await Matrix.of(context).getLoginClient().checkHomeserver(
await Store()
.getItem(HomeserverPickerController.ssoHomeserverKey),
);
}
await Matrix.of(context).createLoginClient().login(
await Matrix.of(context).getLoginClient().login(
LoginType.mLoginToken,
token: token,
initialDeviceDisplayName: PlatformInfos.clientName,
@ -117,7 +117,7 @@ class HomeserverPickerController extends State<HomeserverPicker> {
isLoading = true;
});
final wellKnown =
await Matrix.of(context).client.checkHomeserver(homeserver);
await Matrix.of(context).getLoginClient().checkHomeserver(homeserver);
var jitsi = wellKnown?.additionalProperties
?.tryGet<Map<String, dynamic>>('im.vector.riot.jitsi')
@ -177,13 +177,13 @@ class HomeserverPickerController extends State<HomeserverPicker> {
.any((flow) => flow['type'] == AuthenticationTypes.sso);
Future<Map<String, dynamic>> getLoginTypes() async {
_rawLoginTypes ??= await Matrix.of(context).client.request(
_rawLoginTypes ??= await Matrix.of(context).getLoginClient().request(
RequestType.GET,
'/client/r0/login',
);
if (registrationSupported == null) {
try {
await Matrix.of(context).client.register();
await Matrix.of(context).getLoginClient().register();
registrationSupported = true;
} on MatrixException catch (e) {
registrationSupported = e.requireAdditionalAuthentication ?? false;
@ -200,14 +200,14 @@ class HomeserverPickerController extends State<HomeserverPicker> {
if (kIsWeb) {
// We store the homserver in the local storage instead of a redirect
// parameter because of possible CSRF attacks.
Store().setItem(
ssoHomeserverKey, Matrix.of(context).client.homeserver.toString());
Store().setItem(ssoHomeserverKey,
Matrix.of(context).getLoginClient().homeserver.toString());
}
final redirectUrl = kIsWeb
? AppConfig.webBaseUrl + '/#/'
: AppConfig.appOpenUrlScheme.toLowerCase() + '://login';
final url =
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect/${Uri.encodeComponent(id)}?redirectUrl=${Uri.encodeQueryComponent(redirectUrl)}';
'${Matrix.of(context).getLoginClient().homeserver?.toString()}/_matrix/client/r0/login/sso/redirect/${Uri.encodeComponent(id)}?redirectUrl=${Uri.encodeQueryComponent(redirectUrl)}';
if (PlatformInfos.isMobile) {
browser ??= ChromeSafariBrowser();
browser.open(url: Uri.parse(url));

View File

@ -64,7 +64,7 @@ class LoginController extends State<Login> {
} else {
identifier = AuthenticationUserIdentifier(user: username);
}
await matrix.createLoginClient().login(LoginType.mLoginPassword,
await matrix.getLoginClient().login(LoginType.mLoginPassword,
identifier: identifier,
// To stay compatible with older server versions
// ignore: deprecated_member_use
@ -98,12 +98,13 @@ class LoginController extends State<Login> {
setState(() => usernameError = null);
if (!userId.isValidMatrixId) return;
try {
final oldHomeserver = Matrix.of(context).client.homeserver;
final oldHomeserver = Matrix.of(context).getLoginClient().homeserver;
var newDomain = Uri.https(userId.domain, '');
Matrix.of(context).client.homeserver = newDomain;
Matrix.of(context).getLoginClient().homeserver = newDomain;
DiscoveryInformation wellKnownInformation;
try {
wellKnownInformation = await Matrix.of(context).client.getWellknown();
wellKnownInformation =
await Matrix.of(context).getLoginClient().getWellknown();
if (wellKnownInformation.mHomeserver?.baseUrl?.toString()?.isNotEmpty ??
false) {
newDomain = wellKnownInformation.mHomeserver.baseUrl;
@ -120,8 +121,8 @@ class LoginController extends State<Login> {
.checkHomeserver(newDomain)
.catchError((e) => null),
);
if (Matrix.of(context).client.homeserver == null) {
Matrix.of(context).client.homeserver = oldHomeserver;
if (Matrix.of(context).getLoginClient().homeserver == null) {
Matrix.of(context).getLoginClient().homeserver = oldHomeserver;
// okay, the server we checked does not appear to be a matrix server
Logs().v(
'$newDomain is not running a homeserver, asking to use $oldHomeserver');
@ -177,11 +178,12 @@ class LoginController extends State<Login> {
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
final response = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.requestTokenToResetPasswordEmail(
clientSecret,
input.single,
sendAttempt++,
),
future: () =>
Matrix.of(context).getLoginClient().requestTokenToResetPasswordEmail(
clientSecret,
input.single,
sendAttempt++,
),
);
if (response.error != null) return;
final ok = await showOkAlertDialog(
@ -210,7 +212,7 @@ class LoginController extends State<Login> {
if (password == null) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.changePassword(
future: () => Matrix.of(context).getLoginClient().changePassword(
password.single,
auth: AuthenticationThreePidCreds(
type: AuthenticationTypes.emailIdentity,

View File

@ -37,7 +37,7 @@ class SignupPageController extends State<SignupPage> {
setState(() => loading = true);
try {
final client = Matrix.of(context).createLoginClient();
final client = Matrix.of(context).getLoginClient();
await client.uiaRequestBackground(
(auth) => client.register(
username: usernameController.text,

View File

@ -100,7 +100,7 @@ class HomeserverPickerView extends StatelessWidget {
imageUrl: Uri.parse(
identityProvider.icon)
.getDownloadLink(
Matrix.of(context).client)
Matrix.of(context).getLoginClient())
.toString(),
width: 24,
height: 24,

View File

@ -19,7 +19,7 @@ class LoginView extends StatelessWidget {
elevation: 0,
title: Text(
L10n.of(context).logInTo(Matrix.of(context)
.client
.getLoginClient()
.homeserver
.toString()
.replaceFirst('https://', '')),

View File

@ -38,7 +38,7 @@ class SignupPageView extends StatelessWidget {
labelText: L10n.of(context).username,
prefixText: '@',
suffixText:
':${Matrix.of(context).client.homeserver.host}'),
':${Matrix.of(context).getLoginClient().homeserver.host}'),
),
),
Divider(),

View File

@ -11,7 +11,7 @@ import 'matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart';
abstract class ClientManager {
static const String clientNamespace = 'im.fluffychat.store.clients';
static Future<List<Client>> getClients() async {
final clientNames = <String>{PlatformInfos.clientName};
final clientNames = <String>{};
try {
final rawClientNames = await Store().getItem(clientNamespace);
if (rawClientNames != null) {
@ -23,6 +23,7 @@ abstract class ClientManager {
Logs().w('Client names in store are corrupted', e, s);
await Store().deleteItem(clientNamespace);
}
if (clientNames.isEmpty) clientNames.add(PlatformInfos.clientName);
return clientNames.map(createClient).toList();
}

View File

@ -78,23 +78,23 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
return _activeClient;
}
Client createLoginClient() {
Client _loginClientCandidate;
Client getLoginClient() {
final multiAccount = client.isLogged();
final newClient = multiAccount
? ClientManager.createClient(client.generateUniqueTransactionId())
: client;
if (multiAccount) {
// Add to client list
newClient.onLoginStateChanged.stream
if (!multiAccount) return client;
_loginClientCandidate ??= ClientManager.createClient(
client.generateUniqueTransactionId())
..onLoginStateChanged
.stream
.where((l) => l == LoginState.loggedIn)
.first
.then((_) {
widget.clients.add(client);
ClientManager.addClientNameToStore(client.clientName);
_registerSubs(client.clientName);
widget.clients.add(_loginClientCandidate);
ClientManager.addClientNameToStore(_loginClientCandidate.clientName);
_registerSubs(_loginClientCandidate.clientName);
});
}
return newClient;
return _loginClientCandidate;
}
Client getClientByName(String name) => widget.clients