Skip to content

Commit 791d71d

Browse files
committed
refactor auth service and auth cubit logic
1 parent 3aecefe commit 791d71d

File tree

9 files changed

+63
-172
lines changed

9 files changed

+63
-172
lines changed

app/lib/presentation/ui/pages/home/home_view.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import 'package:app/main/init.dart';
2-
import 'package:domain/services/auth_service.dart';
2+
import 'package:domain/bloc/auth/auth_cubit.dart';
33
import 'package:flutter/material.dart';
44
import 'package:app/presentation/ui/custom/app_theme_switch.dart';
55

66
class HomeView extends StatelessWidget {
7-
AuthService get _authService => getIt();
7+
/// Given this is a global cubit, we can access it directly from getIt
8+
/// other wise use context.read<AuthCubit>() to read the Cubit under that context
9+
AuthCubit get _authCubit => getIt();
810

911
const HomeView({super.key});
1012

@@ -14,7 +16,7 @@ class HomeView extends StatelessWidget {
1416
appBar: AppBar(
1517
actions: [
1618
IconButton(
17-
onPressed: () => _authService.onLogout(),
19+
onPressed: () => _authCubit.logOut(),
1820
icon: const Icon(Icons.logout),
1921
),
2022
const AppThemeSwitch(),

app/lib/presentation/ui/pages/login/login_page.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ import 'package:app/presentation/ui/custom/app_theme_switch.dart';
55
import 'package:app/presentation/ui/custom/loading_screen.dart';
66
import 'package:common/core/resource.dart';
77
import 'package:domain/bloc/auth/auth_cubit.dart';
8-
import 'package:domain/services/auth_service.dart';
98
import 'package:flutter/foundation.dart';
109
import 'package:flutter/material.dart';
1110
import 'package:flutter_bloc/flutter_bloc.dart';
1211

1312
import '../../custom/environment_selector.dart';
1413

1514
class LoginPage extends StatelessWidget {
16-
AuthService get _authService => getIt();
17-
1815
const LoginPage({super.key});
1916

17+
AuthCubit get _authCubit => getIt();
18+
2019
@override
2120
Widget build(BuildContext context) {
2221
return Stack(
@@ -29,14 +28,15 @@ class LoginPage extends StatelessWidget {
2928
child: Column(
3029
mainAxisAlignment: MainAxisAlignment.center,
3130
children: [
31+
const _Loading(),
3232
const AppThemeSwitch(),
3333
SizedBox(height: spacing.m),
3434
SizedBox(
3535
width: double.maxFinite,
3636
child: ElevatedButton(
3737
child: const Text('Login'),
3838
onPressed: () {
39-
_authService.logInWithCredentials(
39+
_authCubit.login(
4040
'Rootstrap',
4141
'12345678',
4242
);
@@ -51,7 +51,6 @@ class LoginPage extends StatelessWidget {
5151
),
5252
),
5353
),
54-
const _Loading(),
5554
],
5655
);
5756
}

app/lib/presentation/ui/pages/splash/splash_page.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:app/main/init.dart';
2-
import 'package:domain/services/auth_service.dart';
2+
import 'package:domain/bloc/auth/auth_cubit.dart';
33
import 'package:flutter/material.dart';
44

55
class SplashPage extends StatefulWidget {
@@ -10,12 +10,19 @@ class SplashPage extends StatefulWidget {
1010
}
1111

1212
class _SplashPageState extends State<SplashPage> {
13-
AuthService get _authService => getIt();
13+
/// Given this is a global cubit, we can access it directly from getIt
14+
/// other wise use context.read<AuthCubit>() to read the Cubit under that context
15+
AuthCubit get _authCubit => getIt();
1416

1517
@override
1618
void initState() {
1719
super.initState();
18-
_authService.onValidate();
20+
21+
/// Add post frame callback to avoid calling bloc methods during build
22+
WidgetsBinding.instance.addPostFrameCallback((_) async {
23+
await Future.delayed(const Duration(seconds: 1));
24+
_authCubit.onValidate();
25+
});
1926
}
2027

2128
@override

app/melos.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ packages:
55
- modules/*
66

77
scripts:
8+
run:web:
9+
exec: cd app && flutter run -t lib/main.dart --dart-define-from-file=env/.dev -d chrome
10+
description: Run the app in development mode for web.
811
lint:all:
912
run: melos run analyze && melos run format
1013
description: Run all static analysis checks.

modules/domain/lib/bloc/auth/auth_cubit.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
import 'package:common/core/resource.dart';
2+
import 'package:common/core/result_type.dart';
23
import 'package:domain/bloc/base_cubit.dart';
34
import 'package:domain/bloc/auth/auth_state.dart';
5+
import 'package:domain/services/auth_service.dart';
46

57
class AuthCubit extends BaseCubit<AuthState> {
6-
AuthCubit() : super(RSuccess(data: AuthStateUnknown()));
8+
final AuthService _authService;
9+
AuthCubit(this._authService) : super(RSuccess(data: AuthStateUnknown()));
10+
11+
Future login(String username, String password) async {
12+
isLoading();
13+
final authResult =
14+
await _authService.logInWithCredentials(username, password);
15+
16+
authResult.map(
17+
success: (_) => isLogin(),
18+
error: (failure) {
19+
isError(failure);
20+
return failure;
21+
},
22+
);
23+
}
24+
25+
Future<void> onValidate() async {
26+
if (_authService.isLoggedIn()) {
27+
isLogin();
28+
} else {
29+
isLogOut();
30+
}
31+
}
32+
33+
Future<void> logOut() async {
34+
await _authService.onLogout();
35+
isLogOut();
36+
}
737

838
void isLogin() => isSuccess(AuthStateAuthenticated());
939

modules/domain/lib/init.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import 'package:get_it/get_it.dart';
55

66
class DomainInit {
77
static Future<void> initialize(GetIt getIt) async {
8-
//Cubits
8+
//Services (with dependencies to repositories)
9+
getIt.registerLazySingleton(() => AuthService(getIt()));
10+
11+
//Global Cubits
912
getIt.registerSingleton(AppCubit(getIt()));
10-
getIt.registerSingleton(AuthCubit());
11-
12-
//Services
13-
getIt.registerLazySingleton(() => AuthService(getIt(), getIt()));
13+
getIt.registerSingleton(AuthCubit(getIt()));
1414
}
1515
}
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
import 'package:common/core/result_type.dart';
2-
import 'package:domain/bloc/auth/auth_cubit.dart';
32
import 'package:domain/repositories/auth_repository.dart';
43

54
class AuthService {
65
final AuthRepository _authRepository;
7-
final AuthCubit _sessionCubit;
86

9-
AuthService(this._authRepository, this._sessionCubit);
7+
AuthService(this._authRepository);
108

11-
Future<void> logInWithCredentials(String username, String password) async {
12-
_sessionCubit.isLoading();
13-
final result = await _authRepository.login(username, password);
14-
switch (result) {
15-
case TSuccess<void> _:
16-
_sessionCubit.isLogin();
17-
case TError<void> _:
18-
_sessionCubit.isError(result.error);
19-
}
20-
}
9+
Future<ResultType<void>> logInWithCredentials(
10+
String username, String password) =>
11+
_authRepository.login(username, password);
2112

22-
void onValidate() {
23-
if (_authRepository.isLoggedIn()) {
24-
_sessionCubit.isLogin();
25-
} else {
26-
_sessionCubit.isLogOut();
27-
}
28-
}
13+
bool isLoggedIn() => _authRepository.isLoggedIn();
2914

3015
Future<void> onLogout() async {
3116
await _authRepository.logout();
32-
_sessionCubit.isLogOut();
3317
}
3418
}

modules/domain/test/auth_cubit_test.dart

Lines changed: 0 additions & 48 deletions
This file was deleted.

modules/domain/test/auth_service_test.dart

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)