ente/lib/ui/account/two_factor_authentication_page.dart

150 lines
4.4 KiB
Dart
Raw Normal View History

2021-06-26 10:37:17 +00:00
import 'package:flutter/material.dart';
2021-06-29 11:21:28 +00:00
import 'package:flutter/services.dart';
2023-04-04 20:21:40 +00:00
import "package:photos/generated/l10n.dart";
2021-06-26 10:37:17 +00:00
import 'package:photos/services/user_service.dart';
2021-06-29 11:21:28 +00:00
import 'package:photos/ui/lifecycle_event_handler.dart';
2021-06-26 10:37:17 +00:00
import 'package:pinput/pin_put/pin_put.dart';
class TwoFactorAuthenticationPage extends StatefulWidget {
final String sessionID;
2022-12-28 06:30:48 +00:00
const TwoFactorAuthenticationPage(this.sessionID, {Key? key})
2021-06-26 10:37:17 +00:00
: super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<TwoFactorAuthenticationPage> createState() =>
2021-06-26 10:37:17 +00:00
_TwoFactorAuthenticationPageState();
}
class _TwoFactorAuthenticationPageState
extends State<TwoFactorAuthenticationPage> {
final _pinController = TextEditingController();
final _pinPutDecoration = BoxDecoration(
2022-07-04 06:02:17 +00:00
border: Border.all(color: const Color.fromRGBO(45, 194, 98, 1.0)),
2021-06-26 10:37:17 +00:00
borderRadius: BorderRadius.circular(15.0),
);
String _code = "";
2022-12-28 06:30:48 +00:00
late LifecycleEventHandler _lifecycleEventHandler;
2021-06-29 11:21:28 +00:00
@override
void initState() {
_lifecycleEventHandler = LifecycleEventHandler(
resumeCallBack: () async {
if (mounted) {
final data = await Clipboard.getData(Clipboard.kTextPlain);
2022-12-28 06:30:48 +00:00
if (data != null && data.text != null && data.text!.length == 6) {
_pinController.text = data.text!;
2021-06-29 11:21:28 +00:00
}
}
},
);
WidgetsBinding.instance.addObserver(_lifecycleEventHandler);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(_lifecycleEventHandler);
super.dispose();
}
2021-06-26 10:37:17 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-04-04 20:21:40 +00:00
title: Text(
S.of(context).twofactorAuthenticationPageTitle,
2021-06-26 10:37:17 +00:00
),
),
body: _getBody(),
);
}
Widget _getBody() {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
2023-04-04 20:21:40 +00:00
Text(
S.of(context).enterThe6digitCodeFromnyourAuthenticatorApp,
style: const TextStyle(
2021-06-26 10:37:17 +00:00
height: 1.4,
fontSize: 16,
),
textAlign: TextAlign.center,
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(32)),
2021-06-26 10:37:17 +00:00
Padding(
padding: const EdgeInsets.fromLTRB(40, 0, 40, 0),
child: PinPut(
fieldsCount: 6,
onSubmit: (String code) {
_verifyTwoFactorCode(code);
},
onChanged: (String pin) {
setState(() {
_code = pin;
});
},
controller: _pinController,
submittedFieldDecoration: _pinPutDecoration.copyWith(
borderRadius: BorderRadius.circular(20.0),
),
selectedFieldDecoration: _pinPutDecoration,
followingFieldDecoration: _pinPutDecoration.copyWith(
borderRadius: BorderRadius.circular(5.0),
border: Border.all(
2022-07-04 06:02:17 +00:00
color: const Color.fromRGBO(45, 194, 98, 0.5),
2021-06-26 10:37:17 +00:00
),
),
2022-07-04 06:02:17 +00:00
inputDecoration: const InputDecoration(
2021-06-26 10:37:17 +00:00
focusedBorder: InputBorder.none,
border: InputBorder.none,
counterText: '',
),
2021-06-26 11:01:32 +00:00
autofocus: true,
2021-06-26 10:37:17 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(24)),
2021-06-26 10:37:17 +00:00
Container(
padding: const EdgeInsets.fromLTRB(80, 0, 80, 0),
width: double.infinity,
height: 64,
child: OutlinedButton(
2021-06-26 10:37:17 +00:00
onPressed: _code.length == 6
? () async {
_verifyTwoFactorCode(_code);
}
: null,
2023-04-04 20:21:40 +00:00
child: Text(S.of(context).verify),
2021-06-26 10:37:17 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(30)),
2021-06-26 10:37:17 +00:00
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
2023-03-14 01:40:54 +00:00
UserService.instance.recoverTwoFactor(context, widget.sessionID);
2021-06-26 10:37:17 +00:00
},
child: Container(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.all(10),
2023-04-04 20:21:40 +00:00
child: Center(
2021-06-26 10:37:17 +00:00
child: Text(
2023-04-04 20:21:40 +00:00
S.of(context).lostDevice,
style: const TextStyle(
2021-06-26 10:37:17 +00:00
decoration: TextDecoration.underline,
fontSize: 12,
),
),
),
),
),
],
);
}
Future<void> _verifyTwoFactorCode(String code) async {
await UserService.instance.verifyTwoFactor(context, widget.sessionID, code);
}
}