ente/lib/ui/account/password_reentry_page.dart

290 lines
10 KiB
Dart
Raw Normal View History

// @dart=2.9
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';
import 'package:photos/models/key_attributes.dart';
import 'package:photos/ui/account/recovery_page.dart';
import 'package:photos/ui/common/dialogs.dart';
2022-07-03 10:09:01 +00:00
import 'package:photos/ui/common/dynamic_fab.dart';
import 'package:photos/ui/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();
2022-07-14 13:28:37 +00:00
String email;
2021-05-12 16:20:30 +00:00
bool _passwordInFocus = false;
bool _passwordVisible = false;
@override
void initState() {
super.initState();
2022-07-14 13:28:37 +00:00
email = Configuration.instance.getEmail();
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() {
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(
isKeypadOpen: isKeypadOpen,
isFormValid: _passwordController.text.isNotEmpty,
2022-07-15 03:55:43 +00:00
buttonText: 'Verify password',
2022-05-30 15:43:33 +00:00
onPressedFunction: () async {
FocusScope.of(context).unfocus();
2022-05-30 15:43:33 +00:00
final dialog = createProgressDialog(context, "Please wait...");
await dialog.show();
try {
await Configuration.instance.decryptAndSaveSecrets(
2022-06-11 08:23:52 +00:00
_passwordController.text,
Configuration.instance.getKeyAttributes(),
);
} on KeyDerivationError catch (e, s) {
_logger.severe("Password verification failed", e, s);
await dialog.hide();
final dialogUserChoice = await showChoiceDialog(
context,
"Recreate password",
"The current device is not powerful enough to verify your "
"password, so we need to regenerate it once in a way that "
"works with all devices. \n\nPlease login using your "
"recovery key and regenerate your password (you can use the same one again if you wish).",
firstAction: "Cancel",
firstActionColor: Theme.of(context).colorScheme.primary,
secondAction: "Use recovery key",
secondActionColor: Theme.of(context).colorScheme.primary,
);
if (dialogUserChoice == DialogUserChoice.secondChoice) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const RecoveryPage();
},
),
);
}
return;
2022-08-25 11:09:49 +00:00
} catch (e, s) {
_logger.severe("Password verification failed", e, s);
2022-05-30 15:43:33 +00:00
await dialog.hide();
2022-08-29 14:43:31 +00:00
final dialogUserChoice = await showChoiceDialog(
context,
"Incorrect password",
"Please try again",
firstAction: "Contact Support",
firstActionColor: Theme.of(context).colorScheme.primary,
secondAction: "Ok",
secondActionColor: Theme.of(context).colorScheme.primary,
);
if (dialogUserChoice == DialogUserChoice.firstChoice) {
await sendLogs(
context,
"Contact support",
"support@ente.io",
postShare: () {},
);
}
2022-05-30 15:43:33 +00:00
return;
}
await dialog.hide();
Bus.instance.fire(SubscriptionPurchasedEvent());
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) {
2022-07-04 06:02:17 +00:00
return const HomeWidget();
},
),
(route) => false,
);
2022-05-30 15:43:33 +00:00
},
),
floatingActionButtonLocation: fabLocation(),
floatingActionButtonAnimator: NoScalingAnimation(),
);
}
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(
'Welcome back!',
style: Theme.of(context).textTheme.headline4,
),
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(
autofillHints: const [AutofillHints.password],
decoration: InputDecoration(
hintText: "Enter your password",
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(
"Forgot password",
style:
Theme.of(context).textTheme.subtitle1.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 {
final dialog =
createProgressDialog(context, "Please wait...");
await dialog.show();
await Configuration.instance.logout();
await dialog.hide();
Navigator.of(context)
.popUntil((route) => route.isFirst);
},
child: Center(
child: Text(
"Change email",
style:
Theme.of(context).textTheme.subtitle1.copyWith(
fontSize: 14,
decoration: TextDecoration.underline,
),
),
2022-04-09 05:40:56 +00:00
),
),
2022-07-14 13:28:37 +00:00
],
),
)
],
),
),
),
2021-03-30 11:36:36 +00:00
],
);
}
void validatePreVerificationState(KeyAttributes keyAttributes) {
if (keyAttributes == null) {
throw Exception("Key Attributes can not be null");
}
}
}