π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Fix verification navigation and recovery context
r4khul9cf7a69965b9
diff --git a/mobile/apps/photos/changes/onboarding-auth-screens.md b/mobile/apps/photos/changes/onboarding-auth-screens.mdindex f779e4069a..03ad5852fd 100644--- a/mobile/apps/photos/changes/onboarding-auth-screens.md+++ b/mobile/apps/photos/changes/onboarding-auth-screens.md@@ -1 +1 @@-- Polished sign-up and login screens. The terms checkbox now starts unchecked and screen titles are clearer.+- Polished sign-up and login screens with clearer titles, explicit terms acceptance, and improved verification actions.diff --git a/mobile/apps/photos/lib/services/account/user_service.dart b/mobile/apps/photos/lib/services/account/user_service.dartindex f1d5ca471d..ec23f3d40c 100644--- a/mobile/apps/photos/lib/services/account/user_service.dart+++ b/mobile/apps/photos/lib/services/account/user_service.dart@@ -112,7 +112,10 @@ class UserService {bool isChangeEmail = false,bool isCreateAccountScreen = false,bool isResetPasswordScreen = false,+ bool isForgotPassword = false,String? purpose,+ bool isResend = false,+ VoidCallback? onChangeEmail,}) async {final dialog = createProgressDialog(context, context.strings.pleaseWait);await dialog.show();@@ -124,7 +127,7 @@ class UserService {isMobile: Platform.isIOS || Platform.isAndroid,);await dialog.hide();- if (!context.mounted) return;+ if (!context.mounted || isResend) return;unawaited(Navigator.of(context).push(MaterialPageRoute(@@ -134,6 +137,9 @@ class UserService {isChangeEmail: isChangeEmail,isCreateAccountScreen: isCreateAccountScreen,isResetPasswordScreen: isResetPasswordScreen,+ isForgotPassword: isForgotPassword,+ purpose: purpose,+ onChangeEmail: onChangeEmail,);},),@@ -422,6 +428,7 @@ class UserService {BuildContext context,String ott, {bool isResettingPasswordScreen = false,+ bool isForgotPassword = false,}) async {final dialog = createProgressDialog(context, context.strings.pleaseWait);await dialog.show();@@ -453,7 +460,7 @@ class UserService {await _saveConfiguration(responseData);if (Configuration.instance.getEncryptedToken() != null) {if (isResettingPasswordScreen) {- page = const RecoveryPage();+ page = RecoveryPage(isForgotPassword: isForgotPassword);} else {page = const PasswordReentryPage();}diff --git a/mobile/apps/photos/lib/ui/account/login_pwd_verification_page.dart b/mobile/apps/photos/lib/ui/account/login_pwd_verification_page.dartindex 3dbc4aa03b..705ff239d6 100644--- a/mobile/apps/photos/lib/ui/account/login_pwd_verification_page.dart+++ b/mobile/apps/photos/lib/ui/account/login_pwd_verification_page.dart@@ -140,6 +140,7 @@ class _LoginPasswordVerificationPageStatecontext,email!,isResetPasswordScreen: true,+ isForgotPassword: true,);},),@@ -149,6 +150,10 @@ class _LoginPasswordVerificationPageState);}+ void _returnToEmailEntry() {+ if (mounted) Navigator.of(context).pop();+ }+Future<void> verifyPassword(BuildContext context, String password) async {final dialog = createProgressDialog(context,@@ -207,6 +212,7 @@ class _LoginPasswordVerificationPageStatecontext,email!,isCreateAccountScreen: true,+ onChangeEmail: _returnToEmailEntry,);return;} else if (e is KeyDerivationError) {diff --git a/mobile/apps/photos/lib/ui/account/ott_verification_page.dart b/mobile/apps/photos/lib/ui/account/ott_verification_page.dartindex d2dcee425f..02b2cb8143 100644--- a/mobile/apps/photos/lib/ui/account/ott_verification_page.dart+++ b/mobile/apps/photos/lib/ui/account/ott_verification_page.dart@@ -8,12 +8,18 @@ class OTTVerificationPage extends StatefulWidget {final bool isChangeEmail;final bool isCreateAccountScreen;final bool isResetPasswordScreen;+ final bool isForgotPassword;+ final String? purpose;+ final VoidCallback? onChangeEmail;const OTTVerificationPage(this.email, {this.isChangeEmail = false,this.isCreateAccountScreen = false,this.isResetPasswordScreen = false,+ this.isForgotPassword = false,+ this.purpose,+ this.onChangeEmail,super.key,});@@ -25,9 +31,10 @@ class _OTTVerificationPageState extends State<OTTVerificationPage> {final _pinController = TextEditingController();String _code = "";bool _isSubmitting = false;+ bool _isResendingCode = false;Future<void> _onVerifyPressed() async {- if (_isSubmitting) {+ if (_isSubmitting || _isResendingCode) {return;}setState(() {@@ -45,6 +52,7 @@ class _OTTVerificationPageState extends State<OTTVerificationPage> {context,_pinController.text,isResettingPasswordScreen: widget.isResetPasswordScreen,+ isForgotPassword: widget.isForgotPassword,);}} finally {@@ -69,7 +77,8 @@ class _OTTVerificationPageState extends State<OTTVerificationPage> {@overrideWidget build(BuildContext context) {final colors = context.componentColors;- final isFormValid = _code.length == 6 && !_isSubmitting;+ final isFormValid =+ _code.length == 6 && !_isSubmitting && !_isResendingCode;return Scaffold(resizeToAvoidBottomInset: true,@@ -109,92 +118,108 @@ class _OTTVerificationPageState extends State<OTTVerificationPage> {final colors = context.componentColors;return SafeArea(- child: Padding(- padding: const EdgeInsets.symmetric(horizontal: 16),- child: Column(- children: [- Center(child: Image.asset('assets/ott.png', height: 96)),- const SizedBox(height: 24),- Text(- context.strings.weHaveSentCodeTo(email: widget.email),- style: TextStyles.body.copyWith(color: colors.textBase),- textAlign: TextAlign.center,- ),- const SizedBox(height: 8),- Text(- widget.isResetPasswordScreen- ? context.strings.toResetVerifyEmail- : context.strings.checkInboxAndSpamFolder,- style: TextStyles.body.copyWith(color: colors.textLight),- textAlign: TextAlign.center,- ),- const SizedBox(height: 32),- Center(- child: PinInputComponent(- length: 6,- controller: _pinController,- autofocus: true,- autofillHints: const [AutofillHints.oneTimeCode],- onChanged: (String pin) {- setState(() {- _code = pin;- });- },- onCompleted: (value) {- if (value.length == 6) {- _onVerifyPressed();- }- },- ),+ child: ListView(+ padding: const EdgeInsets.fromLTRB(16, 0, 16, 96),+ children: [+ Center(child: Image.asset('assets/ott.png', height: 96)),+ const SizedBox(height: 24),+ Text(+ context.strings.weHaveSentCodeTo(email: widget.email),+ style: TextStyles.body.copyWith(color: colors.textBase),+ textAlign: TextAlign.center,+ ),+ const SizedBox(height: 8),+ Text(+ widget.isResetPasswordScreen+ ? context.strings.toResetVerifyEmail+ : context.strings.checkInboxAndSpamFolder,+ style: TextStyles.body.copyWith(color: colors.textLight),+ textAlign: TextAlign.center,+ ),+ const SizedBox(height: 32),+ Center(+ child: PinInputComponent(+ length: 6,+ controller: _pinController,+ autofocus: true,+ isDisabled: _isSubmitting || _isResendingCode,+ autofillHints: const [AutofillHints.oneTimeCode],+ onChanged: (String pin) {+ setState(() {+ _code = pin;+ });+ },+ onCompleted: (value) {+ if (value.length == 6) {+ _onVerifyPressed();+ }+ },),- const SizedBox(height: 24),- if (widget.isResetPasswordScreen)- Center(child: _buildResendCodeButton())- else- Row(- mainAxisAlignment: MainAxisAlignment.spaceBetween,- children: [- Semantics(- button: true,- child: GestureDetector(- behavior: HitTestBehavior.opaque,- onTap: () => Navigator.of(context).pop(),- child: Padding(- padding: const EdgeInsets.symmetric(vertical: 14),- child: Text(- context.strings.changeEmail,- style: TextStyles.bodyLink.copyWith(- color: colors.textLight,- decorationColor: colors.textLight,- ),- ),- ),- ),+ ),+ const SizedBox(height: 24),+ if (widget.isResetPasswordScreen)+ Center(child: _buildResendCodeButton())+ else+ Wrap(+ alignment: WrapAlignment.spaceBetween,+ crossAxisAlignment: WrapCrossAlignment.center,+ spacing: Spacing.lg,+ children: [+ TextButton(+ style: TextButton.styleFrom(+ foregroundColor: colors.textLight,+ padding: const EdgeInsets.symmetric(vertical: 14),+ textStyle: TextStyles.bodyLink,),- _buildResendCodeButton(),- ],- ),- ],- ),+ onPressed: _isSubmitting || _isResendingCode+ ? null+ : () {+ Navigator.of(context).pop();+ widget.onChangeEmail?.call();+ },+ child: Text(context.strings.changeEmail),+ ),+ _buildResendCodeButton(),+ ],+ ),+ ],),);}Widget _buildResendCodeButton() {- return ButtonComponent(- label: context.strings.resendCode,- variant: ButtonComponentVariant.link,- size: ButtonComponentSize.small,- onTap: () async {- // ignore: unawaited_futures- UserService.instance.sendOtt(- context,- widget.email,- isCreateAccountScreen: widget.isCreateAccountScreen,- isResetPasswordScreen: widget.isResetPasswordScreen,- isChangeEmail: widget.isChangeEmail,- );- },+ final colors = context.componentColors;+ return TextButton(+ style: TextButton.styleFrom(+ foregroundColor: colors.primary,+ padding: const EdgeInsets.symmetric(vertical: Spacing.xs),+ textStyle: TextStyles.bodyBold.copyWith(+ decoration: TextDecoration.underline,+ ),+ ),+ onPressed: _isSubmitting || _isResendingCode ? null : _resendCode,+ child: Text(context.strings.resendCode),);}++ Future<void> _resendCode() async {+ if (_isSubmitting || _isResendingCode) return;+ setState(() => _isResendingCode = true);+ try {+ await UserService.instance.sendOtt(+ context,+ widget.email,+ isCreateAccountScreen: widget.isCreateAccountScreen,+ isResetPasswordScreen: widget.isResetPasswordScreen,+ isForgotPassword: widget.isForgotPassword,+ isChangeEmail: widget.isChangeEmail,+ purpose: widget.purpose,+ isResend: true,+ );+ } finally {+ if (mounted) {+ setState(() => _isResendingCode = false);+ }+ }+ }}diff --git a/mobile/apps/photos/lib/ui/account/password_reentry_page.dart b/mobile/apps/photos/lib/ui/account/password_reentry_page.dartindex 3658844aac..73bf414ade 100644--- a/mobile/apps/photos/lib/ui/account/password_reentry_page.dart+++ b/mobile/apps/photos/lib/ui/account/password_reentry_page.dart@@ -253,7 +253,7 @@ class _PasswordReentryPageState extends State<PasswordReentryPage> {Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {- return const RecoveryPage();+ return const RecoveryPage(isForgotPassword: true);},),);diff --git a/mobile/apps/photos/lib/ui/account/recovery_page.dart b/mobile/apps/photos/lib/ui/account/recovery_page.dartindex 1d9540773e..a58cf0532f 100644--- a/mobile/apps/photos/lib/ui/account/recovery_page.dart+++ b/mobile/apps/photos/lib/ui/account/recovery_page.dart@@ -8,7 +8,9 @@ import 'package:photos/ui/notification/toast.dart';import 'package:photos/utils/dialog_util.dart';class RecoveryPage extends StatefulWidget {- const RecoveryPage({super.key});+ final bool isForgotPassword;++ const RecoveryPage({this.isForgotPassword = false, super.key});@overrideState<RecoveryPage> createState() => _RecoveryPageState();@@ -43,7 +45,9 @@ class _RecoveryPageState extends State<RecoveryPage> {},),title: Text(- context.strings.forgotPassword,+ widget.isForgotPassword+ ? context.strings.forgotPassword+ : context.strings.recoverAccount,style: TextStyles.large.copyWith(color: colors.textBase),),centerTitle: true,diff --git a/mobile/apps/photos/test/ui/account/auth_verification_test.dart b/mobile/apps/photos/test/ui/account/auth_verification_test.dartnew file mode 100644index 0000000000..309a6262fb--- /dev/null+++ b/mobile/apps/photos/test/ui/account/auth_verification_test.dart@@ -0,0 +1,332 @@+import "dart:async";++import "package:dio/dio.dart";+import "package:ente_components/ente_components.dart";+import "package:ente_strings/ente_strings.dart";+import "package:flutter/material.dart";+import "package:flutter_test/flutter_test.dart";+import "package:package_info_plus/package_info_plus.dart";+import "package:photos/ente_theme_data.dart";+import "package:photos/service_locator.dart";+import "package:photos/services/account/user_service.dart";+import "package:photos/ui/account/ott_verification_page.dart";+import "package:photos/ui/account/recovery_page.dart";+import "package:shared_preferences/shared_preferences.dart";++void main() {+ TestWidgetsFlutterBinding.ensureInitialized();+ final requests = <RequestOptions>[];+ Completer<void>? pendingRequest;+ Map<String, Object>? failure;++ setUpAll(() async {+ SharedPreferences.setMockInitialValues({});+ final preferences = await SharedPreferences.getInstance();+ final dio = Dio();+ dio.interceptors.add(+ InterceptorsWrapper(+ onRequest: (options, handler) async {+ requests.add(options);+ await pendingRequest?.future;+ if (failure != null) {+ handler.reject(+ DioException(+ requestOptions: options,+ type: DioExceptionType.badResponse,+ response: Response(+ requestOptions: options,+ statusCode: 400,+ data: failure,+ ),+ ),+ );+ } else {+ handler.resolve(Response(requestOptions: options, statusCode: 200));+ }+ },+ ),+ );+ ServiceLocator.instance.init(+ preferences,+ Dio(),+ dio,+ Dio(),+ PackageInfo(+ appName: "Photos",+ packageName: "photos",+ version: "1.0.0",+ buildNumber: "1",+ ),+ );+ });++ setUp(() {+ requests.clear();+ pendingRequest = null;+ failure = null;+ });++ Widget app(+ Widget home, {+ Locale? locale,+ ThemeData? theme,+ double scale = 1,+ }) {+ return MaterialApp(+ theme: theme ?? lightThemeData,+ localizationsDelegates: StringsLocalizations.localizationsDelegates,+ supportedLocales: StringsLocalizations.supportedLocales,+ locale: locale,+ builder: (context, child) => MediaQuery(+ data: MediaQuery.of(+ context,+ ).copyWith(textScaler: TextScaler.linear(scale)),+ child: child!,+ ),+ home: home,+ );+ }++ for (final mode in ["signup", "login", "change", "reset", "recreate"]) {+ testWidgets(+ "$mode resends stay on one route and preserve request purpose",+ (tester) async {+ await tester.pumpWidget(+ app(+ Builder(+ builder: (context) => Scaffold(+ body: TextButton(+ onPressed: () => UserService.instance.sendOtt(+ context,+ "[email protected]",+ isCreateAccountScreen: mode == "signup",+ isChangeEmail: mode == "change",+ isResetPasswordScreen:+ mode == "reset" || mode == "recreate",+ isForgotPassword: mode == "reset",+ purpose: mode == "signup" || mode == "login" ? mode : null,+ ),+ child: const Text("Enter email"),+ ),+ ),+ ),+ ),+ );+ await tester.tap(find.text("Enter email"));+ await tester.pumpAndSettle();+ final page = tester.widget<OTTVerificationPage>(+ find.byType(OTTVerificationPage),+ );+ expect(page.isCreateAccountScreen, mode == "signup");+ expect(page.isChangeEmail, mode == "change");+ expect(+ page.isResetPasswordScreen,+ mode == "reset" || mode == "recreate",+ );+ expect(page.isForgotPassword, mode == "reset");+ final pin = tester.widget<PinInputComponent>(+ find.byType(PinInputComponent),+ );+ pin.controller.text = "12";+ await tester.pump();++ for (var resend = 0; resend < 2; resend++) {+ await tester.tap(find.text("Resend code"));+ await tester.pumpAndSettle();+ expect(+ find.byType(OTTVerificationPage, skipOffstage: false),+ findsOneWidget,+ );+ expect(pin.controller.text, "12");+ }+ expect(requests, hasLength(3));+ for (final request in requests) {+ expect(request.path, endsWith("/users/ott"));+ expect(request.data, {+ "email": "[email protected]",+ "purpose": mode == "reset" || mode == "recreate" ? "" : mode,+ "mobile": false,+ });+ }+ if (mode == "reset" || mode == "recreate") {+ expect(find.text("Change email"), findsNothing);+ Navigator.of(tester.element(find.byType(OTTVerificationPage))).pop();+ } else {+ await tester.tap(find.text("Change email"));+ }+ await tester.pumpAndSettle();+ expect(find.text("Enter email"), findsOneWidget);+ expect(+ find.byType(OTTVerificationPage, skipOffstage: false),+ findsNothing,+ );+ expect(tester.takeException(), isNull);+ },+ );+ }++ testWidgets("change email unwinds a password-verification fallback", (+ tester,+ ) async {+ await tester.pumpWidget(+ app(+ Builder(+ builder: (context) => Scaffold(+ body: TextButton(+ onPressed: () => Navigator.of(context).push(+ MaterialPageRoute<void>(+ builder: (context) => Scaffold(+ body: TextButton(+ onPressed: () => UserService.instance.sendOtt(+ context,+ "[email protected]",+ isCreateAccountScreen: true,+ onChangeEmail: () => Navigator.of(context).pop(),+ ),+ child: const Text("Password fallback"),+ ),+ ),+ ),+ ),+ child: const Text("Enter email"),+ ),+ ),+ ),+ ),+ );+ await tester.tap(find.text("Enter email"));+ await tester.pumpAndSettle();+ await tester.tap(find.text("Password fallback"));+ await tester.pumpAndSettle();+ await tester.tap(find.text("Resend code"));+ await tester.pumpAndSettle();+ await tester.tap(find.text("Change email"));+ await tester.pumpAndSettle();+ expect(find.text("Enter email"), findsOneWidget);+ expect(find.text("Password fallback"), findsNothing);+ expect(find.byType(OTTVerificationPage, skipOffstage: false), findsNothing);+ expect(tester.takeException(), isNull);+ });++ testWidgets("pending resend blocks duplicate sends and code submission", (+ tester,+ ) async {+ await tester.pumpWidget(+ app(const OTTVerificationPage("[email protected]")),+ );+ pendingRequest = Completer<void>();+ await tester.tap(find.text("Resend code"));+ await tester.pump(const Duration(milliseconds: 300));+ expect(requests, hasLength(1));+ final pin = tester.widget<PinInputComponent>(+ find.byType(PinInputComponent),+ );+ expect(pin.isDisabled, isTrue);+ pin.controller.text = "123456";+ await tester.pump();+ expect(requests, hasLength(1));+ final actions = tester.widgetList<TextButton>(find.byType(TextButton));+ expect(actions.every((button) => button.onPressed == null), isTrue);+ pendingRequest!.complete();+ await tester.pumpAndSettle();+ expect(find.byType(OTTVerificationPage), findsOneWidget);+ expect(+ tester+ .widget<PinInputComponent>(find.byType(PinInputComponent))+ .isDisabled,+ isFalse,+ );+ expect(tester.takeException(), isNull);+ });++ testWidgets("failed resend preserves the page and permits a retry", (+ tester,+ ) async {+ await tester.pumpWidget(+ app(const OTTVerificationPage("[email protected]")),+ );+ failure = {"code": "USER_NOT_REGISTERED"};+ await tester.tap(find.text("Resend code"));+ await tester.pumpAndSettle();+ expect(find.byType(OTTVerificationPage), findsOneWidget);+ expect(find.text("Resend code"), findsOneWidget);+ Navigator.of(tester.element(find.byType(OTTVerificationPage))).pop();+ await tester.pumpAndSettle();+ failure = null;+ await tester.tap(find.text("Resend code"));+ await tester.pumpAndSettle();+ expect(requests, hasLength(2));+ expect(find.byType(OTTVerificationPage), findsOneWidget);+ expect(tester.takeException(), isNull);+ });++ testWidgets("disposed resend does not navigate after the response", (+ tester,+ ) async {+ await tester.pumpWidget(+ app(const OTTVerificationPage("[email protected]")),+ );+ pendingRequest = Completer<void>();+ await tester.tap(find.text("Resend code"));+ await tester.pump(const Duration(milliseconds: 300));+ await tester.pumpWidget(const SizedBox());+ pendingRequest!.complete();+ await tester.pumpAndSettle();+ expect(requests, hasLength(1));+ expect(tester.takeException(), isNull);+ });++ for (final theme in [lightThemeData, darkThemeData]) {+ testWidgets(+ "Russian actions wrap at large text scale in ${theme.brightness}",+ (tester) async {+ tester.view.physicalSize = const Size(320, 568);+ tester.view.devicePixelRatio = 1;+ tester.view.viewInsets = const FakeViewPadding(bottom: 250);+ addTearDown(tester.view.reset);+ await tester.pumpWidget(+ app(+ const OTTVerificationPage("[email protected]"),+ locale: const Locale("ru"),+ theme: theme,+ scale: 2,+ ),+ );+ await tester.pumpAndSettle();+ final context = tester.element(find.byType(OTTVerificationPage));+ for (final label in [+ context.strings.changeEmail,+ context.strings.resendCode,+ ]) {+ final action = find.text(label);+ await tester.scrollUntilVisible(action, 150);+ await tester.pumpAndSettle();+ final rect = tester.getRect(action);+ expect(rect.left, greaterThanOrEqualTo(16));+ expect(rect.right, lessThanOrEqualTo(304));+ expect(tester.takeException(), isNull);+ }+ },+ );+ }++ for (final forgotPassword in [true, false]) {+ testWidgets(+ "recovery title matches forgot-password context $forgotPassword",+ (tester) async {+ await tester.pumpWidget(+ app(RecoveryPage(isForgotPassword: forgotPassword)),+ );+ final context = tester.element(find.byType(RecoveryPage));+ final title = forgotPassword+ ? context.strings.forgotPassword+ : context.strings.recoverAccount;+ expect(+ find.descendant(of: find.byType(AppBar), matching: find.text(title)),+ findsOneWidget,+ );+ expect(tester.takeException(), isNull);+ },+ );+ }+}