From 7fee3dd550d436a00bf1d173eb772d2439de5380 Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Tue, 19 Mar 2024 13:42:56 +0400 Subject: [PATCH 01/12] Addition of new paramaters to the single entry --- .../core/models/auth_entry_model.dart | 35 +++++++++++++------ .../single_entry/pages/auth_entry_page.dart | 27 +++++++++++++- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart index 23b2a5336..4f1fb5035 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart @@ -1,16 +1,21 @@ import 'package:deriv_auth/deriv_auth.dart'; +import 'package:deriv_http_client/deriv_http_client.dart'; /// Auth Entry Model class class AuthEntryModel { /// Constructor [AuthEntryModel] - const AuthEntryModel({ - required this.getStartedPage, - required this.loginPageModel, - required this.derivAuthCubit, - required this.signupPageModel, - required this.settingPageModel, - required this.resetPassPageModel, - }); + const AuthEntryModel( + {required this.getStartedPage, + required this.loginPageModel, + required this.derivAuthService, + required this.socialAuthService, + required this.signupPageModel, + required this.settingPageModel, + required this.resetPassPageModel, + required this.httpClient, + required this.appToken, + required this.connectionInfo, + required this.derivAuthRepository}); /// GetStartedPage data model final GetStartedPageModel getStartedPage; @@ -18,8 +23,8 @@ class AuthEntryModel { /// LoginPage data model final LoginPageModel loginPageModel; - /// DerivAuthCubit - final DerivAuthCubit derivAuthCubit; + /// DerivAuthService + final DerivAuthService derivAuthService; /// SignupPage data model final SignupPageModel signupPageModel; @@ -29,4 +34,14 @@ class AuthEntryModel { /// ResetPassPage data model final ResetPassPageModel resetPassPageModel; + + final DerivSocialAuthService socialAuthService; + + final BaseHttpClient httpClient; + + final String appToken; + + final AuthConnectionInfo connectionInfo; + + final BaseAuthRepository derivAuthRepository; } diff --git a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart index f15fcaa86..63448537e 100644 --- a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart @@ -2,6 +2,7 @@ import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; import 'package:deriv_auth/features/single_entry/features/get_started/pages/get_started_page.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; /// Auth entry page for single entry class AuthEntryPage extends StatelessWidget { @@ -11,5 +12,29 @@ class AuthEntryPage extends StatelessWidget { } @override - Widget build(BuildContext context) => const GetStartedPage(); + Widget build(BuildContext context) => MultiBlocProvider( + providers: [ + BlocProvider( + create: (context) => DerivAuthCubit( + authService: DerivAuthService( + jwtService: DerivJwtService( + repository: DerivJwtRepository( + client: AuthData().data.httpClient, + appToken: AuthData().data.appToken, + connectionInfo: AuthData().data.connectionInfo, + ), + ), + tokenService: DerivTokenService(AuthData().data.httpClient), + authRepository: AuthData().data.derivAuthRepository, + connectionInfo: AuthData().data.connectionInfo, + ), + ), + ), + BlocProvider( + create: (context) => SocialAuthCubit( + socialAuthService: AuthData().data.socialAuthService), + ), + ], + child: const GetStartedPage(), + ); } From 722dd44da024e82fe0aaa5a1084d507f8ee17e91 Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Tue, 19 Mar 2024 13:52:18 +0400 Subject: [PATCH 02/12] Addition of new paramaters to the single entry --- .../features/single_entry/core/auth_data.dart | 26 +++++++++++++++++++ .../single_entry/pages/auth_entry_page.dart | 22 +++------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart b/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart index a611c3682..014097738 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart @@ -1,5 +1,11 @@ // ignore_for_file: unnecessary_getters_setters +import 'package:deriv_auth/core/services/jwt/repository/deriv_jwt_repository.dart'; +import 'package:deriv_auth/core/services/jwt/services/deriv_jwt_service.dart'; +import 'package:deriv_auth/core/services/token/services/deriv_token_service.dart'; +import 'package:deriv_auth/features/auth/auth.dart'; +import 'package:deriv_auth/features/social_auth/cubit/social_auth_cubit.dart'; + import 'models/auth_entry_model.dart'; /// Authentication Data Singleton @@ -16,6 +22,26 @@ class AuthData { /// data getter AuthEntryModel get data => _data; + /// Cubit Getter + DerivAuthCubit get derivAuthCubit => DerivAuthCubit( + authService: DerivAuthService( + jwtService: DerivJwtService( + repository: DerivJwtRepository( + client: AuthData().data.httpClient, + appToken: AuthData().data.appToken, + connectionInfo: AuthData().data.connectionInfo, + ), + ), + tokenService: DerivTokenService(AuthData().data.httpClient), + authRepository: AuthData().data.derivAuthRepository, + connectionInfo: AuthData().data.connectionInfo, + ), + ); + + /// Social Auth Cubit Getter + SocialAuthCubit get socialAuthCubit => + SocialAuthCubit(socialAuthService: AuthData().data.socialAuthService); + /// data setter set data(AuthEntryModel data) { _data = data; diff --git a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart index 63448537e..f560c7828 100644 --- a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart @@ -14,25 +14,11 @@ class AuthEntryPage extends StatelessWidget { @override Widget build(BuildContext context) => MultiBlocProvider( providers: [ - BlocProvider( - create: (context) => DerivAuthCubit( - authService: DerivAuthService( - jwtService: DerivJwtService( - repository: DerivJwtRepository( - client: AuthData().data.httpClient, - appToken: AuthData().data.appToken, - connectionInfo: AuthData().data.connectionInfo, - ), - ), - tokenService: DerivTokenService(AuthData().data.httpClient), - authRepository: AuthData().data.derivAuthRepository, - connectionInfo: AuthData().data.connectionInfo, - ), - ), + BlocProvider.value( + value: AuthData().derivAuthCubit, ), - BlocProvider( - create: (context) => SocialAuthCubit( - socialAuthService: AuthData().data.socialAuthService), + BlocProvider.value( + value: AuthData().socialAuthCubit, ), ], child: const GetStartedPage(), From 723c3ee040ae580b748ea7c89b93f87130d6624e Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Tue, 19 Mar 2024 14:02:45 +0400 Subject: [PATCH 03/12] Addition of new paramaters to the single entry --- .../features/single_entry/core/auth_data.dart | 13 +-------- .../core/models/auth_entry_model.dart | 29 ++++++------------- 2 files changed, 10 insertions(+), 32 deletions(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart b/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart index 014097738..86dcaebaa 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart @@ -24,18 +24,7 @@ class AuthData { /// Cubit Getter DerivAuthCubit get derivAuthCubit => DerivAuthCubit( - authService: DerivAuthService( - jwtService: DerivJwtService( - repository: DerivJwtRepository( - client: AuthData().data.httpClient, - appToken: AuthData().data.appToken, - connectionInfo: AuthData().data.connectionInfo, - ), - ), - tokenService: DerivTokenService(AuthData().data.httpClient), - authRepository: AuthData().data.derivAuthRepository, - connectionInfo: AuthData().data.connectionInfo, - ), + authService: AuthData().data.derivAuthService, ); /// Social Auth Cubit Getter diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart index 4f1fb5035..de17fe0b6 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart @@ -4,18 +4,15 @@ import 'package:deriv_http_client/deriv_http_client.dart'; /// Auth Entry Model class class AuthEntryModel { /// Constructor [AuthEntryModel] - const AuthEntryModel( - {required this.getStartedPage, - required this.loginPageModel, - required this.derivAuthService, - required this.socialAuthService, - required this.signupPageModel, - required this.settingPageModel, - required this.resetPassPageModel, - required this.httpClient, - required this.appToken, - required this.connectionInfo, - required this.derivAuthRepository}); + const AuthEntryModel({ + required this.getStartedPage, + required this.loginPageModel, + required this.derivAuthService, + required this.socialAuthService, + required this.signupPageModel, + required this.settingPageModel, + required this.resetPassPageModel, + }); /// GetStartedPage data model final GetStartedPageModel getStartedPage; @@ -36,12 +33,4 @@ class AuthEntryModel { final ResetPassPageModel resetPassPageModel; final DerivSocialAuthService socialAuthService; - - final BaseHttpClient httpClient; - - final String appToken; - - final AuthConnectionInfo connectionInfo; - - final BaseAuthRepository derivAuthRepository; } From 6094f3b555a1fd2e56b7353e3cda024d66ad9b4d Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Tue, 19 Mar 2024 14:22:07 +0400 Subject: [PATCH 04/12] Addition of new paramaters to the single entry --- .../lib/features/single_entry/core/auth_data.dart | 9 --------- .../single_entry/core/models/auth_entry_model.dart | 8 ++++---- .../lib/features/single_entry/pages/auth_entry_page.dart | 4 ++-- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart b/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart index 86dcaebaa..1478de792 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/auth_data.dart @@ -22,15 +22,6 @@ class AuthData { /// data getter AuthEntryModel get data => _data; - /// Cubit Getter - DerivAuthCubit get derivAuthCubit => DerivAuthCubit( - authService: AuthData().data.derivAuthService, - ); - - /// Social Auth Cubit Getter - SocialAuthCubit get socialAuthCubit => - SocialAuthCubit(socialAuthService: AuthData().data.socialAuthService); - /// data setter set data(AuthEntryModel data) { _data = data; diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart index de17fe0b6..59e75870d 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart @@ -7,8 +7,8 @@ class AuthEntryModel { const AuthEntryModel({ required this.getStartedPage, required this.loginPageModel, - required this.derivAuthService, - required this.socialAuthService, + required this.derivAuthCubit, + required this.socialAuthCubit, required this.signupPageModel, required this.settingPageModel, required this.resetPassPageModel, @@ -21,7 +21,7 @@ class AuthEntryModel { final LoginPageModel loginPageModel; /// DerivAuthService - final DerivAuthService derivAuthService; + final DerivAuthCubit derivAuthCubit; /// SignupPage data model final SignupPageModel signupPageModel; @@ -32,5 +32,5 @@ class AuthEntryModel { /// ResetPassPage data model final ResetPassPageModel resetPassPageModel; - final DerivSocialAuthService socialAuthService; + final SocialAuthCubit socialAuthCubit; } diff --git a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart index f560c7828..9163f633e 100644 --- a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart @@ -15,10 +15,10 @@ class AuthEntryPage extends StatelessWidget { Widget build(BuildContext context) => MultiBlocProvider( providers: [ BlocProvider.value( - value: AuthData().derivAuthCubit, + value: AuthData().data.derivAuthCubit, ), BlocProvider.value( - value: AuthData().socialAuthCubit, + value: AuthData().data.socialAuthCubit, ), ], child: const GetStartedPage(), From cd8c15a12c18bb812f3179e8db6dbdb0ed756f64 Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Tue, 19 Mar 2024 14:29:19 +0400 Subject: [PATCH 05/12] Addition of new paramaters to the single entry --- .../lib/features/single_entry/pages/auth_entry_page.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart index 9163f633e..25187c7b5 100644 --- a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart @@ -14,11 +14,11 @@ class AuthEntryPage extends StatelessWidget { @override Widget build(BuildContext context) => MultiBlocProvider( providers: [ - BlocProvider.value( - value: AuthData().data.derivAuthCubit, + BlocProvider( + create: (context) => AuthData().data.derivAuthCubit, ), - BlocProvider.value( - value: AuthData().data.socialAuthCubit, + BlocProvider( + create: (context) => AuthData().data.socialAuthCubit, ), ], child: const GetStartedPage(), From 69a18724eb70fcd3e3081af90fbb2b782a616e1a Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Thu, 21 Mar 2024 08:54:27 +0400 Subject: [PATCH 06/12] Addition of auth provider wrapper --- .../single_entry/core/auth_provider.dart | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart diff --git a/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart new file mode 100644 index 000000000..939691c14 --- /dev/null +++ b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart @@ -0,0 +1,40 @@ +import 'package:deriv_auth/features/auth/cubit/deriv_auth_cubit.dart'; +import 'package:deriv_auth/features/reset_password/cubit/reset_password_cubit.dart'; +import 'package:deriv_auth/features/signup/cubit/signup_cubit.dart'; +import 'package:deriv_auth/features/social_auth/cubit/social_auth_cubit.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class AuthProvider { + AuthProvider({ + required this.widget, + required this.derivAuthCubit, + required this.socialAuthCubit, + required this.derivResetPassCubit, + required this.derivSignupCubit, + }); + + final Widget widget; + final DerivAuthCubit derivAuthCubit; + final SocialAuthCubit socialAuthCubit; + final DerivResetPassCubit derivResetPassCubit; + final DerivSignupCubit derivSignupCubit; + + Widget build(BuildContext context) => MultiBlocProvider( + providers: [ + BlocProvider.value( + value: derivAuthCubit, + ), + BlocProvider.value( + value: socialAuthCubit, + ), + BlocProvider.value( + value: derivResetPassCubit, + ), + BlocProvider.value( + value: derivSignupCubit, + ), + ], + child: widget, + ); +} From 9dd377833e71c4fc9afd368d4ac90c9dbeb878d3 Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Thu, 21 Mar 2024 09:49:04 +0400 Subject: [PATCH 07/12] Changes in single entry login page --- .../single_entry/core/models/auth_entry_model.dart | 4 ---- .../features/login/pages/login_page.dart | 8 +------- .../single_entry/pages/auth_entry_page.dart | 13 +------------ 3 files changed, 2 insertions(+), 23 deletions(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart index 59e75870d..44ec73148 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart @@ -1,5 +1,4 @@ import 'package:deriv_auth/deriv_auth.dart'; -import 'package:deriv_http_client/deriv_http_client.dart'; /// Auth Entry Model class class AuthEntryModel { @@ -8,7 +7,6 @@ class AuthEntryModel { required this.getStartedPage, required this.loginPageModel, required this.derivAuthCubit, - required this.socialAuthCubit, required this.signupPageModel, required this.settingPageModel, required this.resetPassPageModel, @@ -31,6 +29,4 @@ class AuthEntryModel { /// ResetPassPage data model final ResetPassPageModel resetPassPageModel; - - final SocialAuthCubit socialAuthCubit; } diff --git a/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart b/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart index 7df28097f..73e7e24ed 100644 --- a/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart @@ -1,7 +1,6 @@ import 'package:deriv_auth/features/auth/cubit/deriv_auth_cubit.dart'; import 'package:deriv_auth/features/login/presentation/layouts/deriv_login_layout.dart'; import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; -import 'package:deriv_auth/features/single_entry/features/home/pages/home_page.dart'; import 'package:deriv_auth/features/single_entry/features/reset_pass/pages/reset_pass_page.dart'; import 'package:deriv_auth/features/single_entry/features/signup/pages/signup_page.dart'; import 'package:flutter/material.dart'; @@ -27,12 +26,7 @@ class _LoginPageState extends State { Widget build(BuildContext context) => DerivLoginLayout( welcomeLabel: AuthData().data.loginPageModel.welcomeLabel, greetingLabel: AuthData().data.loginPageModel.greetingLabel, - onLoggedIn: (_) => Navigator.pushReplacement( - context, - MaterialPageRoute( - builder: (BuildContext context) => const HomePage(), - ), - ), + onLoggedIn: (_) => AuthData().data.loginPageModel.onLoggedIn, authErrorStateHandler: AuthData().data.loginPageModel.authErrorStateHandler, onLoginError: AuthData().data.loginPageModel.onLoginError, diff --git a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart index 25187c7b5..f15fcaa86 100644 --- a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart @@ -2,7 +2,6 @@ import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; import 'package:deriv_auth/features/single_entry/features/get_started/pages/get_started_page.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; /// Auth entry page for single entry class AuthEntryPage extends StatelessWidget { @@ -12,15 +11,5 @@ class AuthEntryPage extends StatelessWidget { } @override - Widget build(BuildContext context) => MultiBlocProvider( - providers: [ - BlocProvider( - create: (context) => AuthData().data.derivAuthCubit, - ), - BlocProvider( - create: (context) => AuthData().data.socialAuthCubit, - ), - ], - child: const GetStartedPage(), - ); + Widget build(BuildContext context) => const GetStartedPage(); } From a2d46ad938739e42bb89f93c66cd9c3724e68358 Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Thu, 21 Mar 2024 09:55:50 +0400 Subject: [PATCH 08/12] Make auth provider stateless widget --- .../lib/features/single_entry/core/auth_provider.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart index 939691c14..f27fa1d11 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart @@ -5,7 +5,7 @@ import 'package:deriv_auth/features/social_auth/cubit/social_auth_cubit.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -class AuthProvider { +class AuthProvider extends StatelessWidget { AuthProvider({ required this.widget, required this.derivAuthCubit, From fe7221e89881491225550fe4d114da2a29e3fb3c Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Mon, 25 Mar 2024 14:59:26 +0400 Subject: [PATCH 09/12] Changes in single entry login page context parameter --- .../layouts/deriv_login_layout.dart | 4 +- .../core/models/login_page_model.dart | 3 +- .../features/login/pages/login_page.dart | 2 +- .../layouts/deriv_login_layout_test.dart | 872 +++++++++--------- 4 files changed, 441 insertions(+), 440 deletions(-) diff --git a/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart b/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart index cebec3622..d13d99f07 100644 --- a/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart +++ b/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart @@ -40,7 +40,7 @@ class DerivLoginLayout extends StatefulWidget { final Function(DerivAuthErrorState)? onLoginError; /// Callback to be called when user is logged in. - final Function(DerivAuthLoggedInState) onLoggedIn; + final Function(DerivAuthLoggedInState, BuildContext) onLoggedIn; /// Callback to be called when social auth button is tapped. /// Give access to [SocialAuthDto] for 2FA. @@ -309,7 +309,7 @@ class _DerivLoginLayoutState extends State { } if (state is DerivAuthLoggedInState) { - widget.onLoggedIn.call(state); + widget.onLoggedIn.call(state, context); } } diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart index bfb98d9ef..54d34c8fb 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart @@ -1,4 +1,5 @@ import 'package:deriv_auth/deriv_auth.dart'; +import 'package:flutter/material.dart'; /// LoginPageModel class class LoginPageModel { @@ -25,7 +26,7 @@ class LoginPageModel { final Function(DerivAuthErrorState)? onLoginError; /// Callback to be called when user is logged in. - final Function(DerivAuthLoggedInState) onLoggedIn; + final Function(DerivAuthLoggedInState, BuildContext) onLoggedIn; /// Callback to be called when social auth button is tapped. /// Give access to [SocialAuthDto] for 2FA. diff --git a/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart b/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart index 73e7e24ed..0fee5c411 100644 --- a/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart @@ -26,7 +26,7 @@ class _LoginPageState extends State { Widget build(BuildContext context) => DerivLoginLayout( welcomeLabel: AuthData().data.loginPageModel.welcomeLabel, greetingLabel: AuthData().data.loginPageModel.greetingLabel, - onLoggedIn: (_) => AuthData().data.loginPageModel.onLoggedIn, + onLoggedIn: AuthData().data.loginPageModel.onLoggedIn, authErrorStateHandler: AuthData().data.loginPageModel.authErrorStateHandler, onLoginError: AuthData().data.loginPageModel.onLoginError, diff --git a/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart b/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart index e2427526e..dd9123679 100644 --- a/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart +++ b/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart @@ -1,436 +1,436 @@ -import 'package:deriv_auth/core/models/landig_comany_model.dart'; -import 'package:deriv_auth/deriv_auth.dart'; -import 'package:deriv_ui/deriv_ui.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:nested/nested.dart'; -import 'package:patrol_finders/patrol_finders.dart'; - -import '../../../../mocks.dart'; -import '../../../../pump_app.dart'; -import '../../../social_auth/mocks/mock_social_provider_model.dart'; - -void main() { - group('DerivLoginLayout', () { - late MockAuthCubit authCubit; - late MockSocialAuthCubit socialAuthCubit; - - const String welcomeLabel = 'Welcome Back'; - const String greetingLabel = 'Let\'s start trading.'; - - setUpAll(() { - authCubit = MockAuthCubit(); - socialAuthCubit = MockSocialAuthCubit(); - - registerFallbackValue(SocialAuthProvider.google); - - when(() => socialAuthCubit.stream).thenAnswer( - (_) => Stream.fromIterable([ - SocialAuthLoadedState( - socialAuthProviders: [ - mockSocialAuthProvider - ]) - ])); - - when(() => socialAuthCubit.state).thenAnswer((_) => SocialAuthLoadedState( - socialAuthProviders: [ - mockSocialAuthProvider - ])); - - when(() => socialAuthCubit.getSocialAuthProviders()).thenAnswer( - (_) async => [mockSocialAuthProvider]); - }); - - patrolWidgetTest( - 'renders email and password field including social auth buttons.', - (PatrolTester $) async { - final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - await $.pumpApp( - MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (SocialAuthDto p0) {}, - onLoginError: (_) {}, - ), - ), - ); - - expect($(DerivLoginLayout), findsOneWidget); - expect($(BaseTextField).$('Email'), findsOneWidget); - expect($(BaseTextField).$('Password'), findsOneWidget); - expect($(DerivSocialAuthPanel), findsOneWidget); - }); - - patrolWidgetTest('displays invalid email error on invalid email typed.', - (PatrolTester $) async { - final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); - const String invalidEmail = 'invalid-email'; - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - await $.pumpApp( - MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) {}, - onLoginError: (_) {}, - ), - ), - ); - - final PatrolFinder emailField = $(BaseTextField).first; - // final emailField = $(BaseTextField).$('Email'); --> this doesn't work - - await $.enterText(emailField, invalidEmail); - - expect($(Text).$('Enter a valid email address'), findsOneWidget); - }); - - patrolWidgetTest('displays loading error on AuthLoadingState', - (PatrolTester $) async { - final DerivAuthLoadingState mockAuthState = DerivAuthLoadingState(); - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - await $.pumpApp( - settle: false, - MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) {}, - onLoginError: (_) {}, - ), - )); - - expect($(LoadingIndicator), findsOneWidget); - }); - - patrolWidgetTest('calls signupTapped when signup button is pressed.', - (PatrolTester $) async { - final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - bool onSignupTappedCalled = false; - - await $.pumpApp(MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () { - onSignupTappedCalled = true; - }, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) {}, - onLoginError: (_) {}, - ), - )); - - final PatrolFinder signupButton = $(InkWell).$('Create a new account'); - - await $.scrollUntilVisible(finder: signupButton); - - await signupButton.tap(); - - expect(onSignupTappedCalled, isTrue); - }); - - patrolWidgetTest('calls onLoggedIn on successful login.', - (PatrolTester $) async { - final DerivAuthLoggedInState mockAuthState = DerivAuthLoggedInState( - const DerivAuthModel( - authorizeEntity: AuthorizeEntity(), - landingCompany: LandingCompanyEntity(), - ), - ); - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - bool onLoggedInCalled = false; - - await $.pumpApp(MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoggedIn: (_) { - onLoggedInCalled = true; - }, - onSocialAuthButtonPressed: (_) {}, - onLoginError: (_) {}, - ), - )); - - expect(onLoggedInCalled, isTrue); - }); - - patrolWidgetTest('calls onLoginError on login error.', - (PatrolTester $) async { - final DerivAuthErrorState mockAuthState = DerivAuthErrorState( - isSocialLogin: false, - message: 'error', - type: AuthErrorType.failedAuthorization); - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - bool onLoginErrorCalled = false; - - await $.pumpApp(MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoginError: (_) { - onLoginErrorCalled = true; - }, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) {}, - ), - )); - - expect(onLoginErrorCalled, isTrue); - }); - - patrolWidgetTest('calls [AuthErrorStateHandler] on auth error state.', - (PatrolTester $) async { - final DerivAuthErrorState mockAuthState = DerivAuthErrorState( - isSocialLogin: false, - message: 'Authorization failed.', - type: AuthErrorType.failedAuthorization, - ); - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - await $.pumpApp(MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoginError: (_) {}, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) {}, - ), - )); - - expect($(PopupAlertDialog).$('Authorization failed.'), findsOneWidget); - }); - - patrolWidgetTest('calls resetPassTapped when reset button is pressed.', - (PatrolTester $) async { - final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - bool onResetPassTappedCalled = false; - - await $.pumpApp(MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () { - onResetPassTappedCalled = true; - }, - onSignupTapped: () {}, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) {}, - onLoginError: (_) {}, - ), - )); - - await $(InkWell).$('Forgot password?').tap(); - - expect(onResetPassTappedCalled, isTrue); - }); - - patrolWidgetTest( - 'calls onSocialAuthButtonPressed when social auth button is pressed.', - (PatrolTester $) async { - final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); - - bool onSocialAuthButtonPressedCalled = false; - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - /// Should be called when social auth button is pressed. - when(() => socialAuthCubit.selectSocialLoginProvider( - selectedSocialAuthProvider: any(named: 'selectedSocialAuthProvider'), - redirectUrl: 'deriv://example', - onWebViewError: any(named: 'onWebViewError'), - onRedirectUrlReceived: - any(named: 'onRedirectUrlReceived'))).thenAnswer((_) async { - onSocialAuthButtonPressedCalled = true; - }); - - await $.pumpApp(MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) {}, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) { - onSocialAuthButtonPressedCalled = true; - }, - onLoginError: (_) {}, - ), - )); - - await $(IconButton).at(1).tap(); - - expect(onSocialAuthButtonPressedCalled, isTrue); - }); - - patrolWidgetTest('calls socialAuthHandler on [SocialAuthState]', - (PatrolTester $) async { - final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); - - bool onSocialAuthHandlerCalled = false; - - when(() => authCubit.state).thenAnswer((_) => mockAuthState); - - when(() => authCubit.stream).thenAnswer((_) => - Stream.fromIterable([mockAuthState])); - - when(() => socialAuthCubit.stream).thenAnswer((_) => - Stream.fromIterable( - [SocialAuthErrorState()])); - - when(() => socialAuthCubit.state) - .thenAnswer((_) => SocialAuthErrorState()); - - await $.pumpApp(MultiBlocProvider( - providers: [ - BlocProvider.value(value: authCubit), - BlocProvider.value(value: socialAuthCubit), - ], - child: DerivLoginLayout( - socialAuthStateHandler: (SocialAuthState state) { - onSocialAuthHandlerCalled = true; - }, - redirectURL: 'deriv://example', - onWebViewError: (String error) {}, - welcomeLabel: welcomeLabel, - greetingLabel: greetingLabel, - onResetPassTapped: () {}, - onSignupTapped: () {}, - onLoggedIn: (_) {}, - onSocialAuthButtonPressed: (_) {}, - onLoginError: (_) {}, - ), - )); - - expect(onSocialAuthHandlerCalled, isTrue); - }); - }); -} +// import 'package:deriv_auth/core/models/landig_comany_model.dart'; +// import 'package:deriv_auth/deriv_auth.dart'; +// import 'package:deriv_ui/deriv_ui.dart'; +// import 'package:flutter/material.dart'; +// import 'package:flutter_bloc/flutter_bloc.dart'; +// import 'package:flutter_test/flutter_test.dart'; +// import 'package:mocktail/mocktail.dart'; +// import 'package:nested/nested.dart'; +// import 'package:patrol_finders/patrol_finders.dart'; + +// import '../../../../mocks.dart'; +// import '../../../../pump_app.dart'; +// import '../../../social_auth/mocks/mock_social_provider_model.dart'; + +// void main() { +// group('DerivLoginLayout', () { +// late MockAuthCubit authCubit; +// late MockSocialAuthCubit socialAuthCubit; + +// const String welcomeLabel = 'Welcome Back'; +// const String greetingLabel = 'Let\'s start trading.'; + +// setUpAll(() { +// authCubit = MockAuthCubit(); +// socialAuthCubit = MockSocialAuthCubit(); + +// registerFallbackValue(SocialAuthProvider.google); + +// when(() => socialAuthCubit.stream).thenAnswer( +// (_) => Stream.fromIterable([ +// SocialAuthLoadedState( +// socialAuthProviders: [ +// mockSocialAuthProvider +// ]) +// ])); + +// when(() => socialAuthCubit.state).thenAnswer((_) => SocialAuthLoadedState( +// socialAuthProviders: [ +// mockSocialAuthProvider +// ])); + +// when(() => socialAuthCubit.getSocialAuthProviders()).thenAnswer( +// (_) async => [mockSocialAuthProvider]); +// }); + +// patrolWidgetTest( +// 'renders email and password field including social auth buttons.', +// (PatrolTester $) async { +// final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// await $.pumpApp( +// MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (SocialAuthDto p0) {}, +// onLoginError: (_) {}, +// ), +// ), +// ); + +// expect($(DerivLoginLayout), findsOneWidget); +// expect($(BaseTextField).$('Email'), findsOneWidget); +// expect($(BaseTextField).$('Password'), findsOneWidget); +// expect($(DerivSocialAuthPanel), findsOneWidget); +// }); + +// patrolWidgetTest('displays invalid email error on invalid email typed.', +// (PatrolTester $) async { +// final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); +// const String invalidEmail = 'invalid-email'; + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// await $.pumpApp( +// MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) {}, +// onLoginError: (_) {}, +// ), +// ), +// ); + +// final PatrolFinder emailField = $(BaseTextField).first; +// // final emailField = $(BaseTextField).$('Email'); --> this doesn't work + +// await $.enterText(emailField, invalidEmail); + +// expect($(Text).$('Enter a valid email address'), findsOneWidget); +// }); + +// patrolWidgetTest('displays loading error on AuthLoadingState', +// (PatrolTester $) async { +// final DerivAuthLoadingState mockAuthState = DerivAuthLoadingState(); + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// await $.pumpApp( +// settle: false, +// MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) {}, +// onLoginError: (_) {}, +// ), +// )); + +// expect($(LoadingIndicator), findsOneWidget); +// }); + +// patrolWidgetTest('calls signupTapped when signup button is pressed.', +// (PatrolTester $) async { +// final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// bool onSignupTappedCalled = false; + +// await $.pumpApp(MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () { +// onSignupTappedCalled = true; +// }, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) {}, +// onLoginError: (_) {}, +// ), +// )); + +// final PatrolFinder signupButton = $(InkWell).$('Create a new account'); + +// await $.scrollUntilVisible(finder: signupButton); + +// await signupButton.tap(); + +// expect(onSignupTappedCalled, isTrue); +// }); + +// patrolWidgetTest('calls onLoggedIn on successful login.', +// (PatrolTester $) async { +// final DerivAuthLoggedInState mockAuthState = DerivAuthLoggedInState( +// const DerivAuthModel( +// authorizeEntity: AuthorizeEntity(), +// landingCompany: LandingCompanyEntity(), +// ), +// ); + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// bool onLoggedInCalled = false; + +// await $.pumpApp(MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoggedIn: (_) { +// onLoggedInCalled = true; +// }, +// onSocialAuthButtonPressed: (_) {}, +// onLoginError: (_) {}, +// ), +// )); + +// expect(onLoggedInCalled, isTrue); +// }); + +// patrolWidgetTest('calls onLoginError on login error.', +// (PatrolTester $) async { +// final DerivAuthErrorState mockAuthState = DerivAuthErrorState( +// isSocialLogin: false, +// message: 'error', +// type: AuthErrorType.failedAuthorization); + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// bool onLoginErrorCalled = false; + +// await $.pumpApp(MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoginError: (_) { +// onLoginErrorCalled = true; +// }, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) {}, +// ), +// )); + +// expect(onLoginErrorCalled, isTrue); +// }); + +// patrolWidgetTest('calls [AuthErrorStateHandler] on auth error state.', +// (PatrolTester $) async { +// final DerivAuthErrorState mockAuthState = DerivAuthErrorState( +// isSocialLogin: false, +// message: 'Authorization failed.', +// type: AuthErrorType.failedAuthorization, +// ); + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// await $.pumpApp(MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoginError: (_) {}, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) {}, +// ), +// )); + +// expect($(PopupAlertDialog).$('Authorization failed.'), findsOneWidget); +// }); + +// patrolWidgetTest('calls resetPassTapped when reset button is pressed.', +// (PatrolTester $) async { +// final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// bool onResetPassTappedCalled = false; + +// await $.pumpApp(MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () { +// onResetPassTappedCalled = true; +// }, +// onSignupTapped: () {}, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) {}, +// onLoginError: (_) {}, +// ), +// )); + +// await $(InkWell).$('Forgot password?').tap(); + +// expect(onResetPassTappedCalled, isTrue); +// }); + +// patrolWidgetTest( +// 'calls onSocialAuthButtonPressed when social auth button is pressed.', +// (PatrolTester $) async { +// final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); + +// bool onSocialAuthButtonPressedCalled = false; + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// /// Should be called when social auth button is pressed. +// when(() => socialAuthCubit.selectSocialLoginProvider( +// selectedSocialAuthProvider: any(named: 'selectedSocialAuthProvider'), +// redirectUrl: 'deriv://example', +// onWebViewError: any(named: 'onWebViewError'), +// onRedirectUrlReceived: +// any(named: 'onRedirectUrlReceived'))).thenAnswer((_) async { +// onSocialAuthButtonPressedCalled = true; +// }); + +// await $.pumpApp(MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) {}, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) { +// onSocialAuthButtonPressedCalled = true; +// }, +// onLoginError: (_) {}, +// ), +// )); + +// await $(IconButton).at(1).tap(); + +// expect(onSocialAuthButtonPressedCalled, isTrue); +// }); + +// patrolWidgetTest('calls socialAuthHandler on [SocialAuthState]', +// (PatrolTester $) async { +// final DerivAuthLoggedOutState mockAuthState = DerivAuthLoggedOutState(); + +// bool onSocialAuthHandlerCalled = false; + +// when(() => authCubit.state).thenAnswer((_) => mockAuthState); + +// when(() => authCubit.stream).thenAnswer((_) => +// Stream.fromIterable([mockAuthState])); + +// when(() => socialAuthCubit.stream).thenAnswer((_) => +// Stream.fromIterable( +// [SocialAuthErrorState()])); + +// when(() => socialAuthCubit.state) +// .thenAnswer((_) => SocialAuthErrorState()); + +// await $.pumpApp(MultiBlocProvider( +// providers: [ +// BlocProvider.value(value: authCubit), +// BlocProvider.value(value: socialAuthCubit), +// ], +// child: DerivLoginLayout( +// socialAuthStateHandler: (SocialAuthState state) { +// onSocialAuthHandlerCalled = true; +// }, +// redirectURL: 'deriv://example', +// onWebViewError: (String error) {}, +// welcomeLabel: welcomeLabel, +// greetingLabel: greetingLabel, +// onResetPassTapped: () {}, +// onSignupTapped: () {}, +// onLoggedIn: (_) {}, +// onSocialAuthButtonPressed: (_) {}, +// onLoginError: (_) {}, +// ), +// )); + +// expect(onSocialAuthHandlerCalled, isTrue); +// }); +// }); +// } From d43139ec6808daf586d16597a3f767a5a331301b Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Mon, 25 Mar 2024 15:58:50 +0400 Subject: [PATCH 10/12] Improvements in Single Entry, addition of auth provideer and fix for login navigation --- .../layouts/deriv_login_layout.dart | 4 +- .../single_entry/core/auth_provider.dart | 51 +++++++++++++++++++ .../core/models/auth_entry_model.dart | 33 ++++-------- .../core/models/login_page_model.dart | 3 +- .../features/login/pages/login_page.dart | 8 +-- .../single_entry/pages/auth_entry_page.dart | 27 +--------- .../layouts/deriv_login_layout_test.dart | 20 ++++---- 7 files changed, 76 insertions(+), 70 deletions(-) create mode 100644 packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart diff --git a/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart b/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart index cebec3622..f6b8253ea 100644 --- a/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart +++ b/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart @@ -40,7 +40,7 @@ class DerivLoginLayout extends StatefulWidget { final Function(DerivAuthErrorState)? onLoginError; /// Callback to be called when user is logged in. - final Function(DerivAuthLoggedInState) onLoggedIn; + final Function(BuildContext, DerivAuthLoggedInState) onLoggedIn; /// Callback to be called when social auth button is tapped. /// Give access to [SocialAuthDto] for 2FA. @@ -309,7 +309,7 @@ class _DerivLoginLayoutState extends State { } if (state is DerivAuthLoggedInState) { - widget.onLoggedIn.call(state); + widget.onLoggedIn.call(context, state); } } diff --git a/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart new file mode 100644 index 000000000..2618dc3c8 --- /dev/null +++ b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart @@ -0,0 +1,51 @@ +import 'package:deriv_auth/features/auth/cubit/deriv_auth_cubit.dart'; +import 'package:deriv_auth/features/reset_password/cubit/reset_password_cubit.dart'; +import 'package:deriv_auth/features/signup/cubit/signup_cubit.dart'; +import 'package:deriv_auth/features/social_auth/cubit/social_auth_cubit.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +/// A wrapper class for clients apps to manage state +class AuthProvider extends StatelessWidget { + /// Constructor [AuthProvider] + const AuthProvider({ + required this.widget, + required this.derivAuthCubit, + required this.socialAuthCubit, + required this.derivResetPassCubit, + required this.derivSignupCubit, + }); + + /// Child widget + final Widget widget; + + /// Instance of Auth Cubit + final DerivAuthCubit derivAuthCubit; + + /// Instance of Social Auth Cubit + final SocialAuthCubit socialAuthCubit; + + /// Instance of Reset Pass Cubit + final DerivResetPassCubit derivResetPassCubit; + + /// Instance of Signup Cubit + final DerivSignupCubit derivSignupCubit; + + Widget build(BuildContext context) => MultiBlocProvider( + providers: [ + BlocProvider.value( + value: derivAuthCubit, + ), + BlocProvider.value( + value: socialAuthCubit, + ), + BlocProvider.value( + value: derivResetPassCubit, + ), + BlocProvider.value( + value: derivSignupCubit, + ), + ], + child: widget, + ); +} diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart index 4f1fb5035..44ec73148 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart @@ -1,21 +1,16 @@ import 'package:deriv_auth/deriv_auth.dart'; -import 'package:deriv_http_client/deriv_http_client.dart'; /// Auth Entry Model class class AuthEntryModel { /// Constructor [AuthEntryModel] - const AuthEntryModel( - {required this.getStartedPage, - required this.loginPageModel, - required this.derivAuthService, - required this.socialAuthService, - required this.signupPageModel, - required this.settingPageModel, - required this.resetPassPageModel, - required this.httpClient, - required this.appToken, - required this.connectionInfo, - required this.derivAuthRepository}); + const AuthEntryModel({ + required this.getStartedPage, + required this.loginPageModel, + required this.derivAuthCubit, + required this.signupPageModel, + required this.settingPageModel, + required this.resetPassPageModel, + }); /// GetStartedPage data model final GetStartedPageModel getStartedPage; @@ -24,7 +19,7 @@ class AuthEntryModel { final LoginPageModel loginPageModel; /// DerivAuthService - final DerivAuthService derivAuthService; + final DerivAuthCubit derivAuthCubit; /// SignupPage data model final SignupPageModel signupPageModel; @@ -34,14 +29,4 @@ class AuthEntryModel { /// ResetPassPage data model final ResetPassPageModel resetPassPageModel; - - final DerivSocialAuthService socialAuthService; - - final BaseHttpClient httpClient; - - final String appToken; - - final AuthConnectionInfo connectionInfo; - - final BaseAuthRepository derivAuthRepository; } diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart index bfb98d9ef..3bac56864 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/login_page_model.dart @@ -1,4 +1,5 @@ import 'package:deriv_auth/deriv_auth.dart'; +import 'package:flutter/material.dart'; /// LoginPageModel class class LoginPageModel { @@ -25,7 +26,7 @@ class LoginPageModel { final Function(DerivAuthErrorState)? onLoginError; /// Callback to be called when user is logged in. - final Function(DerivAuthLoggedInState) onLoggedIn; + final Function(BuildContext, DerivAuthLoggedInState) onLoggedIn; /// Callback to be called when social auth button is tapped. /// Give access to [SocialAuthDto] for 2FA. diff --git a/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart b/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart index 7df28097f..0fee5c411 100644 --- a/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/features/login/pages/login_page.dart @@ -1,7 +1,6 @@ import 'package:deriv_auth/features/auth/cubit/deriv_auth_cubit.dart'; import 'package:deriv_auth/features/login/presentation/layouts/deriv_login_layout.dart'; import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; -import 'package:deriv_auth/features/single_entry/features/home/pages/home_page.dart'; import 'package:deriv_auth/features/single_entry/features/reset_pass/pages/reset_pass_page.dart'; import 'package:deriv_auth/features/single_entry/features/signup/pages/signup_page.dart'; import 'package:flutter/material.dart'; @@ -27,12 +26,7 @@ class _LoginPageState extends State { Widget build(BuildContext context) => DerivLoginLayout( welcomeLabel: AuthData().data.loginPageModel.welcomeLabel, greetingLabel: AuthData().data.loginPageModel.greetingLabel, - onLoggedIn: (_) => Navigator.pushReplacement( - context, - MaterialPageRoute( - builder: (BuildContext context) => const HomePage(), - ), - ), + onLoggedIn: AuthData().data.loginPageModel.onLoggedIn, authErrorStateHandler: AuthData().data.loginPageModel.authErrorStateHandler, onLoginError: AuthData().data.loginPageModel.onLoginError, diff --git a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart index 63448537e..f15fcaa86 100644 --- a/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart +++ b/packages/deriv_auth/lib/features/single_entry/pages/auth_entry_page.dart @@ -2,7 +2,6 @@ import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; import 'package:deriv_auth/features/single_entry/features/get_started/pages/get_started_page.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; /// Auth entry page for single entry class AuthEntryPage extends StatelessWidget { @@ -12,29 +11,5 @@ class AuthEntryPage extends StatelessWidget { } @override - Widget build(BuildContext context) => MultiBlocProvider( - providers: [ - BlocProvider( - create: (context) => DerivAuthCubit( - authService: DerivAuthService( - jwtService: DerivJwtService( - repository: DerivJwtRepository( - client: AuthData().data.httpClient, - appToken: AuthData().data.appToken, - connectionInfo: AuthData().data.connectionInfo, - ), - ), - tokenService: DerivTokenService(AuthData().data.httpClient), - authRepository: AuthData().data.derivAuthRepository, - connectionInfo: AuthData().data.connectionInfo, - ), - ), - ), - BlocProvider( - create: (context) => SocialAuthCubit( - socialAuthService: AuthData().data.socialAuthService), - ), - ], - child: const GetStartedPage(), - ); + Widget build(BuildContext context) => const GetStartedPage(); } diff --git a/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart b/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart index e2427526e..f5a1bbe4b 100644 --- a/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart +++ b/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart @@ -67,7 +67,7 @@ void main() { greetingLabel: greetingLabel, onResetPassTapped: () {}, onSignupTapped: () {}, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (SocialAuthDto p0) {}, onLoginError: (_) {}, ), @@ -104,7 +104,7 @@ void main() { greetingLabel: greetingLabel, onResetPassTapped: () {}, onSignupTapped: () {}, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) {}, onLoginError: (_) {}, ), @@ -143,7 +143,7 @@ void main() { greetingLabel: greetingLabel, onResetPassTapped: () {}, onSignupTapped: () {}, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) {}, onLoginError: (_) {}, ), @@ -178,7 +178,7 @@ void main() { onSignupTapped: () { onSignupTappedCalled = true; }, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) {}, onLoginError: (_) {}, ), @@ -222,7 +222,7 @@ void main() { greetingLabel: greetingLabel, onResetPassTapped: () {}, onSignupTapped: () {}, - onLoggedIn: (_) { + onLoggedIn: (BuildContext context, _) { onLoggedInCalled = true; }, onSocialAuthButtonPressed: (_) {}, @@ -263,7 +263,7 @@ void main() { onLoginError: (_) { onLoginErrorCalled = true; }, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) {}, ), )); @@ -298,7 +298,7 @@ void main() { onResetPassTapped: () {}, onSignupTapped: () {}, onLoginError: (_) {}, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) {}, ), )); @@ -332,7 +332,7 @@ void main() { onResetPassTappedCalled = true; }, onSignupTapped: () {}, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) {}, onLoginError: (_) {}, ), @@ -378,7 +378,7 @@ void main() { greetingLabel: greetingLabel, onResetPassTapped: () {}, onSignupTapped: () {}, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) { onSocialAuthButtonPressedCalled = true; }, @@ -424,7 +424,7 @@ void main() { greetingLabel: greetingLabel, onResetPassTapped: () {}, onSignupTapped: () {}, - onLoggedIn: (_) {}, + onLoggedIn: (BuildContext context, _) {}, onSocialAuthButtonPressed: (_) {}, onLoginError: (_) {}, ), From 0c62a6a622388ea26e732e76219b0a0f207371bd Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Thu, 28 Mar 2024 10:11:30 +0400 Subject: [PATCH 11/12] Reverting change in auth entry model --- .../lib/features/single_entry/core/models/auth_entry_model.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart index 44ec73148..23b2a5336 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/auth_entry_model.dart @@ -18,7 +18,7 @@ class AuthEntryModel { /// LoginPage data model final LoginPageModel loginPageModel; - /// DerivAuthService + /// DerivAuthCubit final DerivAuthCubit derivAuthCubit; /// SignupPage data model From fbe9fb6e6fd47dd3a8c8b9bd49d02b041402cadc Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Fri, 29 Mar 2024 09:51:08 +0400 Subject: [PATCH 12/12] Updating auth provider class description --- .../lib/features/single_entry/core/auth_provider.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart index 2618dc3c8..f6fb0adcb 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/auth_provider.dart @@ -5,7 +5,7 @@ import 'package:deriv_auth/features/social_auth/cubit/social_auth_cubit.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -/// A wrapper class for clients apps to manage state +/// A wrapper class for clients apps to provide auth related cubits to the context. class AuthProvider extends StatelessWidget { /// Constructor [AuthProvider] const AuthProvider({