Noodles
Noodles

Reputation: 4080

Check if user is new using Firebase Authentication in Flutter

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.

Upvotes: 11

Views: 6006

Answers (3)

Adam Smaka
Adam Smaka

Reputation: 6393

Or just do something like that:

final userCredential = await _firebaseAuth.signInWithPopup(provider);
final username = userCredential.additionalUserInfo?.username;

Upvotes: 1

Pratham Sarankar
Pratham Sarankar

Reputation: 599

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
}

}

Upvotes: 4

Akas Antony
Akas Antony

Reputation: 749

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
  }

}

Upvotes: 20

Related Questions