mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-04 18:50:42 -06:00
[client] Better error handling for login requests
This commit is contained in:
@@ -6,32 +6,40 @@ import 'package:http/http.dart';
|
||||
import 'package:offtheline/offtheline.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
import 'package:phylum/util/dialogs.dart';
|
||||
import 'package:phylum/util/logging.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:uri/uri.dart';
|
||||
|
||||
void performLogin(BuildContext context, Uri instanceUri, String email, String password) async {
|
||||
showProgressDialog(
|
||||
context,
|
||||
barrierDismissible: false,
|
||||
title: 'Logging in...',
|
||||
);
|
||||
|
||||
final builder = UriBuilder.fromUri(instanceUri);
|
||||
builder.path = '${builder.path}/api/v1/auth/password';
|
||||
final request = MultipartRequest('post', builder.build());
|
||||
request.fields['email'] = email;
|
||||
request.fields['password'] = password;
|
||||
|
||||
sendRequest(context, 'Logging In', request, (response) async {
|
||||
sendRequest(context, 'Logging In', request, (responseString) async {
|
||||
final accountManager = context.read<AccountManager<PhylumAccount>>();
|
||||
final account = await PhylumAccount.createFromLoginResponse(instanceUri, response.cast<String, dynamic>());
|
||||
accountManager.addAccount(account);
|
||||
final navigator = Navigator.of(context);
|
||||
showProgressDialog(context, barrierDismissible: false, title: 'Processing Login Response');
|
||||
try {
|
||||
final response = jsonDecode(responseString) as Map;
|
||||
final account = await PhylumAccount.createFromLoginResponse(instanceUri, response.cast<String, dynamic>());
|
||||
accountManager.addAccount(account);
|
||||
} catch (e) {
|
||||
navigator.pop();
|
||||
if (context.mounted) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
barrierDismissible: false,
|
||||
title: 'Error',
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void requestPasswordReset(BuildContext context, Uri instanceUrl, String email) async {
|
||||
showProgressDialog(context, barrierDismissible: false, title: 'Requesting Password Reset');
|
||||
|
||||
final builder = UriBuilder.fromUri(instanceUrl);
|
||||
builder.path = '${builder.path}/api/v1/auth/request-password-reset';
|
||||
final request = MultipartRequest('post', builder.build());
|
||||
@@ -47,32 +55,19 @@ void requestPasswordReset(BuildContext context, Uri instanceUrl, String email) a
|
||||
});
|
||||
}
|
||||
|
||||
void sendRequest(BuildContext context, String title, BaseRequest request, Function(Map) successCallback) async {
|
||||
void sendRequest(BuildContext context, String title, BaseRequest request, Function(String) successCallback) async {
|
||||
final navigator = Navigator.of(context);
|
||||
showProgressDialog(context, barrierDismissible: false, title: title);
|
||||
|
||||
late int responseStatusCode;
|
||||
late String responseString;
|
||||
try {
|
||||
final navigator = Navigator.of(context);
|
||||
final response = await Client().send(request);
|
||||
final responseString = await response.stream.bytesToString();
|
||||
final reponse = jsonDecode(responseString) as Map;
|
||||
|
||||
// Dismiss progress dialog
|
||||
navigator.pop();
|
||||
if (response.statusCode == 200) {
|
||||
successCallback(reponse);
|
||||
} else {
|
||||
if (context.mounted) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
barrierDismissible: false,
|
||||
title: 'Error',
|
||||
message: reponse['msg'],
|
||||
);
|
||||
}
|
||||
}
|
||||
responseStatusCode = response.statusCode;
|
||||
responseString = await response.stream.bytesToString();
|
||||
} on SocketException {
|
||||
navigator.pop();
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
showAlertDialog(
|
||||
context,
|
||||
barrierDismissible: false,
|
||||
@@ -80,9 +75,10 @@ void sendRequest(BuildContext context, String title, BaseRequest request, Functi
|
||||
message: 'Unable to reach ${request.url}',
|
||||
);
|
||||
}
|
||||
return;
|
||||
} catch (e) {
|
||||
navigator.pop();
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
showAlertDialog(
|
||||
context,
|
||||
barrierDismissible: false,
|
||||
@@ -90,6 +86,34 @@ void sendRequest(BuildContext context, String title, BaseRequest request, Functi
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
rethrow;
|
||||
logger.w('Request error to ${request.url.path}', error: e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Dismiss progress dialog
|
||||
navigator.pop();
|
||||
if (responseStatusCode == 200) {
|
||||
successCallback(responseString);
|
||||
} else {
|
||||
if (context.mounted) {
|
||||
try {
|
||||
final reponse = jsonDecode(responseString) as Map;
|
||||
showAlertDialog(
|
||||
context,
|
||||
barrierDismissible: false,
|
||||
title: 'Error',
|
||||
message: reponse['msg'],
|
||||
);
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
barrierDismissible: false,
|
||||
title: 'Error',
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user