Skip to content

Commit

Permalink
Merge pull request #1359 from ardriveapp/arconnect_new_permissions
Browse files Browse the repository at this point in the history
PE-4606: check permissions before connecting
  • Loading branch information
matibat authored Sep 14, 2023
2 parents a881ec7 + 02c8f12 commit 5d7d605
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/authentication/login/blocs/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
final ArDriveAuth _arDriveAuth;
final ArConnectService _arConnectService;

bool ignoreNextWaletSwitch = false;

@visibleForTesting
String? lastKnownWalletAddress;

Expand Down Expand Up @@ -216,9 +218,20 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
try {
emit(LoginLoading());

await _arConnectService.connect();
bool hasPermissions = await _arConnectService.checkPermissions();
if (!hasPermissions) {
try {
// If we have partial permissions, we're gonna disconnect before
/// re-connecting again.
ignoreNextWaletSwitch = true;
await _arConnectService.disconnect();
} catch (_) {}

await _arConnectService.connect();
}

if (!(await _arConnectService.checkPermissions())) {
hasPermissions = await _arConnectService.checkPermissions();
if (!hasPermissions) {
throw Exception('ArConnect permissions not granted');
}

Expand Down Expand Up @@ -300,6 +313,11 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
}

onArConnectWalletSwitch(() {
if (ignoreNextWaletSwitch) {
ignoreNextWaletSwitch = false;
return;
}

logger.i('ArConnect wallet switched');
// ignore: invalid_use_of_visible_for_testing_member
emit(const LoginFailure(WalletMismatchException()));
Expand Down
3 changes: 3 additions & 0 deletions lib/authentication/login/views/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:ardrive/services/config/config_service.dart';
import 'package:ardrive/utils/app_localizations_wrapper.dart';
import 'package:ardrive/utils/app_platform.dart';
import 'package:ardrive/utils/io_utils.dart';
import 'package:ardrive/utils/logger/logger.dart';
import 'package:ardrive/utils/open_url.dart';
import 'package:ardrive/utils/pre_cache_assets.dart';
import 'package:ardrive/utils/split_localizations.dart';
Expand Down Expand Up @@ -208,6 +209,8 @@ class _LoginPageScaffoldState extends State<LoginPageScaffold> {
if (state is LoginFailure) {
// TODO: Verify if the error is `NoConnectionException` and show an appropriate message after validating with UI/UX

logger.e('Login Failure', state.error);

if (state.error is WalletMismatchException) {
showAnimatedDialog(
context,
Expand Down

0 comments on commit 5d7d605

Please sign in to comment.