According to the bottom of this page, AdditionalUserInfo provides a method called isNewUser()
to check for example if a social login (Facebook, Google etc.) is a sign in or a sign up. An example is given in this answer. The problem with Flutter is that I cannot find any class called AdditionalUserInfo
or a function called getAdditionalUserInfo()
. So does anyone know how to check if a user registered for the first time, preferably with Facebook & Firebase Authentication.
I am using firebase_auth and flutter_facebook_login.
Show activity on this post. try { _firbaseAuth. createUserWithEmailAndPassword( email: '[email protected]', password: 'password' ); } catch(signUpError) { if(signUpError is PlatformException) { if(signUpError. code == 'ERROR_EMAIL_ALREADY_IN_USE') { /// `[email protected]` has alread been registered. } } }
AdditionalUserInfo is a method of the AuthResult
class. If you want to check if the user is logging in for the first time then you can use the value of additionalUserInfo.isNewUser
. The following code logs in a user using Google Auth.
Future<FirebaseUser> _signIn() async {
try {
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAuthentication gSA = await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: gSA.accessToken,
idToken: gSA.idToken,
);
AuthResult authResult = await _auth.signInWithCredential(credential);
if (authResult.additionalUserInfo.isNewUser) {
//User logging in for the first time
// Redirect user to tutorial
}
else {
//User has already logged in before.
//Show user profile
}
} catch (e) {
print(e.message);
}}
Edit: Since the question was meant for Facebook Login I am adding that code as well. The code below uses flutter_facebook_login
and firebase_auth
packages.
Future<FirebaseUser> _facebookAuthHandler() async {
final result = await facebookLogin.logInWithReadPermissions(['email']);
final AuthCredential credential = FacebookAuthProvider.getCredential(accessToken: result.accessToken.token);
AuthResult authResult = await _auth.signInWithCredential(credential);
if (authResult.additionalUserInfo.isNewUser) {
//TODO User logging in for the first time. Redirect to Pages.SignUp Page
}
else {
//TODO User has already logged in before. Redirect to Home Page
}
}
In the recent version, there's no authResult class, instead there's userCredential class. Which will give you the additional information about the user and so you will know whether the user is knew.
Future<void> signInWithGoogle() async {
GoogleSignInAccount account = await googleSignIn.signIn();
GoogleSignInAuthentication authentication = await account.authentication;
OAuthCredential credential = GoogleAuthProvider.credential(
accessToken: authentication.accessToken,
idToken: authentication.idToken,
);
UserCredential userCredential =
await _firebaseAuth.signInWithCredential(credential);
User user = userCredential.user;
if (userCredential.additionalUserInfo.isNewUser) {
//Here, you can do something with the profile of a new user. eg. upload information
//in database
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With