feat: One page login
parent
535ee062dd
commit
a6000ddf60
@ -1,106 +0,0 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:famedlysdk/famedlysdk.dart';
|
|
||||||
import 'package:fluffychat/config/app_config.dart';
|
|
||||||
import 'package:fluffychat/pages/views/sign_up_view.dart';
|
|
||||||
import 'package:fluffychat/utils/famedlysdk_store.dart';
|
|
||||||
|
|
||||||
import 'package:fluffychat/widgets/matrix.dart';
|
|
||||||
import 'package:flutter/cupertino.dart';
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
|
||||||
import 'package:universal_html/html.dart' as html;
|
|
||||||
|
|
||||||
class SignUp extends StatefulWidget {
|
|
||||||
@override
|
|
||||||
SignUpController createState() => SignUpController();
|
|
||||||
}
|
|
||||||
|
|
||||||
class SignUpController extends State<SignUp> {
|
|
||||||
Map<String, dynamic> _rawLoginTypes;
|
|
||||||
bool registrationSupported;
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool get passwordLoginSupported =>
|
|
||||||
Matrix.of(context)
|
|
||||||
.client
|
|
||||||
.supportedLoginTypes
|
|
||||||
.contains(AuthenticationTypes.password) &&
|
|
||||||
_rawLoginTypes
|
|
||||||
.tryGetList('flows')
|
|
||||||
.any((flow) => flow['type'] == AuthenticationTypes.password);
|
|
||||||
|
|
||||||
bool get ssoLoginSupported =>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const String ssoHomeserverKey = 'sso-homeserver';
|
|
||||||
|
|
||||||
void ssoLoginAction(String id) {
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
final redirectUrl = kIsWeb
|
|
||||||
? html.window.location.href
|
|
||||||
: AppConfig.appOpenUrlScheme.toLowerCase() + '://sso';
|
|
||||||
launch(
|
|
||||||
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect/${Uri.encodeComponent(id)}?redirectUrl=${Uri.encodeQueryComponent(redirectUrl)}');
|
|
||||||
}
|
|
||||||
|
|
||||||
void signUpAction() => launch(
|
|
||||||
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/static/client/register');
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) => SignUpView(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
class IdentityProvider {
|
|
||||||
final String id;
|
|
||||||
final String name;
|
|
||||||
final String icon;
|
|
||||||
final String brand;
|
|
||||||
|
|
||||||
IdentityProvider({this.id, this.name, this.icon, this.brand});
|
|
||||||
|
|
||||||
factory IdentityProvider.fromJson(Map<String, dynamic> json) =>
|
|
||||||
IdentityProvider(
|
|
||||||
id: json['id'],
|
|
||||||
name: json['name'],
|
|
||||||
icon: json['icon'],
|
|
||||||
brand: json['brand'],
|
|
||||||
);
|
|
||||||
}
|
|
@ -1,136 +0,0 @@
|
|||||||
import 'package:cached_network_image/cached_network_image.dart';
|
|
||||||
import 'package:vrouter/vrouter.dart';
|
|
||||||
import 'package:fluffychat/pages/sign_up.dart';
|
|
||||||
import 'package:fluffychat/widgets/fluffy_banner.dart';
|
|
||||||
|
|
||||||
import 'package:fluffychat/widgets/matrix.dart';
|
|
||||||
import 'package:fluffychat/widgets/layouts/one_page_card.dart';
|
|
||||||
import 'package:flutter/cupertino.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
||||||
import '../../utils/localized_exception_extension.dart';
|
|
||||||
|
|
||||||
import 'package:famedlysdk/famedlysdk.dart';
|
|
||||||
|
|
||||||
class SignUpView extends StatelessWidget {
|
|
||||||
final SignUpController controller;
|
|
||||||
|
|
||||||
const SignUpView(this.controller, {Key key}) : super(key: key);
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return OnePageCard(
|
|
||||||
child: Scaffold(
|
|
||||||
appBar: AppBar(
|
|
||||||
elevation: 0,
|
|
||||||
title: Text(
|
|
||||||
Matrix.of(context)
|
|
||||||
.client
|
|
||||||
.homeserver
|
|
||||||
.toString()
|
|
||||||
.replaceFirst('https://', ''),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
body: FutureBuilder(
|
|
||||||
future: controller.getLoginTypes(),
|
|
||||||
builder: (context, snapshot) {
|
|
||||||
if (snapshot.hasError) {
|
|
||||||
return Center(
|
|
||||||
child: Text(
|
|
||||||
snapshot.error.toLocalizedString(context),
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!snapshot.hasData) {
|
|
||||||
return Center(child: CircularProgressIndicator());
|
|
||||||
}
|
|
||||||
return ListView(children: <Widget>[
|
|
||||||
Hero(
|
|
||||||
tag: 'loginBanner',
|
|
||||||
child: FluffyBanner(),
|
|
||||||
),
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.all(12.0),
|
|
||||||
child: Column(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
||||||
children: [
|
|
||||||
if (controller.ssoLoginSupported) ...{
|
|
||||||
for (final identityProvider
|
|
||||||
in controller.identityProviders)
|
|
||||||
OutlinedButton.icon(
|
|
||||||
onPressed: () =>
|
|
||||||
controller.ssoLoginAction(identityProvider.id),
|
|
||||||
icon: identityProvider.icon == null
|
|
||||||
? Icon(Icons.web_outlined)
|
|
||||||
: CachedNetworkImage(
|
|
||||||
imageUrl: Uri.parse(identityProvider.icon)
|
|
||||||
.getDownloadLink(
|
|
||||||
Matrix.of(context).client)
|
|
||||||
.toString(),
|
|
||||||
width: 24,
|
|
||||||
height: 24,
|
|
||||||
),
|
|
||||||
label: Text(L10n.of(context).loginWith(
|
|
||||||
identityProvider.brand ??
|
|
||||||
identityProvider.name ??
|
|
||||||
L10n.of(context).singlesignon)),
|
|
||||||
),
|
|
||||||
if (controller.registrationSupported ||
|
|
||||||
controller.passwordLoginSupported)
|
|
||||||
Row(children: [
|
|
||||||
Expanded(child: Divider()),
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.all(12.0),
|
|
||||||
child: Text(L10n.of(context).or),
|
|
||||||
),
|
|
||||||
Expanded(child: Divider()),
|
|
||||||
]),
|
|
||||||
},
|
|
||||||
Row(
|
|
||||||
children: [
|
|
||||||
if (controller.passwordLoginSupported)
|
|
||||||
Expanded(
|
|
||||||
child: Container(
|
|
||||||
height: 64,
|
|
||||||
child: OutlinedButton.icon(
|
|
||||||
onPressed: () =>
|
|
||||||
context.vRouter.push('/login'),
|
|
||||||
icon: Icon(Icons.login_outlined),
|
|
||||||
label: Text(L10n.of(context).login),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
if (controller.registrationSupported &&
|
|
||||||
controller.passwordLoginSupported)
|
|
||||||
SizedBox(width: 12),
|
|
||||||
if (controller.registrationSupported)
|
|
||||||
Expanded(
|
|
||||||
child: Container(
|
|
||||||
height: 64,
|
|
||||||
child: OutlinedButton.icon(
|
|
||||||
onPressed: controller.signUpAction,
|
|
||||||
icon: Icon(Icons.add_box_outlined),
|
|
||||||
label: Text(L10n.of(context).register),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
]
|
|
||||||
.map(
|
|
||||||
(widget) => Container(
|
|
||||||
height: 64,
|
|
||||||
padding: EdgeInsets.only(bottom: 12),
|
|
||||||
child: widget),
|
|
||||||
)
|
|
||||||
.toList(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
import 'package:fluffychat/pages/sign_up.dart';
|
|
||||||
import 'package:fluffychat/main.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
testWidgets('Test if the widget can be created', (WidgetTester tester) async {
|
|
||||||
await tester.pumpWidget(FluffyChatApp(testWidget: SignUp()));
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
Reference in New Issue