From e2b7ed02f12ef53c65e7b00cd6cec2a8f7732a44 Mon Sep 17 00:00:00 2001 From: Luke Knoble <35696371+Desync-o-tron@users.noreply.github.com> Date: Sat, 21 Sep 2024 13:11:49 -0400 Subject: [PATCH] sign in flow++ --- lib/community/profile_page.dart | 4 +++- lib/main.dart | 8 ++++++++ lib/navigation/routes.dart | 24 +++++++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/community/profile_page.dart b/lib/community/profile_page.dart index 3fd0520..106ceee 100644 --- a/lib/community/profile_page.dart +++ b/lib/community/profile_page.dart @@ -76,6 +76,8 @@ class SignInScreenWrapper extends StatelessWidget { Widget build(BuildContext context) { return SignInScreen( providers: providers, + //I double check auth in routes.dart now, but...synergy. + // I'm too dumb to get it to work only there. that's fine I think actions: [ AuthStateChangeAction((context, state) { final user = switch (state) { @@ -109,7 +111,7 @@ class EmailVerificationScreenWrapper extends StatelessWidget { context.go(routeNames.Profile.text); }), AuthCancelledAction((context) { - context.pop(); + context.go(routeNames.Profile.text); }), ], ); diff --git a/lib/main.dart b/lib/main.dart index 0ed5b22..83abc35 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -31,9 +32,16 @@ Future main() async { ); HydratedBloc.storage = hydratedStorage; + //https://stackoverflow.com/a/77448906/3894291 + FirebaseAuth.instance.authStateChanges().listen((User? user) { + routerConfig.refresh(); + }); + runApp(const MyApp()); } +//TODO replace the banner on the bottom when not signed in to make the place more welcoming! + Timer? trainingDurationTimer; class MyApp extends StatelessWidget { diff --git a/lib/navigation/routes.dart b/lib/navigation/routes.dart index ec7b3d2..48ab332 100644 --- a/lib/navigation/routes.dart +++ b/lib/navigation/routes.dart @@ -1,8 +1,8 @@ // ignore_for_file: constant_identifier_names, camel_case_types +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:firebase_ui_auth/firebase_ui_auth.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter/material.dart'; -// import 'package:firebase_auth/firebase_auth.dart' hide EmailAuthProvider, AuthProvider; -// import 'package:firebase_ui_auth/firebase_ui_auth.dart'; -// import 'package:firebase_ui_oauth_google/firebase_ui_oauth_google.dart'; import 'package:flutter/widgets.dart'; import 'package:go_router/go_router.dart'; import 'package:open_fitness_tracker/community/community_page.dart'; @@ -94,4 +94,22 @@ final GoRouter routerConfig = GoRouter( ], ), ], + //todo is this slow? + // redirect to the login page if the user is not logged in + redirect: (BuildContext context, GoRouterState state) async { + final bool loggedIn = FirebaseAuth.instance.currentUser != null; + if (!loggedIn) { + return routeNames.SignIn.text; + } else if (!FirebaseAuth.instance.currentUser!.emailVerified) { + if (state.matchedLocation == routeNames.SignIn.text || + state.matchedLocation == routeNames.Profile.text || + state.matchedLocation == routeNames.VerifyEmail.text) { + return null; + } else { + return routeNames.Profile.text; + } + } + // otherwise no need to redirect at all + return null; + }, );