ente/lib/ui/account/verify_recovery_page.dart

217 lines
7.8 KiB
Dart
Raw Normal View History

2022-09-20 06:08:12 +00:00
import 'dart:ui';
import 'package:bip39/bip39.dart' as bip39;
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:photos/core/event_bus.dart';
2022-09-23 09:04:15 +00:00
import 'package:photos/ente_theme_data.dart';
import 'package:photos/events/notification_event.dart';
2023-04-04 20:21:40 +00:00
import "package:photos/generated/l10n.dart";
2022-09-20 06:08:12 +00:00
import 'package:photos/services/local_authentication_service.dart';
import 'package:photos/services/user_remote_flag_service.dart';
import 'package:photos/services/user_service.dart';
import 'package:photos/ui/account/recovery_key_page.dart';
import 'package:photos/ui/common/gradient_button.dart';
import 'package:photos/ui/components/buttons/button_widget.dart';
2023-02-03 04:41:45 +00:00
import 'package:photos/utils/crypto_util.dart';
2022-09-20 06:08:12 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/navigation_util.dart';
class VerifyRecoveryPage extends StatefulWidget {
const VerifyRecoveryPage({Key? key}) : super(key: key);
@override
State<VerifyRecoveryPage> createState() => _VerifyRecoveryPageState();
}
class _VerifyRecoveryPageState extends State<VerifyRecoveryPage> {
final _recoveryKey = TextEditingController();
final Logger _logger = Logger((_VerifyRecoveryPageState).toString());
void _verifyRecoveryKey() async {
2023-04-04 20:21:40 +00:00
final dialog =
createProgressDialog(context, S.of(context).verifyingRecoveryKey);
2022-09-20 06:08:12 +00:00
await dialog.show();
try {
final String inputKey = _recoveryKey.text.trim();
2023-02-03 04:41:45 +00:00
final String recoveryKey = CryptoUtil.bin2hex(
2022-09-20 06:08:12 +00:00
await UserService.instance.getOrCreateRecoveryKey(context),
);
final String recoveryKeyWords = bip39.entropyToMnemonic(recoveryKey);
if (inputKey == recoveryKey || inputKey == recoveryKeyWords) {
try {
await UserRemoteFlagService.instance.markRecoveryVerificationAsDone();
} catch (e) {
await dialog.hide();
if (e is DioError && e.type == DioErrorType.other) {
await showErrorDialog(
context,
"No internet connection",
"Please check your internet connection and try again.",
);
2022-09-20 10:33:22 +00:00
} else {
2023-11-30 05:19:35 +00:00
await showGenericErrorDialog(context: context, error: e);
2022-09-20 06:08:12 +00:00
}
return;
}
Bus.instance.fire(NotificationEvent());
2022-09-20 06:08:12 +00:00
await dialog.hide();
// todo: change this as per figma once the component is ready
await showErrorDialog(
context,
2023-04-04 20:21:40 +00:00
S.of(context).recoveryKeyVerified,
S.of(context).recoveryKeySuccessBody,
);
2022-09-20 06:08:12 +00:00
Navigator.of(context).pop();
} else {
throw Exception("recovery key didn't match");
}
} catch (e, s) {
_logger.severe("failed to verify recovery key", e, s);
await dialog.hide();
2023-04-04 20:21:40 +00:00
final String errMessage = S.of(context).invalidRecoveryKey;
final result = await showChoiceDialog(
2023-01-09 05:41:46 +00:00
context,
2023-04-04 20:21:40 +00:00
title: S.of(context).invalidKey,
body: errMessage,
2023-04-04 20:21:40 +00:00
firstButtonLabel: S.of(context).tryAgain,
secondButtonLabel: S.of(context).viewRecoveryKey,
secondButtonAction: ButtonAction.second,
2022-09-20 06:08:12 +00:00
);
if (result!.action == ButtonAction.second) {
2022-09-20 06:08:12 +00:00
await _onViewRecoveryKeyClick();
}
}
}
Future<void> _onViewRecoveryKeyClick() async {
final hasAuthenticated =
await LocalAuthenticationService.instance.requestLocalAuthentication(
context,
"Please authenticate to view your recovery key",
);
if (hasAuthenticated) {
String recoveryKey;
try {
2023-02-03 04:41:45 +00:00
recoveryKey = CryptoUtil.bin2hex(
2022-09-20 06:08:12 +00:00
await UserService.instance.getOrCreateRecoveryKey(context),
);
2023-12-16 21:04:26 +00:00
// ignore: unawaited_futures
2022-09-20 06:08:12 +00:00
routeToPage(
context,
RecoveryKeyPage(
recoveryKey,
2023-04-04 20:21:40 +00:00
S.of(context).ok,
2022-09-20 06:08:12 +00:00
showAppBar: true,
onDone: () {
Navigator.of(context).pop();
},
),
);
} catch (e) {
2023-12-02 11:42:52 +00:00
await showGenericErrorDialog(context: context, error: e);
2022-09-20 06:08:12 +00:00
return;
}
}
}
@override
Widget build(BuildContext context) {
2022-09-23 09:04:15 +00:00
final enteTheme = Theme.of(context).colorScheme.enteTheme;
2022-09-20 06:08:12 +00:00
return Scaffold(
appBar: AppBar(
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
color: Theme.of(context).iconTheme.color,
onPressed: () {
Navigator.of(context).pop();
},
),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: LayoutBuilder(
builder: (context, constraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: constraints.maxWidth,
minHeight: constraints.maxHeight,
),
child: IntrinsicHeight(
child: Column(
children: [
const SizedBox(height: 12),
SizedBox(
width: double.infinity,
child: Text(
2023-04-04 20:21:40 +00:00
S.of(context).confirmRecoveryKey,
2022-09-23 09:04:15 +00:00
style: enteTheme.textTheme.h3Bold,
2022-09-20 06:08:12 +00:00
textAlign: TextAlign.left,
),
),
2022-09-23 09:04:15 +00:00
const SizedBox(height: 18),
2022-09-20 06:08:12 +00:00
Text(
2023-04-04 20:21:40 +00:00
S.of(context).recoveryKeyVerifyReason,
2022-09-23 09:04:15 +00:00
style: enteTheme.textTheme.small
.copyWith(color: enteTheme.colorScheme.textMuted),
2022-09-20 06:08:12 +00:00
),
const SizedBox(height: 12),
2022-09-20 06:08:12 +00:00
TextFormField(
decoration: InputDecoration(
filled: true,
2023-04-04 20:21:40 +00:00
hintText: S.of(context).enterYourRecoveryKey,
2022-09-20 06:08:12 +00:00
contentPadding: const EdgeInsets.all(20),
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
),
style: const TextStyle(
fontSize: 14,
fontFeatures: [FontFeature.tabularFigures()],
),
controller: _recoveryKey,
autofocus: false,
autocorrect: false,
keyboardType: TextInputType.multiline,
minLines: 4,
maxLines: null,
onChanged: (_) {
setState(() {});
},
),
const SizedBox(height: 12),
2022-09-20 06:08:12 +00:00
Expanded(
child: Container(
alignment: Alignment.bottomCenter,
width: double.infinity,
padding: const EdgeInsets.fromLTRB(0, 12, 0, 40),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
GradientButton(
onTap: _verifyRecoveryKey,
2023-04-04 20:21:40 +00:00
text: S.of(context).confirm,
2022-09-20 06:08:12 +00:00
),
const SizedBox(height: 8),
],
),
),
),
2023-08-19 11:39:56 +00:00
const SizedBox(height: 20),
2022-09-20 06:08:12 +00:00
],
),
),
),
);
},
),
),
);
}
}