feat: Nicer registration form
This commit is contained in:
parent
d3a54c8911
commit
661b740f57
6 changed files with 241 additions and 119 deletions
|
|
@ -5,6 +5,8 @@ import 'package:flutter/material.dart';
|
|||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'uia_request_manager.dart';
|
||||
|
||||
extension LocalizedExceptionExtension on Object {
|
||||
String toLocalizedString(BuildContext context) {
|
||||
if (this is MatrixException) {
|
||||
|
|
@ -48,6 +50,7 @@ extension LocalizedExceptionExtension on Object {
|
|||
if (this is MatrixConnectionException || this is SocketException) {
|
||||
return L10n.of(context).noConnectionToTheServer;
|
||||
}
|
||||
if (this is UiaException) return toString();
|
||||
Logs().w('Something went wrong: ', this);
|
||||
return L10n.of(context).oopsSomethingWentWrong;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,24 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
|
||||
extension UiaRequestManager on MatrixState {
|
||||
Future uiaRequestHandler(UiaRequest uiaRequest) async {
|
||||
try {
|
||||
if (uiaRequest.state != UiaRequestState.waitForUser ||
|
||||
uiaRequest.nextStages.isEmpty) return;
|
||||
uiaRequest.nextStages.isEmpty) {
|
||||
Logs().d('Uia Request Stage: ${uiaRequest.state}');
|
||||
return;
|
||||
}
|
||||
final stage = uiaRequest.nextStages.first;
|
||||
Logs().d('Uia Request Stage: $stage');
|
||||
switch (stage) {
|
||||
case AuthenticationTypes.password:
|
||||
final input = cachedPassword ??
|
||||
|
|
@ -31,7 +37,9 @@ extension UiaRequestManager on MatrixState {
|
|||
],
|
||||
))
|
||||
?.single;
|
||||
if (input?.isEmpty ?? true) return;
|
||||
if (input?.isEmpty ?? true) {
|
||||
return uiaRequest.cancel();
|
||||
}
|
||||
return uiaRequest.completeStage(
|
||||
AuthenticationPassword(
|
||||
session: uiaRequest.session,
|
||||
|
|
@ -40,35 +48,18 @@ extension UiaRequestManager on MatrixState {
|
|||
),
|
||||
);
|
||||
case AuthenticationTypes.emailIdentity:
|
||||
final emailInput = await showTextInputDialog(
|
||||
context: navigatorContext,
|
||||
message: L10n.of(context).serverRequiresEmail,
|
||||
okLabel: L10n.of(context).next,
|
||||
cancelLabel: L10n.of(context).cancel,
|
||||
textFields: [
|
||||
DialogTextField(
|
||||
hintText: L10n.of(context).addEmail,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
),
|
||||
],
|
||||
);
|
||||
if (emailInput == null || emailInput.isEmpty) {
|
||||
return uiaRequest
|
||||
.cancel(Exception(L10n.of(context).serverRequiresEmail));
|
||||
if (currentThreepidCreds == null || currentClientSecret == null) {
|
||||
return uiaRequest.cancel(
|
||||
UiaException(L10n.of(widget.context).serverRequiresEmail),
|
||||
);
|
||||
}
|
||||
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
|
||||
final currentThreepidCreds = await client.requestTokenToRegisterEmail(
|
||||
clientSecret,
|
||||
emailInput.single,
|
||||
0,
|
||||
);
|
||||
final auth = AuthenticationThreePidCreds(
|
||||
session: uiaRequest.session,
|
||||
type: AuthenticationTypes.emailIdentity,
|
||||
threepidCreds: [
|
||||
ThreepidCreds(
|
||||
sid: currentThreepidCreds.sid,
|
||||
clientSecret: clientSecret,
|
||||
clientSecret: currentClientSecret,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
|
@ -92,24 +83,41 @@ extension UiaRequestManager on MatrixState {
|
|||
),
|
||||
);
|
||||
default:
|
||||
await launch(
|
||||
client.homeserver.toString() +
|
||||
'/_matrix/client/r0/auth/$stage/fallback/web?session=${uiaRequest.session}',
|
||||
);
|
||||
if (OkCancelResult.ok ==
|
||||
await showOkCancelAlertDialog(
|
||||
useRootNavigator: false,
|
||||
message: L10n.of(context).pleaseFollowInstructionsOnWeb,
|
||||
context: navigatorContext,
|
||||
okLabel: L10n.of(context).next,
|
||||
cancelLabel: L10n.of(context).cancel,
|
||||
)) {
|
||||
return uiaRequest.completeStage(
|
||||
AuthenticationData(session: uiaRequest.session),
|
||||
final url = Uri.parse(client.homeserver.toString() +
|
||||
'/_matrix/client/r0/auth/$stage/fallback/web?session=${uiaRequest.session}');
|
||||
if (PlatformInfos.isMobile) {
|
||||
final browser = UiaFallbackBrowser();
|
||||
browser.addMenuItem(
|
||||
ChromeSafariBrowserMenuItem(
|
||||
action: (_, __) {
|
||||
uiaRequest.cancel();
|
||||
},
|
||||
label: L10n.of(context).cancel,
|
||||
id: 0,
|
||||
),
|
||||
);
|
||||
await browser.open(url: url);
|
||||
await browser.whenClosed.stream.first;
|
||||
} else {
|
||||
return uiaRequest.cancel();
|
||||
launch(url.toString());
|
||||
if (OkCancelResult.ok ==
|
||||
await showOkCancelAlertDialog(
|
||||
useRootNavigator: false,
|
||||
message: L10n.of(context).pleaseFollowInstructionsOnWeb,
|
||||
context: navigatorContext,
|
||||
okLabel: L10n.of(context).next,
|
||||
cancelLabel: L10n.of(context).cancel,
|
||||
)) {
|
||||
return uiaRequest.completeStage(
|
||||
AuthenticationData(session: uiaRequest.session),
|
||||
);
|
||||
} else {
|
||||
return uiaRequest.cancel();
|
||||
}
|
||||
}
|
||||
await uiaRequest.completeStage(
|
||||
AuthenticationData(session: uiaRequest.session),
|
||||
);
|
||||
}
|
||||
} catch (e, s) {
|
||||
Logs().e('Error while background UIA', e, s);
|
||||
|
|
@ -117,3 +125,19 @@ extension UiaRequestManager on MatrixState {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
class UiaException implements Exception {
|
||||
final String reason;
|
||||
|
||||
UiaException(this.reason);
|
||||
|
||||
@override
|
||||
String toString() => reason;
|
||||
}
|
||||
|
||||
class UiaFallbackBrowser extends ChromeSafariBrowser {
|
||||
final StreamController<bool> whenClosed = StreamController<bool>.broadcast();
|
||||
|
||||
@override
|
||||
onClosed() => whenClosed.add(true);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue