Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 489
Noodles Avatar asked Aug 13 '18 09:08

Noodles


People also ask

How do you check if email already exists in Firebase authentication in Flutter?

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. } } }


2 Answers

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
  }

}
like image 171
Akas Antony Avatar answered Sep 23 '22 00:09

Akas Antony


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
}

}

like image 26
Pratham Sarankar Avatar answered Sep 21 '22 00:09

Pratham Sarankar