From 3f01e0fb9d60e33a639176c3e3e65b9592996fc8 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Fri, 13 Sep 2024 13:15:59 +0200 Subject: [PATCH] Migrated the isSignedIn property in the authentication service to a ValueListenable Signed-off-by: Hans Kokx --- .../authentication/authentication_service.dart | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/src/services/authentication/authentication_service.dart b/lib/src/services/authentication/authentication_service.dart index 9c253e1..b06dc7d 100644 --- a/lib/src/services/authentication/authentication_service.dart +++ b/lib/src/services/authentication/authentication_service.dart @@ -1,6 +1,7 @@ import "dart:async"; import "package:arcane_framework/arcane_framework.dart"; +import "package:flutter/foundation.dart"; import "package:flutter/widgets.dart"; import "package:flutter_bloc/flutter_bloc.dart"; @@ -37,11 +38,11 @@ class ArcaneAuthenticationService extends ArcaneService { /// been registered. ArcaneAuthInterface get authInterface => _authInterface; - /// A shortcut to `status == AuthenticationStatus.authenticated`. - bool get isAuthenticated => status == AuthenticationStatus.authenticated; + /// A shortcut to `status != AuthenticationStatus.unauthenticated`. + bool get isAuthenticated => status != AuthenticationStatus.unauthenticated; - /// A shortcut to the `isSignedIn` getter of the registered `ArcaneAuthInterface`. - Future get isSignedIn => authInterface.isSignedIn; + /// Expose the ValueListenable so other widgets can listen to changes. + ValueListenable get isSignedIn => ValueNotifier(isAuthenticated); /// Returns a JWT access token if the registered `ArcaneAuthInterface` /// provides one. This token is often used in the headers of HTTP requests @@ -103,16 +104,16 @@ class ArcaneAuthenticationService extends ArcaneService { /// Logs the current user out. Upon successful logout, `status` will be set to /// `AuthenticationStatus.unauthenticated`. - Future logOut({required VoidCallback onLoggedOut}) async { + Future logOut({VoidCallback? onLoggedOut}) async { if (_mocked) return; - if (status == AuthenticationStatus.unauthenticated) return; + if (!isAuthenticated) return; final Result loggedOut = await authInterface.logout(); await loggedOut.fold( onSuccess: (_) async { setUnauthenticated(); - onLoggedOut(); + if (onLoggedOut != null) onLoggedOut(); }, onError: (e) { throw Exception(e);