Check if user is new using Firebase Authentication in Flutter

4,432

Solution 1

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
  }

}

Solution 2

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
}

}

Solution 3

Or just do something like that:

final userCredential = await _firebaseAuth.signInWithPopup(provider);
final username = userCredential.additionalUserInfo?.username;
Share:
4,432
Noodles
Author by

Noodles

Updated on December 06, 2022

Comments

  • Noodles
    Noodles over 1 year

    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.