Skip to content

Commit

Permalink
sign in flow++
Browse files Browse the repository at this point in the history
  • Loading branch information
Desync-o-tron committed Sep 21, 2024
1 parent 46fc11b commit e2b7ed0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/community/profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -109,7 +111,7 @@ class EmailVerificationScreenWrapper extends StatelessWidget {
context.go(routeNames.Profile.text);
}),
AuthCancelledAction((context) {
context.pop();
context.go(routeNames.Profile.text);
}),
],
);
Expand Down
8 changes: 8 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -31,9 +32,16 @@ Future<void> 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 {
Expand Down
24 changes: 21 additions & 3 deletions lib/navigation/routes.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
},
);

0 comments on commit e2b7ed0

Please sign in to comment.