Skip to content

Commit

Permalink
updated deriv auth authorize methods
Browse files Browse the repository at this point in the history
  • Loading branch information
akhil-deriv committed Jul 4, 2024
1 parent 96fe2f7 commit 0a1e142
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
58 changes: 42 additions & 16 deletions packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,32 @@ class DerivAuthCubit extends Cubit<DerivAuthState>
}

@override
Future<void> tokenLogin(String token, {List<String>? tokenList}) async {
Future<void> multiTokenLogin(String? token) async {
emit(DerivAuthLoadingState());
List<String> accountTokens = <String>[];
if (tokenList == null) {
final List<AccountModel> 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<AccountModel> accountList =
await authService.getLatestAccounts();
final List<String> 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<void> tokenLogin(String token) async {
emit(DerivAuthLoadingState());

await _tokenLoginRequest(
token,
accounts: await authService.getLatestAccounts(),
);
}
Expand Down Expand Up @@ -189,7 +196,7 @@ class DerivAuthCubit extends Cubit<DerivAuthState>
}

@override
Future<void> authorizeDefaultAccount() async {
Future<void> multiAuthorizeDefaultAccount() async {
emit(DerivAuthLoadingState());

List<String> tokenList = <String>[];
Expand Down Expand Up @@ -219,6 +226,25 @@ class DerivAuthCubit extends Cubit<DerivAuthState>
);
}

@override
Future<void> 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<void> logout() async {
if (state is DerivAuthLoggedOutState) {
Expand Down
10 changes: 9 additions & 1 deletion packages/deriv_auth/lib/features/auth/deriv_auth_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ abstract class DerivAuthIO {
});

/// Log user in with [token] after reset password or sign up.
Future<void> tokenLogin(String token, {List<String>? tokenList});
@deprecated
Future<void> tokenLogin(String token);

/// Log user in with multi token authorization after reset password or sign up.
Future<void> multiTokenLogin(String? token);

/// Log user out.
Future<void> logout();

/// Log default user in.
@deprecated
Future<void> authorizeDefaultAccount();

/// Log default user in using multi authorization.
Future<void> multiAuthorizeDefaultAccount();

/// Deriv auth output.
Stream<DerivAuthState> get output;
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ void main() {
when(() => service.getLandingCompany(any())).thenAnswer((_) =>
Future<LandingCompanyEntity>.value(const LandingCompanyEntity()));

when(() => service
.login(any(),
accounts: any(named: 'accounts'),
tokenList: <String>[])).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service.login(any(), accounts: any(named: 'accounts')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
Expand Down Expand Up @@ -269,11 +267,9 @@ void main() {
Future<List<AccountModel>>.value(
<AccountModel>[mockedAccountModel]));

when(() => service
.login(any(),
accounts: any(named: 'accounts'),
tokenList: <String>[])).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service.login(any(), accounts: any(named: 'accounts')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
Expand All @@ -289,11 +285,10 @@ void main() {
emitsInOrder(expectedResponse),
);

authCubit.tokenLogin(_token, tokenList: <String>[]);
authCubit.tokenLogin(_token);

verify(
() => service.login(any(),
accounts: any(named: 'accounts'), tokenList: <String>[]),
() => service.login(any(), accounts: any(named: 'accounts')),
).called(1);
});

Expand All @@ -306,9 +301,10 @@ void main() {
Future<List<AccountModel>>.value(
<AccountModel>[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,
));
Expand All @@ -327,8 +323,10 @@ void main() {
authCubit.tokenLogin(_token);

verify(
() => service.login(any(),
accounts: any(named: 'accounts'), tokenList: <String>[]),
() => service.login(
any(),
accounts: any(named: 'accounts'),
),
).called(1);
});

Expand Down

0 comments on commit 0a1e142

Please sign in to comment.