6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit 94435976

AuthorNKL<nikolaj.king@gmail.com>
Date2024-10-08 12:53:09 +0200
before save

Changed files

.../authentication/authentication_repository.dart  |  8 ++-----
 .../authentication/bloc/authentication_bloc.dart   |  8 +------
 comwell_key_app/lib/comwell_app.dart               |  2 +-
 .../lib/login/components/azure_b2c_widget.dart     | 13 +++++++----
 comwell_key_app/lib/login/cubit/login_cubit.dart   | 25 ++++++++++++++++++++++
 comwell_key_app/lib/login/cubit/login_state.dart   | 19 ++++++++++++++++
 comwell_key_app/lib/routing/app_router.dart        |  4 ++--
 comwell_key_app/test/widget_test.dart              |  2 +-
 8 files changed, 60 insertions(+), 21 deletions(-)

Diff

diff --git a/comwell_key_app/lib/authentication/authentication_repository.dart b/comwell_key_app/lib/authentication/authentication_repository.dart
index 3ea26b08..9fc7fa9b 100644
--- a/comwell_key_app/lib/authentication/authentication_repository.dart
+++ b/comwell_key_app/lib/authentication/authentication_repository.dart
@@ -3,8 +3,6 @@ import 'dart:async';
import 'package:comwell_key_app/authentication/enum/authentication_status.dart';
class AuthenticationRepository {
- // Constructor
- AuthenticationRepository();
final _controller = StreamController<AuthenticationStatus>();
@@ -12,12 +10,10 @@ class AuthenticationRepository {
await Future<void>.delayed(const Duration(seconds: 1));
yield AuthenticationStatus.unauthenticated;
yield* _controller.stream;
+ print("Stream: ${_controller.stream}");
}
- Future<void> logIn({
- required String username,
- required String password,
- }) async {
+ Future<void> logIn() async {
await Future.delayed(
const Duration(milliseconds: 300),
() => _controller.add(AuthenticationStatus.authenticated),
diff --git a/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart b/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart
index e7c87c0e..704c9523 100644
--- a/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart
+++ b/comwell_key_app/lib/authentication/bloc/authentication_bloc.dart
@@ -11,9 +11,6 @@ class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState>
required AuthenticationRepository authenticationRepository,
}) : _authenticationRepository = authenticationRepository, super(const AuthenticationState.unknown()) {
-
-
-
on<AuthenticationSubscriptionRequested>(_onSubscriptionRequested);
on<AuthenticationLogoutPressed>(_onLogoutPressed);
@@ -42,10 +39,7 @@ class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState>
case AuthenticationStatus.authenticated:
//final user = await _tryGetUser(); TODO: this should check on tokens saved in secure storage
return emit(
- /* user != null
- ? AuthenticationState.authenticated(user)
- : const AuthenticationState.unauthenticated(), */
- AuthenticationState._()
+ const AuthenticationState.authenticated()
);
case AuthenticationStatus.unknown:
return emit(const AuthenticationState.unknown());
diff --git a/comwell_key_app/lib/comwell_app.dart b/comwell_key_app/lib/comwell_app.dart
index fa6c47fb..00a46a1a 100644
--- a/comwell_key_app/lib/comwell_app.dart
+++ b/comwell_key_app/lib/comwell_app.dart
@@ -23,7 +23,7 @@ class ComwellApp extends StatelessWidget {
Widget build(BuildContext context) {
return BlocProvider<AuthenticationBloc>(
- create: (context) => authBloc,
+ create: (context) => authBloc..add(AuthenticationSubscriptionRequested()),
child: MultiBlocProvider(
providers: blocProviderList,
child:
diff --git a/comwell_key_app/lib/login/components/azure_b2c_widget.dart b/comwell_key_app/lib/login/components/azure_b2c_widget.dart
index 5351993d..8c3cbf64 100644
--- a/comwell_key_app/lib/login/components/azure_b2c_widget.dart
+++ b/comwell_key_app/lib/login/components/azure_b2c_widget.dart
@@ -1,15 +1,16 @@
import 'package:aad_b2c_webview/aad_b2c_webview.dart';
+import 'package:comwell_key_app/authentication/authentication_repository.dart';
import 'package:comwell_key_app/login/auth.dart';
-import 'package:comwell_key_app/routing/app_routes.dart';
+import 'package:comwell_key_app/login/cubit/login_cubit.dart';
import 'package:comwell_key_app/utils/secure_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
-import 'package:go_router/go_router.dart';
class AzureB2CWidget extends StatelessWidget {
final Auth authEnum;
final SecureStorage secureStorage = SecureStorage();
+ final LoginCubit loginCubit = LoginCubit(authenticationRepository: AuthenticationRepository());
AzureB2CWidget({
required this.authEnum,
super.key,
@@ -25,17 +26,21 @@ class AzureB2CWidget extends StatelessWidget {
clientId: dotenv.env['AAD_B2C_CLIENT_ID']!,
redirectUrl: dotenv.env['AAD_B2C_REDIRECT_URL']!,
scopes: dotenv.env['AAD_B2C_SCOPES']!.split(','),
- onAnyTokenRetrieved: (Token anyToken) {},
+ onAnyTokenRetrieved: (Token anyToken) {
+ print("token" + anyToken.value);
+ loginCubit.login();
+ },
onIDToken: (Token token) {
secureStorage.write('id_token', token.value);
},
- onRedirect: (context) => context.goNamed(AppRoutes.home.name),
onAccessToken: (Token token) {
secureStorage.write('access_token', token.value);
+ loginCubit.login();
},
onRefreshToken: (Token token) {
secureStorage.write('refresh_token', token.value);
},
+ onErrorOrCancel: (context) => loginCubit.loginErrorFromAzureB2C(),
optionalParameters: const [],
webViewBackgroundColor: sandColor[40]!,
);
diff --git a/comwell_key_app/lib/login/cubit/login_cubit.dart b/comwell_key_app/lib/login/cubit/login_cubit.dart
new file mode 100644
index 00000000..feeffcdc
--- /dev/null
+++ b/comwell_key_app/lib/login/cubit/login_cubit.dart
@@ -0,0 +1,25 @@
+import 'package:bloc/bloc.dart';
+import 'package:comwell_key_app/authentication/authentication_repository.dart';
+import 'package:equatable/equatable.dart';
+
+part 'login_state.dart';
+
+class LoginCubit extends Cubit<LoginState> {
+ final AuthenticationRepository authenticationRepository;
+ LoginCubit({required this.authenticationRepository}) : super(LoginInitial());
+
+ void login() {
+
+ try {
+ // Call the login method from the authenticationRepository
+ authenticationRepository.logIn();
+ emit(LoginSuccessful());
+ } catch (e) {
+ emit(LoginError(e.toString()));
+ }
+ }
+ void loginErrorFromAzureB2C() {
+ emit(const LoginError('Error from az'));
+ authenticationRepository.logOut();
+ }
+}
diff --git a/comwell_key_app/lib/login/cubit/login_state.dart b/comwell_key_app/lib/login/cubit/login_state.dart
new file mode 100644
index 00000000..4be19474
--- /dev/null
+++ b/comwell_key_app/lib/login/cubit/login_state.dart
@@ -0,0 +1,19 @@
+part of 'login_cubit.dart';
+
+sealed class LoginState extends Equatable {
+ const LoginState();
+
+ @override
+ List<Object> get props => [];
+}
+
+final class LoginInitial extends LoginState {}
+final class LoginSuccessful extends LoginState {}
+final class LoginError extends LoginState {
+ final String error;
+
+ const LoginError(this.error);
+
+ @override
+ List<Object> get props => [error];
+}
diff --git a/comwell_key_app/lib/routing/app_router.dart b/comwell_key_app/lib/routing/app_router.dart
index 7fc45a99..29717120 100644
--- a/comwell_key_app/lib/routing/app_router.dart
+++ b/comwell_key_app/lib/routing/app_router.dart
@@ -2,7 +2,6 @@ import 'package:comwell_key_app/authentication/bloc/authentication_bloc.dart';
import 'package:comwell_key_app/authentication/enum/authentication_status.dart';
import 'package:comwell_key_app/home/home_page.dart';
import 'package:comwell_key_app/key/key_page.dart';
-import 'package:comwell_key_app/login/auth.dart';
import 'package:comwell_key_app/login/login_page.dart';
import 'package:comwell_key_app/profile/profile_page.dart';
import 'package:comwell_key_app/redeem_debug/redeem_page.dart';
@@ -20,6 +19,7 @@ GoRouter goRouter(AuthenticationBloc authBloc) {
initialLocation: '/login',
navigatorKey: _rootNavigatorKey,
debugLogDiagnostics: true,
+ refreshListenable: StreamToListenable([authBloc.stream]),
redirect: (context, state) {
final isAuthenticated = authBloc.state.status == AuthenticationStatus.authenticated;
final isUnAuthenticated = authBloc.state.status == AuthenticationStatus.unauthenticated;
@@ -34,7 +34,7 @@ GoRouter goRouter(AuthenticationBloc authBloc) {
}
return null;
},
- refreshListenable: StreamToListenable([authBloc.stream]),
+
routes: <RouteBase>[
GoRoute(
path: "/",
diff --git a/comwell_key_app/test/widget_test.dart b/comwell_key_app/test/widget_test.dart
index 00acf59d..56823869 100644
--- a/comwell_key_app/test/widget_test.dart
+++ b/comwell_key_app/test/widget_test.dart
@@ -14,7 +14,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Verify Platform version', (WidgetTester tester) async {
// Build our app and trigger a frame.
- await tester.pumpWidget(const ComwellApp());
+ await tester.pumpWidget( ComwellApp());
// Verify that platform version is retrieved.
expect(