diff --git a/lib/common/show_dialog.dart b/lib/common/show_dialog.dart index e88817d..37d1740 100644 --- a/lib/common/show_dialog.dart +++ b/lib/common/show_dialog.dart @@ -12,7 +12,6 @@ Future errorShowDialog(String title, String text, BuildContext context) { type: CoolAlertType.error, onConfirmBtnTap: (context) { if (Navigator.canPop(context)) Navigator.of(context).pop(); - if (Navigator.canPop(context)) Navigator.of(context).pop(); }, title: title, text: text, @@ -29,7 +28,6 @@ Future successShowDialog(String title, String text, BuildContext context) { type: CoolAlertType.success, onConfirmBtnTap: (context) { if (Navigator.canPop(context)) Navigator.of(context).pop(); - if (Navigator.canPop(context)) Navigator.of(context).pop(); }, title: title, text: text, @@ -42,7 +40,6 @@ Future warningShowDialog(String title, String text, BuildContext context) { type: CoolAlertType.warning, onConfirmBtnTap: (context) { if (Navigator.canPop(context)) Navigator.of(context).pop(); - if (Navigator.canPop(context)) Navigator.of(context).pop(); }, title: title, text: text, diff --git a/lib/provider/auth.dart b/lib/provider/auth.dart index 892d705..e60e06b 100644 --- a/lib/provider/auth.dart +++ b/lib/provider/auth.dart @@ -61,14 +61,32 @@ class AccountCredentials with ChangeNotifier { throw Exception('No account found for setLastAccountUsed'); } - Future getinitializeAuth(String locationId, String password) async { - _authToken = AuthToken(locationId, password); - final success = await RsJsonApi.checkExistingAuthTokens( - locationId, - password, - _authToken!, - ); - return success; + Future getinitializeAuth(Account account, String password) async { + // Retry logic as the core might take a moment to initialize the API for the unlocked account + for (int retry = 0; retry < 3; retry++) { + if (retry > 0) { + await Future.delayed(const Duration(seconds: 1)); + } + + // 1. Try locationId (SSL ID) - most robust for multiple locations + _authToken = AuthToken(account.locationId, password); + bool success = await RsJsonApi.isAuthTokenValid(_authToken!); + if (success) return true; + + // 2. Try pgpName (The username used in signup as apiUser) + _authToken = AuthToken(account.pgpName, password); + success = await RsJsonApi.isAuthTokenValid(_authToken!); + if (success) return true; + + // 3. Try locationName (The node name, sometimes used as fallback) + _authToken = AuthToken(account.locationName, password); + success = await RsJsonApi.isAuthTokenValid(_authToken!); + if (success) return true; + } + + // 4. Default back to locationId if all failed + _authToken = AuthToken(account.locationId, password); + return false; } Future checkIsValidAuthToken() async { @@ -81,7 +99,7 @@ class AccountCredentials with ChangeNotifier { // Login success 0, already logged in 1 if (resp == 0 || resp == 1) { final isAuthTokenValid = - await getinitializeAuth(currentAccount.locationName, password); + await getinitializeAuth(currentAccount, password); if (!isAuthTokenValid) { throw const HttpException('AUTHTOKEN FAILED'); } @@ -106,7 +124,7 @@ class AccountCredentials with ChangeNotifier { _accountsList.add(account.$2); logginAccount = account.$2; final isAuthTokenValid = - await getinitializeAuth(account.$2.locationName, password); + await getinitializeAuth(account.$2, password); if (!isAuthTokenValid) throw const HttpException('AUTHTOKEN FAILED'); notifyListeners(); diff --git a/lib/ui/signin_screen.dart b/lib/ui/signin_screen.dart index 73ac29b..3a10572 100644 --- a/lib/ui/signin_screen.dart +++ b/lib/ui/signin_screen.dart @@ -83,6 +83,10 @@ class SignInScreenState extends State { } } on HttpException catch (error) { if (!mounted) return; + + // Close the splash screen/loading screen first to avoid UI conflicts + if (Navigator.canPop(context)) Navigator.pop(context); + if (error.message.contains('WRONG PASSWORD')) { _handleWrongPassword(); } else { @@ -91,16 +95,17 @@ class SignInScreenState extends State { error.message, context, ); - if (Navigator.canPop(context)) Navigator.pop(context); } } catch (e) { if (!mounted) return; + + if (Navigator.canPop(context)) Navigator.pop(context); + await errorShowDialog( 'Retroshare Service Down', 'An error occurred: $e'.trim(), context, ); - if (Navigator.canPop(context)) Navigator.pop(context); } } diff --git a/lib/ui/signup_screen.dart b/lib/ui/signup_screen.dart index 0a71edc..c5e9874 100644 --- a/lib/ui/signup_screen.dart +++ b/lib/ui/signup_screen.dart @@ -103,9 +103,11 @@ class SignUpScreenState extends State { }); }); } on HttpException { + if (Navigator.canPop(context)) Navigator.pop(context); const errorMessage = 'Authentication failed'; await errorShowDialog(errorMessage, 'Something went wrong', context); } catch (e) { + if (Navigator.canPop(context)) Navigator.pop(context); debugPrint('Error creating account: $e'); await errorShowDialog( 'Retroshare Service Down',