Skip to content

Commit

Permalink
add persistent user session
Browse files Browse the repository at this point in the history
  • Loading branch information
itMatos committed Sep 2, 2024
1 parent 86c22cf commit 40cb4c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
23 changes: 16 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:zenith_monitor/modules/map/screen/map_screen.dart';
import 'package:zenith_monitor/modules/signup/screen/sign_up_screen.dart';
import 'package:zenith_monitor/utils/ui/animations/zenith_progress_indicator.dart';
import 'package:zenith_monitor/modules/login/screen/login_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -42,17 +43,22 @@ class Application extends StatelessWidget {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
UsbManager usbManager = UsbManager();
FirestoreServices fireServices = FirestoreServices();
// UsbManager usbManager = UsbManager();
// FirestoreServices fireServices = FirestoreServices();
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => LoginBloc(auth: GoogleAuth())),
// BlocProvider(
// create: (context) =>
// DataBloc(usbManager: usbManager, fireServices: fireServices)),
// BlocProvider(
// create: (context) => MapDataBloc(
// usbManager: usbManager, fireServices: fireServices)),
BlocProvider(
create: (context) =>
DataBloc(usbManager: usbManager, fireServices: fireServices)),
BlocProvider(
create: (context) => MapDataBloc(
usbManager: usbManager, fireServices: fireServices)),
TerminalBloc(dataBloc: BlocProvider.of<DataBloc>(context))),

BlocProvider(create: (context) => LoginBloc(auth: GoogleAuth())),
BlocProvider(
create: (context) =>
TerminalBloc(dataBloc: BlocProvider.of<DataBloc>(context))),
Expand All @@ -65,6 +71,8 @@ class Application extends StatelessWidget {
}

if (snapshot.connectionState == ConnectionState.done) {
final User? currentUser = FirebaseAuth.instance.currentUser;

return MaterialApp(
showPerformanceOverlay: false, // shows fps
debugShowCheckedModeBanner: false,
Expand All @@ -74,7 +82,8 @@ class Application extends StatelessWidget {
backgroundColor: Colors.black.withOpacity(0)),
primaryColor: Colors.black,
),
initialRoute: '/login',
initialRoute: currentUser != null ? '/home' : '/login',
// initialRoute: '/login',
routes: {
'/login': (context) => const LoginScreen(),
'/signup': (context) => const SignUpScreen(),
Expand Down
13 changes: 7 additions & 6 deletions lib/modules/login/bloc/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {

Future<void> _onSignOut(SignOutEvent event, Emitter<LoginState> emit) async {
await _auth.signOut();
Navigator.pushAndRemoveUntil(
// ignore: use_build_context_synchronously
event.context,
MaterialPageRoute(builder: (BuildContext context) => const LoginScreen()),
ModalRoute.withName('/login'),
);
emit(SignOutSuccess());
// Navigator.pushAndRemoveUntil(
// // ignore: use_build_context_synchronously
// event.context,
// MaterialPageRoute(builder: (BuildContext context) => const LoginScreen()),
// ModalRoute.withName('/login'),
// );
}
}
4 changes: 4 additions & 0 deletions lib/modules/login/bloc/login_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ class LoadingState extends LoginState {}
class LoginSuccess extends LoginState {
LoginSuccess(LocalUser newUser) : super(user: newUser);
}

class SignOutSuccess extends LoginState {}

class CheckSessionEvent extends LoginEvent {}
3 changes: 3 additions & 0 deletions lib/widgets/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class LoginWidgetState extends State<LoginWidget> {
if (state is LoginSuccess) {
Navigator.popAndPushNamed(context, '/home');
}
if (state is SignOutSuccess) {
Navigator.popAndPushNamed(context, '/login');
}
},
builder: (context, state) {
if (state is LoadingState) {
Expand Down

0 comments on commit 40cb4c9

Please sign in to comment.