I am trying to figure out how to make use of Firebase's onAuthStateChanges()
stream to use as a Listenable in the refreshListenable
parameter from the go_router package to redirect whenever the authState changes.
In additon I am using flutter_riverpod for State Mangement.
My code looks like this so far:
I created a simple AuthService class (shrinked down to the most important parts):
abstract class BaseAuthService {
Stream<bool> get isLoggedIn;
Future<bool> signInWithEmailAndPassword({ required String email, required String password });
}
class AuthService implements BaseAuthService {
final Reader _read;
const AuthService(this._read);
FirebaseAuth get auth => _read(firebaseAuthProvider);
@override
Stream<bool> get isLoggedIn => auth.authStateChanges().map((User? user) => user != null);
@override
Future<bool> signInWithEmailAndPassword({ required String email, required String password }) async {
try {
await auth.signInWithEmailAndPassword(email: email, password: password);
return true;
} on FirebaseAuthException catch (e) {
...
} catch (e) {
...
}
return false;
}
Next I created these providers:
final firebaseAuthProvider = Provider.autoDispose<FirebaseAuth>((ref) => FirebaseAuth.instance);
final authServiceProvider = Provider.autoDispose<AuthService>((ref) => AuthService(ref.read));
As mentioned before, I would like to somehow listen to these authChanges and pass them to the router:
final router = GoRouter(
refreshListenable: ???
redirect: (GoRouterState state) {
bool isLoggedIn = ???
if (!isLoggedIn && !onAuthRoute) redirect to /signin;
}
)
ChangeNotifier
and callnotifyListeners
when yourStream.authState
changes - useStream.listen
to listen for any new events from that stream – CircumlocutionStream
dont you? if so, you can listen that stream – Circumlocution