From c2cfb46d825fa448bfee768250b148f0864759e9 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Mon, 19 May 2025 17:00:01 +0200 Subject: [PATCH] Fix authentication interface and service methods for logout and login. --- .../authentication/authentication_interface.dart | 8 +++++++- .../authentication/authentication_service.dart | 15 ++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/src/services/authentication/authentication_interface.dart b/lib/src/services/authentication/authentication_interface.dart index 06f0205..9cff69a 100644 --- a/lib/src/services/authentication/authentication_interface.dart +++ b/lib/src/services/authentication/authentication_interface.dart @@ -36,7 +36,11 @@ abstract class ArcaneAuthInterface { /// This method terminates the current session and removes any stored tokens. /// Returns a `Result` that either contains a `void` on success or an error /// message. - Future> logout(); + /// Upon a successful logout, the `onLoggedOut` method will be called if it + /// has been provided. + Future> logout({ + Future Function()? onLoggedOut, + }); /// Logs the user in using an optional, generic `T` type of input. /// This login method is a generic method that can be used to login with any @@ -44,6 +48,8 @@ abstract class ArcaneAuthInterface { /// and password. Any type of input can be passed in, and it will be handled /// by the implementation of the method wihin the specific authentication /// service. + /// Upon a successful login, the `onLoggedIn` method will be called if it + /// has been provided. /// /// Example: /// ```dart diff --git a/lib/src/services/authentication/authentication_service.dart b/lib/src/services/authentication/authentication_service.dart index 6ad8045..c2bdda3 100644 --- a/lib/src/services/authentication/authentication_service.dart +++ b/lib/src/services/authentication/authentication_service.dart @@ -50,13 +50,13 @@ class ArcaneAuthenticationService extends ArcaneService { /// Returns a JWT access token if the registered `ArcaneAuthInterface` /// provides one. This token is often used in the headers of HTTP requests /// to the backend API. - Future get accessToken => - authInterface?.accessToken ?? Future.value(""); + Future get accessToken async => + await authInterface?.accessToken ?? Future.value(""); /// Returns a JWT refresh token if the registered `ArcaneAuthInterface` /// provides one. - Future get refreshToken => - authInterface?.refreshToken ?? Future.value(""); + Future get refreshToken async => + await authInterface?.refreshToken ?? Future.value(""); AuthenticationStatus? _previousModeWhenSettingDebug; @@ -169,11 +169,12 @@ class ArcaneAuthenticationService extends ArcaneService { if (!isAuthenticated) Result.error("User is not authenticated."); - final Result loggedOut = await authInterface!.logout(); + final Result loggedOut = await authInterface!.logout( + onLoggedOut: onLoggedOut, + ); if (loggedOut.isSuccess) { setUnauthenticated(); - if (onLoggedOut != null) await onLoggedOut(); } _previousModeWhenSettingDebug = null; @@ -192,11 +193,11 @@ class ArcaneAuthenticationService extends ArcaneService { final Result result = await authInterface!.login( input: input, + onLoggedIn: onLoggedIn, ); if (result.isSuccess) { setAuthenticated(); - if (onLoggedIn != null) await onLoggedIn(); } return result;