I am using the go_router and Firebase Auth for my web application but can not properly set it up. I tried this:
class AuthService {
static final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
static User? get currentUser => FirebaseAuth.instance.currentUser;
static bool isLoggedIn() {
return _firebaseAuth.currentUser != null;
}
...
With my router
:
redirect: (context, state) {
final String destination = state.location;
final bool isOnStartView = destination == '/start';
final bool isOnEmailFlow = state.subloc.contains('/email');
if (!isOnStartView && !isOnEmailFlow && !AuthService.isLoggedIn()) {
return '/start';
}
return null;
},
However this is not working properly as isLoggedIn
is always return false
even if there a is a user logged in. I searched for this topic and found that I probably use some sort of Stream
or Notifier
with onAuthStateChange
but I didn’t find anything on how to implement a proper FirebaseAuthentication Flow in combination with GoRouter.
How would I do that?
Let me know if you need any more info.
2
Answers
You can do this
You can use riverpod or provider to listen changes
here is code sample