From 0a1e1422c2ad21e5142c4da26367f724a8041b11 Mon Sep 17 00:00:00 2001 From: akhil-deriv Date: Thu, 4 Jul 2024 13:53:57 +0400 Subject: [PATCH] updated deriv auth authorize methods --- .../features/auth/cubit/deriv_auth_cubit.dart | 58 ++++++++++++++----- .../lib/features/auth/deriv_auth_io.dart | 10 +++- .../auth/cubit/deriv_auth_cubit_test.dart | 34 +++++------ 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart b/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart index aa1f5599e..14673bbec 100644 --- a/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart +++ b/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart @@ -100,25 +100,32 @@ class DerivAuthCubit extends Cubit } @override - Future tokenLogin(String token, {List? tokenList}) async { + Future multiTokenLogin(String? token) async { emit(DerivAuthLoadingState()); - List accountTokens = []; - if (tokenList == null) { - final List accountList = - await authService.getLatestAccounts(); - accountTokens = accountList - .where((AccountModel account) => account.token != null) - .map((AccountModel account) => account.token!) - .toList(); - } else { - accountTokens = tokenList; - } - if (!accountTokens.contains(token)) { - accountTokens.add(token); + + final List accountList = + await authService.getLatestAccounts(); + final List tokenList = accountList + .where((AccountModel account) => account.token != null) + .map((AccountModel account) => account.token!) + .toList(); + + if (token != null && !tokenList.contains(token)) { + tokenList.add(token); } await _tokenLoginRequest( 'MULTI', - tokenList: accountTokens, + tokenList: tokenList, + accounts: accountList, + ); + } + + @override + Future tokenLogin(String token) async { + emit(DerivAuthLoadingState()); + + await _tokenLoginRequest( + token, accounts: await authService.getLatestAccounts(), ); } @@ -189,7 +196,7 @@ class DerivAuthCubit extends Cubit } @override - Future authorizeDefaultAccount() async { + Future multiAuthorizeDefaultAccount() async { emit(DerivAuthLoadingState()); List tokenList = []; @@ -219,6 +226,25 @@ class DerivAuthCubit extends Cubit ); } + @override + Future authorizeDefaultAccount() async { + emit(DerivAuthLoadingState()); + + final String? defaultAccountToken = + (await authService.getDefaultAccount())?.token; + + if (defaultAccountToken == null) { + emit(DerivAuthLoggedOutState()); + + return; + } + + await _tokenLoginRequest( + defaultAccountToken, + accounts: await authService.getLatestAccounts(), + ); + } + @override Future logout() async { if (state is DerivAuthLoggedOutState) { diff --git a/packages/deriv_auth/lib/features/auth/deriv_auth_io.dart b/packages/deriv_auth/lib/features/auth/deriv_auth_io.dart index c72401b34..b3fe99bad 100644 --- a/packages/deriv_auth/lib/features/auth/deriv_auth_io.dart +++ b/packages/deriv_auth/lib/features/auth/deriv_auth_io.dart @@ -27,14 +27,22 @@ abstract class DerivAuthIO { }); /// Log user in with [token] after reset password or sign up. - Future tokenLogin(String token, {List? tokenList}); + @deprecated + Future tokenLogin(String token); + + /// Log user in with multi token authorization after reset password or sign up. + Future multiTokenLogin(String? token); /// Log user out. Future logout(); /// Log default user in. + @deprecated Future authorizeDefaultAccount(); + /// Log default user in using multi authorization. + Future multiAuthorizeDefaultAccount(); + /// Deriv auth output. Stream get output; } diff --git a/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart b/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart index cf14654cd..90518157b 100644 --- a/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart +++ b/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart @@ -91,11 +91,9 @@ void main() { when(() => service.getLandingCompany(any())).thenAnswer((_) => Future.value(const LandingCompanyEntity())); - when(() => service - .login(any(), - accounts: any(named: 'accounts'), - tokenList: [])).thenAnswer( - (_) => Future.value(mockedValidAuthorizeEntity)); + when(() => service.login(any(), accounts: any(named: 'accounts'))) + .thenAnswer((_) => + Future.value(mockedValidAuthorizeEntity)); final List> expectedResponse = >[ @@ -269,11 +267,9 @@ void main() { Future>.value( [mockedAccountModel])); - when(() => service - .login(any(), - accounts: any(named: 'accounts'), - tokenList: [])).thenAnswer( - (_) => Future.value(mockedValidAuthorizeEntity)); + when(() => service.login(any(), accounts: any(named: 'accounts'))) + .thenAnswer((_) => + Future.value(mockedValidAuthorizeEntity)); final List> expectedResponse = >[ @@ -289,11 +285,10 @@ void main() { emitsInOrder(expectedResponse), ); - authCubit.tokenLogin(_token, tokenList: []); + authCubit.tokenLogin(_token); verify( - () => service.login(any(), - accounts: any(named: 'accounts'), tokenList: []), + () => service.login(any(), accounts: any(named: 'accounts')), ).called(1); }); @@ -306,9 +301,10 @@ void main() { Future>.value( [mockedAccountModel])); - when(() => service.login(any(), - accounts: any(named: 'accounts'), - tokenList: any(named: 'tokenList'))).thenThrow(DerivAuthException( + when(() => service.login( + any(), + accounts: any(named: 'accounts'), + )).thenThrow(DerivAuthException( message: 'message', type: AuthErrorType.invalidToken, )); @@ -327,8 +323,10 @@ void main() { authCubit.tokenLogin(_token); verify( - () => service.login(any(), - accounts: any(named: 'accounts'), tokenList: []), + () => service.login( + any(), + accounts: any(named: 'accounts'), + ), ).called(1); });