Skip to content

Commit

Permalink
handled login exceptions with AuthenticationException
Browse files Browse the repository at this point in the history
  • Loading branch information
peucastro committed Nov 9, 2024
1 parent df48c3c commit bc5ba1c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
2 changes: 0 additions & 2 deletions packages/uni_app/lib/session/flows/credentials/request.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:uni/controller/fetchers/faculties_fetcher.dart';
import 'package:uni/session/exception.dart';
import 'package:uni/session/flows/base/request.dart';
Expand Down Expand Up @@ -48,7 +47,6 @@ class CredentialsSessionRequest extends SessionRequest {
AuthenticationExceptionType.wrongCredentials,
);
} else {
unawaited(Sentry.captureException(failureReason));
throw const AuthenticationException(
'Failed to authenticate user',
);
Expand Down
88 changes: 87 additions & 1 deletion packages/uni_app/lib/view/login/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import 'package:logger/logger.dart';
import 'package:provider/provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:uni/app_links/uni_app_links.dart';
import 'package:uni/controller/networking/url_launcher.dart';
import 'package:uni/generated/l10n.dart';
import 'package:uni/model/providers/startup/session_provider.dart';
import 'package:uni/model/providers/state_providers.dart';
import 'package:uni/session/exception.dart';
import 'package:uni/session/flows/credentials/initiator.dart';
import 'package:uni/session/flows/federated/initiator.dart';
import 'package:uni/utils/constants.dart';
Expand Down Expand Up @@ -97,10 +99,52 @@ class LoginPageViewState extends State<LoginPageView>
_loggingIn = false;
});
}
} catch (err) {
} on AuthenticationException catch (err, st) {
setState(() {
_loggingIn = false;
});

switch (err.type) {
case AuthenticationExceptionType.expiredCredentials:
_updatePasswordDialog();
case AuthenticationExceptionType.internetError:
if (mounted) {
unawaited(
ToastMessage.warning(
context,
S.of(context).internet_status_exception,
),
);
}
case AuthenticationExceptionType.wrongCredentials:
if (mounted) {
unawaited(
ToastMessage.error(
context,
S.of(context).wrong_credentials_exception,
),
);
}
default:
Logger().e(err, stackTrace: st);
unawaited(Sentry.captureException(err, stackTrace: st));
if (mounted) {
unawaited(
ToastMessage.error(context, S.of(context).failed_login),
);
}
}
}
// Handles other unexpected exceptions
catch (err, st) {
setState(() {
_loggingIn = false;
});
Logger().e(err, stackTrace: st);
unawaited(Sentry.captureException(err, stackTrace: st));
if (mounted) {
unawaited(ToastMessage.error(context, S.of(context).failed_login));
}
}
}
}
Expand Down Expand Up @@ -335,4 +379,46 @@ class LoginPageViewState extends State<LoginPageView>
},
);
}

void _updatePasswordDialog() {
showDialog<void>(
context: context,
builder: (context) {
return AlertDialog(
title: Text(S.of(context).expired_password),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
S.of(context).pass_change_request,
textAlign: TextAlign.start,
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 20),
Align(
alignment: Alignment.centerLeft,
child: Text(
S.of(context).change_prompt,
textAlign: TextAlign.start,
),
),
],
),
actions: [
TextButton(
child: Text(S.of(context).cancel),
onPressed: () {
Navigator.of(context).pop();
},
),
ElevatedButton(
child: Text(S.of(context).change),
onPressed: () =>
launchUrlWithToast(context, 'https://self-id.up.pt/password'),
),
],
);
},
);
}
}

0 comments on commit bc5ba1c

Please sign in to comment.