ente/lib/ui/account/password_reentry_page.dart

299 lines
10 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/errors.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/subscription_purchased_event.dart';
2023-04-04 20:21:40 +00:00
import "package:photos/generated/l10n.dart";
import 'package:photos/ui/account/recovery_page.dart';
2022-07-03 10:09:01 +00:00
import 'package:photos/ui/common/dynamic_fab.dart';
import 'package:photos/ui/components/buttons/button_widget.dart';
2023-06-06 09:57:17 +00:00
import 'package:photos/ui/tabs/home_widget.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/email_util.dart';
2021-01-05 14:27:02 +00:00
class PasswordReentryPage extends StatefulWidget {
const PasswordReentryPage({Key? key}) : super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<PasswordReentryPage> createState() => _PasswordReentryPageState();
}
2021-01-05 14:27:02 +00:00
class _PasswordReentryPageState extends State<PasswordReentryPage> {
final _logger = Logger((_PasswordReentryPageState).toString());
2021-01-05 15:26:10 +00:00
final _passwordController = TextEditingController();
final FocusNode _passwordFocusNode = FocusNode();
String? email;
2021-05-12 16:20:30 +00:00
bool _passwordInFocus = false;
bool _passwordVisible = false;
2023-07-18 02:47:29 +00:00
String? _volatilePassword;
2021-05-12 16:20:30 +00:00
@override
void initState() {
super.initState();
2022-07-14 13:28:37 +00:00
email = Configuration.instance.getEmail();
2023-07-18 02:47:29 +00:00
_volatilePassword = Configuration.instance.getVolatilePassword();
if (_volatilePassword != null) {
Future.delayed(
Duration.zero,
() => verifyPassword(_volatilePassword!),
);
}
2021-05-12 16:20:30 +00:00
_passwordFocusNode.addListener(() {
setState(() {
_passwordInFocus = _passwordFocusNode.hasFocus;
});
});
}
@override
Widget build(BuildContext context) {
2022-06-15 06:48:23 +00:00
final isKeypadOpen = MediaQuery.of(context).viewInsets.bottom > 100;
2022-04-09 05:40:56 +00:00
FloatingActionButtonLocation? fabLocation() {
2022-04-09 05:40:56 +00:00
if (isKeypadOpen) {
return null;
} else {
return FloatingActionButtonLocation.centerFloat;
}
}
return Scaffold(
2022-06-15 06:48:23 +00:00
resizeToAvoidBottomInset: isKeypadOpen,
2022-05-30 15:43:33 +00:00
appBar: AppBar(
elevation: 0,
leading: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(Icons.arrow_back),
2022-05-30 15:43:33 +00:00
color: Theme.of(context).iconTheme.color,
onPressed: () {
Navigator.of(context).pop();
2022-04-09 05:40:56 +00:00
},
),
2022-05-30 15:43:33 +00:00
),
body: _getBody(),
floatingActionButton: DynamicFAB(
key: const ValueKey("verifyPasswordButton"),
2022-05-30 15:43:33 +00:00
isKeypadOpen: isKeypadOpen,
isFormValid: _passwordController.text.isNotEmpty,
2023-04-04 20:21:40 +00:00
buttonText: S.of(context).verifyPassword,
2022-05-30 15:43:33 +00:00
onPressedFunction: () async {
FocusScope.of(context).unfocus();
2023-07-18 02:47:29 +00:00
await verifyPassword(_passwordController.text);
2022-05-30 15:43:33 +00:00
},
),
floatingActionButtonLocation: fabLocation(),
floatingActionButtonAnimator: NoScalingAnimation(),
);
}
2023-07-18 02:47:29 +00:00
Future<void> verifyPassword(String password) async {
FocusScope.of(context).unfocus();
final dialog =
createProgressDialog(context, S.of(context).pleaseWait);
await dialog.show();
try {
await Configuration.instance.decryptAndSaveSecrets(
password,
Configuration.instance.getKeyAttributes()!,
);
} on KeyDerivationError catch (e, s) {
_logger.severe("Password verification failed", e, s);
await dialog.hide();
final dialogChoice = await showChoiceDialog(
context,
title: S.of(context).recreatePasswordTitle,
body: S.of(context).recreatePasswordBody,
firstButtonLabel: S.of(context).useRecoveryKey,
);
if (dialogChoice!.action == ButtonAction.first) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const RecoveryPage();
},
),
);
}
return;
} catch (e, s) {
_logger.severe("Password verification failed", e, s);
await dialog.hide();
final dialogChoice = await showChoiceDialog(
context,
title: S.of(context).incorrectPasswordTitle,
body: S.of(context).pleaseTryAgain,
firstButtonLabel: S.of(context).contactSupport,
secondButtonLabel: S.of(context).ok,
);
if (dialogChoice!.action == ButtonAction.first) {
await sendLogs(
context,
S.of(context).contactSupport,
"support@ente.io",
postShare: () {},
);
}
return;
}
await dialog.hide();
Bus.instance.fire(SubscriptionPurchasedEvent());
unawaited(
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) {
return const HomeWidget();
},
),
(route) => false,
),
);
}
Widget _getBody() {
2021-03-30 11:36:36 +00:00
return Column(
children: [
2022-04-09 05:40:56 +00:00
Expanded(
2022-07-14 13:28:37 +00:00
child: AutofillGroup(
child: ListView(
children: [
Padding(
padding:
const EdgeInsets.symmetric(vertical: 30, horizontal: 20),
child: Text(
2023-04-04 20:21:40 +00:00
S.of(context).welcomeBack,
2023-06-13 06:41:31 +00:00
style: Theme.of(context).textTheme.headlineMedium,
2022-07-14 13:28:37 +00:00
),
2022-06-11 08:23:52 +00:00
),
2022-07-14 13:28:37 +00:00
Visibility(
// hidden textForm for suggesting auto-fill service for saving
// password
visible: false,
child: TextFormField(
autofillHints: const [
AutofillHints.email,
],
autocorrect: false,
keyboardType: TextInputType.emailAddress,
initialValue: email,
textInputAction: TextInputAction.next,
2022-04-09 05:40:56 +00:00
),
2022-07-14 13:28:37 +00:00
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 24, 20, 0),
child: TextFormField(
key: const ValueKey("passwordInputField"),
2022-07-14 13:28:37 +00:00
autofillHints: const [AutofillHints.password],
decoration: InputDecoration(
2023-04-04 20:21:40 +00:00
hintText: S.of(context).enterYourPassword,
2022-07-14 13:28:37 +00:00
filled: true,
contentPadding: const EdgeInsets.all(20),
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
suffixIcon: _passwordInFocus
? IconButton(
icon: Icon(
_passwordVisible
? Icons.visibility
: Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
size: 20,
),
onPressed: () {
setState(() {
_passwordVisible = !_passwordVisible;
});
},
)
: null,
),
style: const TextStyle(
fontSize: 14,
),
controller: _passwordController,
autofocus: true,
autocorrect: false,
obscureText: !_passwordVisible,
keyboardType: TextInputType.visiblePassword,
focusNode: _passwordFocusNode,
onChanged: (_) {
setState(() {});
},
2022-04-09 05:40:56 +00:00
),
2021-03-30 12:13:13 +00:00
),
2022-07-14 13:28:37 +00:00
const Padding(
padding: EdgeInsets.symmetric(vertical: 18),
child: Divider(
thickness: 1,
),
),
2022-07-14 13:28:37 +00:00
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const RecoveryPage();
},
),
);
},
child: Center(
child: Text(
2023-04-04 20:21:40 +00:00
S.of(context).forgotPassword,
2023-06-13 06:41:31 +00:00
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(
fontSize: 14,
decoration: TextDecoration.underline,
),
2022-04-09 05:40:56 +00:00
),
),
),
2022-07-14 13:28:37 +00:00
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () async {
2023-04-04 20:21:40 +00:00
final dialog = createProgressDialog(
2023-04-18 09:05:21 +00:00
context,
S.of(context).pleaseWait,
);
2022-07-14 13:28:37 +00:00
await dialog.show();
await Configuration.instance.logout();
await dialog.hide();
Navigator.of(context)
.popUntil((route) => route.isFirst);
},
child: Center(
child: Text(
2023-04-04 20:21:40 +00:00
S.of(context).changeEmail,
2023-06-13 06:41:31 +00:00
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(
fontSize: 14,
decoration: TextDecoration.underline,
),
2022-07-14 13:28:37 +00:00
),
2022-04-09 05:40:56 +00:00
),
),
2022-07-14 13:28:37 +00:00
],
),
)
],
),
),
),
2021-03-30 11:36:36 +00:00
],
);
}
}