Once my user login is successful and I have a Firebase User I want to check if there is already a Profile created on my server by dispatching an action which makes an async call to get a Profile.
If no Profile is returned I want to navigate the app to Profile creation page otherwise, load the Profile page.
I'm thinking dispatching the second action right in the login middleware, but it seems weird to be mixing profile and login code in the login middleware.
Is there a better way or more of a standard way to do what I'm trying to do?
I haven't tried anything, but I was thinking of dispatching the second action in the login middleware and navigating to the desired page from there.
ThunkAction<AppState> logIn = (Store<AppState> store) async {
store.dispatch(UserLoginAction());
try {
final GoogleSignIn _googleSignIn = new GoogleSignIn();
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseUser _fb = await _auth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
User user = new User(id: _fb.uid, name: _fb.displayName, email: _fb.email, photoUrl: _fb.photoUrl);
store.dispatch(UserLoginSuccessAction(user: user));
store.dispatch(GetProfileAction());
Profile profile = await ProfileService.get().getProfile(_fb.uid);
store.dispatch(GetProfileSuccessAction(profile: profile));
if (profile) {
// Navigate to Profile Page
} else {
// Navigate to Profile Creation Page
}
} catch(error) {
store.dispatch(UserLoginFailAction(error: error));
}
};
Edit: I just thought of a way which seems better than my previous suggestion.
What if after successful login I just navigate them to the Profile page. On the init of the Profile page is where I would dispatch the action to get the Profile. The Profile page would render differently based on whether the Profile was initialized in the AppState after the middleware and reducer have completed.
Does this seem okay to do or is there a different and better way?
Dispatch
GetProfileAction()
on ProfilePage'sonInit
.
Your second suggested solution is more appropriate for these reasons: