I am working on an application in which When a user is new, he must fill a form before going to the home page after registration. I am using Firebase authentication with social logins and with email also.
The issue is that as soon as the auth state is changed in social logins module, the listener is notified and it goes to homePage instead of going to that form page (the listener is in the wrapper class that is listening to the state changes whether the user is signed in or not). But when I register with email directly it works fine.
Below is the StreamBuilder which is listening to the auth state changes
StreamBuilder<User>(
stream: Authentication().auth.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
User user = snapshot.data;
if (user != null) {
return Layout();
}
else {
return SignIn();
}
}
else {
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
},
)
By checking the console logs, I saw that the listener is notified and the state is changed to logged in and the homepage is called immediately, the homepage should only be called if the user is logged in && is an old user. If the user is new && logged in then the form page should be called. I am not sure how to handle this condition as the authSateChanges() is returning a Stream which does not give detail that the user is new or old.
Is there a way to pause the listener in AUthentication class when we get the credentials because there I get a chance to see if the user is new or not by the following line of code
UserCredential result = await auth.signInWithCredential(credential);
if(result.additionalUserInfo!.isNewUser){}
Using .isNewUser
is probably not a good method to know if the user is newly created if you strictly want the user to fill up the form before going to homepage. Because consider the situation where user uninstalls app right after login without filling up the form and then reinstalling again which will then make the user as old user and would thus not ask to fill up the form, when it actually should ask, so this is why this method is suitable.
I suggest you using Custom Claims in firebase authentication using Cloud Functions. With custom claims you can set key, value pairs to denote whether a user is fully registered or not. Once registered you can update the custom claims and pass on that to user with updated claims which can thus make user to land up on homepage everytime he signs in.
To update a custom claims from nodejs environment, you can use admin.auth().setCustomUserClaims(uid, {formFilled: true})
when you see the form data is updated.
On the client side update the idToken, to receive updated claims using var result = currentUser.getIdTokenResult()
and get claims using result.claims
and check if result.claims["formFilled"] == true
Note that once the custom claims are updated, the client would have to explicitly call the getIdTokenResult()
to force refresh tokens.