Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/common/show_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
38 changes: 28 additions & 10 deletions lib/provider/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,32 @@ class AccountCredentials with ChangeNotifier {
throw Exception('No account found for setLastAccountUsed');
}

Future<bool> getinitializeAuth(String locationId, String password) async {
_authToken = AuthToken(locationId, password);
final success = await RsJsonApi.checkExistingAuthTokens(
locationId,
password,
_authToken!,
);
return success;
Future<bool> 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<bool> checkIsValidAuthToken() async {
Expand All @@ -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');
}
Expand All @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions lib/ui/signin_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class SignInScreenState extends State<SignInScreen> {
}
} 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 {
Expand All @@ -91,16 +95,17 @@ class SignInScreenState extends State<SignInScreen> {
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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ui/signup_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ class SignUpScreenState extends State<SignUpScreen> {
});
});
} 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',
Expand Down