Line data Source code
1 : import 'package:redux/redux.dart';
2 :
3 : import '../../../actions.dart';
4 : import '../../navigation/actions/pop_all_pages_action.dart';
5 : import '../../redux/extensions/store_extensions.dart';
6 : import '../../types/red_fire_state.dart';
7 : import '../../utils/red_fire_locator.dart';
8 : import '../actions/get_id_token_action.dart';
9 :
10 : class ObserveAuthStateMiddleware<T extends RedFireState>
11 : extends TypedMiddleware<T, ObserveAuthStateAction> {
12 2 : ObserveAuthStateMiddleware()
13 4 : : super((store, action, next) async {
14 2 : next(action);
15 :
16 : try {
17 2 : final authService = RedFireLocator.getAuthService();
18 :
19 : // Listen to the stream that emits actions on any auth change
20 : // and call dispatch on the action.
21 6 : authService.streamOfSetAuthUserData.listen((setAuthUserDataAction) {
22 : try {
23 2 : store.dispatch(setAuthUserDataAction);
24 :
25 : // If signing in, dispatch an action to get an id token.
26 : // The logic is here in order to cover both signing in explicitly
27 : // and when returning to the app already signed in.
28 2 : if (setAuthUserDataAction.authUserData != null) {
29 2 : store.dispatch(const GetIdTokenAction());
30 2 : store.dispatch(const PopAllPagesAction());
31 6 : RedFireLocator.getOnSignInActions.forEach(store.dispatch);
32 : }
33 : } catch (error, trace) {
34 1 : store.dispatchProblem(error, trace);
35 : }
36 2 : }, onError: store.dispatchProblem);
37 : } catch (error, trace) {
38 1 : store.dispatchProblem(error, trace);
39 : }
40 : });
41 : }
|