2021-05-22 10:25:37 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-04-12 17:31:53 +02:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2021-05-22 09:08:23 +02:00
|
|
|
import 'package:fluffychat/pages/views/sign_up_view.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-05-22 08:53:52 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-04-12 17:31:53 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-04-12 17:31:53 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:uni_links/uni_links.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
import 'package:universal_html/html.dart' as html;
|
2021-05-23 13:11:55 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
|
|
|
|
import '../main.dart';
|
2021-04-12 17:31:53 +02:00
|
|
|
|
|
|
|
class SignUp extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
SignUpController createState() => SignUpController();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SignUpController extends State<SignUp> {
|
2021-06-06 16:55:31 +02:00
|
|
|
Map<String, dynamic> _rawLoginTypes;
|
|
|
|
bool registrationSupported;
|
2021-05-22 10:25:37 +02:00
|
|
|
StreamSubscription _intentDataStreamSubscription;
|
2021-06-06 16:55:31 +02:00
|
|
|
List<IdentityProvider> get identityProviders {
|
|
|
|
if (!ssoLoginSupported) return [];
|
|
|
|
final rawProviders = _rawLoginTypes.tryGetList('flows').singleWhere(
|
|
|
|
(flow) =>
|
|
|
|
flow['type'] == AuthenticationTypes.sso)['identity_providers'];
|
|
|
|
return (rawProviders as List)
|
|
|
|
.map((json) => IdentityProvider.fromJson(json))
|
|
|
|
.toList();
|
|
|
|
}
|
2021-05-22 10:25:37 +02:00
|
|
|
|
|
|
|
void _loginWithToken(String token) {
|
|
|
|
if (token?.isEmpty ?? true) return;
|
|
|
|
showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => Matrix.of(context).client.login(
|
|
|
|
type: AuthenticationTypes.token,
|
|
|
|
token: token,
|
|
|
|
initialDeviceDisplayName: PlatformInfos.clientName,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _processIncomingUris(String text) async {
|
|
|
|
if (text == null || !text.startsWith(AppConfig.appOpenUrlScheme)) return;
|
2021-05-23 13:11:55 +02:00
|
|
|
VRouter.of(context).push('/home');
|
2021-05-22 10:25:37 +02:00
|
|
|
final token = Uri.parse(text).queryParameters['loginToken'];
|
|
|
|
if (token != null) _loginWithToken(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _initReceiveUri() {
|
|
|
|
if (!PlatformInfos.isMobile) return;
|
|
|
|
// For receiving shared Uris
|
|
|
|
_intentDataStreamSubscription = linkStream.listen(_processIncomingUris);
|
|
|
|
if (FluffyChatApp.gotInitialLink == false) {
|
|
|
|
FluffyChatApp.gotInitialLink = true;
|
|
|
|
getInitialLink().then(_processIncomingUris);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_initReceiveUri();
|
|
|
|
if (kIsWeb) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
final token =
|
|
|
|
Uri.parse(html.window.location.href).queryParameters['loginToken'];
|
|
|
|
_loginWithToken(token);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
_intentDataStreamSubscription?.cancel();
|
|
|
|
}
|
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
bool get passwordLoginSupported =>
|
|
|
|
Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.supportedLoginTypes
|
|
|
|
.contains(AuthenticationTypes.password) &&
|
|
|
|
_rawLoginTypes
|
|
|
|
.tryGetList('flows')
|
|
|
|
.any((flow) => flow['type'] == AuthenticationTypes.password);
|
2021-05-22 10:25:37 +02:00
|
|
|
|
|
|
|
bool get ssoLoginSupported =>
|
2021-06-06 16:55:31 +02:00
|
|
|
Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.supportedLoginTypes
|
|
|
|
.contains(AuthenticationTypes.sso) &&
|
|
|
|
_rawLoginTypes
|
|
|
|
.tryGetList('flows')
|
|
|
|
.any((flow) => flow['type'] == AuthenticationTypes.sso);
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>> getLoginTypes() async {
|
|
|
|
_rawLoginTypes ??= await Matrix.of(context).client.request(
|
|
|
|
RequestType.GET,
|
|
|
|
'/client/r0/login',
|
|
|
|
);
|
|
|
|
if (registrationSupported == null) {
|
|
|
|
try {
|
|
|
|
await Matrix.of(context).client.register();
|
|
|
|
registrationSupported = true;
|
|
|
|
} on MatrixException catch (e) {
|
|
|
|
registrationSupported = e.requireAdditionalAuthentication ?? false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _rawLoginTypes;
|
2021-05-22 10:25:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
void ssoLoginAction(String id) {
|
2021-05-22 10:25:37 +02:00
|
|
|
final redirectUrl = kIsWeb
|
|
|
|
? html.window.location.href
|
|
|
|
: AppConfig.appOpenUrlScheme.toLowerCase() + '://sso';
|
|
|
|
launch(
|
2021-06-06 16:55:31 +02:00
|
|
|
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect/${Uri.encodeComponent(id)}?redirectUrl=${Uri.encodeQueryComponent(redirectUrl)}');
|
2021-05-22 10:25:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
void signUpAction() => launch(
|
|
|
|
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/static/client/register');
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => SignUpView(this);
|
|
|
|
}
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
class IdentityProvider {
|
|
|
|
final String id;
|
|
|
|
final String name;
|
|
|
|
final String icon;
|
|
|
|
final String brand;
|
2021-05-23 13:11:55 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
IdentityProvider({this.id, this.name, this.icon, this.brand});
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
factory IdentityProvider.fromJson(Map<String, dynamic> json) =>
|
|
|
|
IdentityProvider(
|
|
|
|
id: json['id'],
|
|
|
|
name: json['name'],
|
|
|
|
icon: json['icon'],
|
|
|
|
brand: json['brand'],
|
|
|
|
);
|
2021-04-12 17:31:53 +02:00
|
|
|
}
|